Neuron
Neuron

Reputation: 1129

ArrayList java error

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; 
    }
}

Upvotes: 1

Views: 171

Answers (4)

Ganesh Sahu
Ganesh Sahu

Reputation: 41

Please use checks in order to avoid arrayindex exceptions

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

Upvotes: 0

HenriqueMS
HenriqueMS

Reputation: 3974

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));

    //...

Upvotes: 1

Bills
Bills

Reputation: 798

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.

Upvotes: 0

Kamil Banaszczyk
Kamil Banaszczyk

Reputation: 1153

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

Upvotes: 1

Related Questions