2017-09-12 87 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(); 
}