使用两个ArrayLists填充列表视图

时间:2017-01-27 23:40:38

标签: android

我正在尝试使用两个String ArrayLists填充listView。这些ArrayLists从另一个活动传递到listView活动。

使用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);
  }
}

自定义适配器

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

当我运行程序时,它会崩溃并提供错误:

  

java.lang.IndexOutOfBoundsException:索引2无效,大小为2

指向该行:

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

我不知道我在这里做错了什么。

3 个答案:

答案 0 :(得分:1)

检查array2 = intent.getStringArrayExtra("List2");(在DecisionActivity中)。它正在返回比预期更小的数组。

当您执行t2.setText(list2.get(i));(在customDecisionAdapter中)时,检查尺寸并在您按索引if (i <= list1.size())阅读时进行检查。

答案 1 :(得分: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;
                }

            }

答案 2 :(得分:0)

请注意,您只返回list1的大小,

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

并且无论如何你只能返回一个列表的大小,这意味着如果list2与该大小不匹配,它将始终给出该错误。每当您使用包含多个列表的数据时,始终建议使用POJO类。