1

So, I have to write an app with ListView. It have to work: you are clicking on item and it direct you into the second activity. There must be information from JSON. I done it with okHttp. I'm not far from my destiny, but here is an error. SOLVED. Next problem: there is no reaction on clicking on ListView item.

MainActivity.java (part of problem code):

public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<String> list = new ArrayList<>();
private ArrayAdapter<String> adapter;
private List<MyDataModel> datas = new ArrayList<>();

public class MyClickListener implements AdapterView.OnItemClickListener{
    List<MyDataModel> datas;

    public MyClickListener(List<MyDataModel> datas){
        this.datas = datas;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){
        Intent intent = new Intent(MainActivity.this, DetailsActivity.class);

        if(position < datas.size()){
            MyDataModel data = datas.get(position);
            intent.putExtra(Key.KEY_ID,data.getItemId());
            intent.putExtra(Key.KEY_ABOUT,data.getAbout());
            intent.putExtra(Key.KEY_ADDRESS,data.getAddress());
            intent.putExtra(Key.KEY_COMPANY,data.getCompany());
            intent.putExtra(Key.KEY_EMAIL,data.getEmail());
            intent.putExtra(Key.KEY_FIRST_NAME,data.getFirstName());
            intent.putExtra(Key.KEY_LAST_NAME,data.getLastName());
            intent.putExtra(Key.KEY_PHONE,data.getPhone());

        }
        datas.add(new MyDataModel());
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    list = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, list);

    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new MyClickListener(datas));

And code of DetailsActivity.java:

public class DetailsActivity extends AppCompatActivity{

    private TextView textViewId;
    private TextView textViewAbout;
    private TextView textViewAddress;
    private TextView textViewCompany;
    private TextView textViewEmail;
    private TextView textViewFirstName;
    private TextView textViewLastName;
    private TextView textViewPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        textViewId = (TextView) findViewById(R.id.textViewId);
        textViewAbout = (TextView) findViewById(R.id.textViewAbout);
        textViewAddress = (TextView) findViewById(R.id.textViewAddress);
        textViewCompany = (TextView) findViewById(R.id.textViewCompany);
        textViewEmail = (TextView) findViewById(R.id.textViewEmail);
        textViewFirstName = (TextView) findViewById(R.id.textViewFirstName);
        textViewLastName = (TextView) findViewById(R.id.textViewLastName);
        textViewPhone = (TextView) findViewById(R.id.textViewPhone);

        Intent intent = getIntent();

        textViewId.setText(String.valueOf(intent.getIntExtra(Key.KEY_ID, 0)));
        textViewAbout.setText(intent.getStringExtra(Key.KEY_ABOUT));
        textViewAddress.setText(intent.getStringExtra(Key.KEY_ADDRESS));
        textViewCompany.setText(intent.getStringExtra(Key.KEY_COMPANY));
        textViewEmail.setText(intent.getStringExtra(Key.KEY_EMAIL));
        textViewFirstName.setText(intent.getStringExtra(Key.KEY_FIRST_NAME));
        textViewLastName.setText(intent.getStringExtra(Key.KEY_LAST_NAME));
        textViewPhone.setText(intent.getStringExtra(Key.KEY_PHONE));
    }
}

And I think it will be useful to give you MyDataModel.java:

public class MyDataModel {
    private String about;
    private String address;
    private String company;
    private String email;
    private String firstname;
    private String lastname;
    private String phone;
    private int itemId;
    private String photo;

    public String getAbout(){
        return about;
    }
    public void setAbout(String about){
        this.about = about;
    }

    public String getAddress(){
        return address;
    }
    public void setAddress(String address){
       this.address = address;
    }

    public String getEmail(){
        return email;
    }
    public void setEmail(String email){
        this.email = email;
    }

    public String getFirstName(){
        return firstname;
    }
    public void setFirstName(String firstname){
        this.firstname = firstname;
    }

    public String getCompany(){
        return company;
    }
    public void setCompany(String company){
        this.company = company;
    }

    public String getLastName(){
        return lastname;
    }
    public void setLastName(String lastname){
        this.lastname = lastname;
    }

    public String getPhone(){
        return phone;
    }
    public void setPhone(String phone){
        this.phone = phone;
    }

    public int getItemId(){
        return itemId;
    }
    public void setItemId(int itemId){
        this.itemId = itemId; 
    }

    public String getPhoto(){
        return photo;
    }
    public void setPhoto(String photo){
        this.photo = photo; 
    }
}
3
  • 3
    You try to access the element number 1 in an empty list. Commented Oct 19, 2016 at 11:08
  • 4
    you are passing list in adapter, but accessing datas in onItemClick() Commented Oct 19, 2016 at 11:09
  • Create List datas = new ArrayList<MyDataModel>();, add something before to get MyDataModel data = datas.get(position); Commented Oct 19, 2016 at 11:09

4 Answers 4

1

When you get a java.lang.IndexOutOfBoundsException Means you are trying to access a non-existing index in your data structure, in this case index 1 , when the total size is 0 (Index: 1, Size: 0)

From the official Java documentation:

public class IndexOutOfBoundsException extends RuntimeException

Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.

Advice: First you define your listener, you pass the list reference to te constructor, so when the user clicks, you can check if it is empty or not

public class MyClickListener implements AdapterView.OnItemClickListener{

List<MyDataModel> datas;

public MyClickListener(List<MyDataModel> datas)
{

    this.datas = datas;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Intent intent = new Intent(MainActivity.this, DetailsActivity.class);

    //--> this was a problem--> datas = new ArrayList<MyDataModel>();

    if (position < datas.size())
    {
        MyDataModel data = datas.get(position);
        intent.putExtra(Key.KEY_ID,data.getItemId());
        intent.putExtra(Key.KEY_ABOUT,data.getAbout());
        intent.putExtra(Key.KEY_ADDRESS,data.getAddress());
        intent.putExtra(Key.KEY_COMPANY,data.getCompany());
        intent.putExtra(Key.KEY_EMAIL,data.getEmail());
        intent.putExtra(Key.KEY_FIRST_NAME,data.getFirstName());
        intent.putExtra(Key.KEY_LAST_NAME,data.getLastName());
        intent.putExtra(Key.KEY_PHONE,data.getPhone());

        view.getContext().startActivity(intent);
    }
}

}

then set the listener wherever you want

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //...
    listView.setOnItemClickListener(new MyClickListener(datas));

    //...
Sign up to request clarification or add additional context in comments.

8 Comments

Done this, but in " listView.setOnItemClickListener(new MyClickListener(datas)); " the error of resolving symbol "datas". How can I solve it?
That is your data structure =) meaning that you must have defined it somewhere else before the listener constructor !
Excuse me, don't understand, just a beginner in coding. Please, explain me. I created class "MyClickListener" right after class "MainActivity" begins. And put "listView......." right after "listView.setAdapter(adapter)". What i'm doing wrong? Thanks
I putted "private List<MyDataModel> datas = new ArrayList<>();" on right after "MainActivity" class started and there are no errors, but when i'm clicking on list item - there is no reaction..
That is the correct behaviour =) The if condition is not fulfilled, so nothing happens. Think about it: if (position < datas.size()){...} your datas has just been created, nothing will ever happen, as intended. If the code were to be executed you would get the original exception. Solution: you must add some data to the list : datas.add(new MyDataModel(...)); after you do this, the code inside the if condition will run. There is no problem in being a beginner, that is the purpose of this site.
|
1

When you are instantiating datas

datas = new ArrayList<MyDataModel>();

it will create new ArrayList with 0 elements

then you are trying to obtain something from it:

MyDataModel data = datas.get(position);

But it is empty, that is why you have:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

Comments

0

You are not inserting data in private List<MyDataModel> datas = new ArrayList<>();. And I think you should not put datas = new ArrayList<MyDataModel>(); in ononItemClick because it empty the array, each time it is called.

Data should be added in datas ArrayList something like datas.add(new MyDataModel());

You should pass same ArratList to ArrayAdapter.

2 Comments

Great work, but it added on DetailsActivity only "0"
I just gave hint, you should have all objects in the list. You can loop to add the object in list. Or any other way according to your application working.
0

Please use checks in order to avoid arrayindex exceptions

datas = new ArrayList<MyDataModel>();
if(datas.size()>0)
{
MyDataModel data = datas.get(position);
}

2 Comments

Lists have a Method isEmpty() that can be used instead of checking .size()>0 manually.
yeah more or less the same thing public boolean isEmpty() { return size == 0; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.