2017-09-12 91 views
0

我有一些问题循环使用我自己的自定义adapterclass的列表视图中的所有对象。这个想法是,对于复选框被选中的每一行,都会发生一些事情。现在,函数checkBoxCount正确地标识了选中的框的数量和位置。但是,当我检查多个框时,我得到:通过listview循环的问题Android

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 

错误发生在我分配listitem到“f”的行。

void checkBoxCount(){ 

    for (int i = 0; i < list.getCount(); i++) { 
     CheckBox cb = list.getChildAt(i).findViewById(R.id.checkBoxSendAnytimerTo); 

     if (cb.isChecked()){ 
      Log.d("i= ",String.valueOf(i)); 
      f = (Friend) list.getItemAtPosition(i); 

     } 

    } 

} 

这是我的适配器,因为我觉得有些事情会错有:

public class SendAnytimerAdapter extends ArrayAdapter<Friend> { 

    ArrayList<Friend> friendList = new ArrayList<>(); 

    public SendAnytimerAdapter(Context context, int textViewResourceId, ArrayList<Friend> objects) { 
     super(context, textViewResourceId, objects); 
     friendList = objects; 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View v; 
     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.send_anytimer_adapter_layout, null); 
     TextView textView1 = (TextView) v.findViewById(R.id.textViewSendAnytimerAdapterName); 
     TextView textView2 = (TextView) v.findViewById(R.id.textViewSendAnytimerAdapterCount); 
     textView2.setText(friendList.get(position).getAnytimerCountFriend()); 
     textView1.setText(friendList.get(position).getfriendUserName()); 
     return v; 
    } 

在哪里我的错误在于任何线索?

在此先感谢!

+0

有没有什么地方你删除列表中的所有项目? – Vodet

+0

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

+0

该死的,你是对的。有时间去拜访我的配镜师 –

回答

0

你需要覆盖的方法getCount将

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