0

I am trying to populate a listView with two String ArrayLists. These ArrayLists are passed from another activity to the listView activity.

Activity with Listview

public class DecisionActivity extends AppCompatActivity {
  ListView lv;
  customDecisionAdapter decisionAdapter;
  String[] array1, array2 ;
  ArrayList<String> l1;
  ArrayList<String> l2;
  TextView t1,t2;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_decision);
    t1=(TextView) findViewById(R.id.textForl1);
    t2=(TextView) findViewById(R.id.textForl2);
    lv= (ListView) findViewById(R.id.listViewDecision);
    Intent intent = getIntent();
    array1=intent.getStringArrayExtra("List1");
    array2=intent.getStringArrayExtra("List2");
    l1=new ArrayList<>(Arrays.asList(array1));
    l2=new ArrayList<>(Arrays.asList(array2));
    decisionAdapter=new customDecisionAdapter(getApplicationContext(),l1,l2);
    lv.setAdapter(decisionAdapter);
  }
}

Custom Adapter

public class customDecisionAdapter extends BaseAdapter {
  Context context;
  private ArrayList<String> list1;
  private ArrayList<String> list2;

  public customDecisionAdapter(Context context, ArrayList<String>list1, ArrayList<String>list2) {
    this.context= context;
    this.list1= list1;
    this.list2= list2;

  }

  @Override
  public View getView(int i, View view, ViewGroup viewGroup) {
    View convertView =  view;
    if(convertView==null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.customdecision,viewGroup,false);

    }
    TextView t1 = (TextView) convertView.findViewById(R.id.textDisease);
    TextView t2 = (TextView) convertView.findViewById(R.id.textTotal);
    t1.setText(list1.get(i));
    t2.setText(list2.get(i));
    return convertView;
  }

  @Override
  public int getCount() {
    return list1.size();
  }

  @Override
  public Object getItem(int i) {
    if (i>= list1.size())
        return list2.get(i);
    return list1.get(i);
  }

  @Override
  public long getItemId(int i) {
    return i;
  }
}

When I run the program, it crashes and provides the error of:

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2

which point to the line:

t2.setText(list2.get(i));

I don't know what I'm doing wrong here.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
mcprilla79
  • 99
  • 1
  • 11

3 Answers3

1

Check array2 = intent.getStringArrayExtra("List2"); (in DecisionActivity). It is returning array of less size than expected.

Also when you do t2.setText(list2.get(i)); (in customDecisionAdapter), check the size and do a check when you read by index if (i <= list1.size()).

A.A.
  • 866
  • 1
  • 8
  • 22
1
//Replace your adapter code with this, I have check the size of Arraylist  
//inside getCount() and inside getview() method verify value
//of position not greater than size of Arraylist.

public class customDecisionAdapter extends BaseAdapter {
                Context context;
                private ArrayList<String> list1;
                private ArrayList<String> list2;

                public customDecisionAdapter(Context context, ArrayList<String>list1, ArrayList<String>list2) {
                    this.context= context;
                    this.list1= list1;
                    this.list2= list2;

                }

                @Override
                public View getView(int position, View view, ViewGroup viewGroup) {
                    View convertView =  view;
                    if(convertView==null){
                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        convertView = inflater.inflate(R.layout.customedecisio,viewGroup,false);

                    }
                    TextView t1 = (TextView) convertView.findViewById(R.id.textDisease);
                    TextView t2 = (TextView) convertView.findViewById(R.id.textTotal);

                    // Verify value of position not greater than size of ArrayList.
                    if(position < list1.size())
                            t1.setText(list1.get(position));

                    if(position< list2.size())
                         t2.setText(list2.get(position));

                    return convertView;
                }

                @Override
                public int getCount()
                {
                    if(list1.size() < list2.size())
                      return list2.size();
                    else
                       return list1.size();
                }

                @Override
                public Object getItem(int position) {
                    return position;
                }

                @Override
                public long getItemId(int position) {
                    return position;
                }

            }
jessica
  • 1,700
  • 1
  • 11
  • 17
0

Please note that you are returning size of only list1,

  @Override
  public int getCount() {
    return list1.size();
  }

and anyway you can only return size of one list, which means if list2 does not match with that size it will always give that error. Whenever you are using data with more than one list,it is always advisable to use POJO classes.

Pritish
  • 1,284
  • 1
  • 19
  • 42
  • ok . could u give me an example of the POJO class should be like? – mcprilla79 Jan 28 '17 at 08:00
  • Lot of examples out there on google.Just make a java class with the variable names with possibly with the same name as the keys in json , in case you are using third part lib,like GSON. Android studio will let you automatically build getter and setter methods out of that. Then make its object and create a list of it. – Pritish Jan 28 '17 at 08:10
  • For example name and designation are two variables of Employee pojo class.Then Employee employee;would be your class object,and employee.getDesignation() and employee.getName() will be its method. Then make an Employee obj list.like List list. then you can do t1.setText(list.get(position).getDesignation). and so on. – Pritish Jan 28 '17 at 08:10
  • ok, i get it now. Thanks, i'll give u feedback on what happens – mcprilla79 Jan 28 '17 at 08:17