循环访问listview Android

时间:2017-09-12 08:57:21

标签: java android listview arraylist

我在使用自己的自定义适配器类的listview中循环遍历所有对象时遇到了一些问题。我们的想法是,对于选中复选框的每一行,都会发生一些事情。现在,函数checkBoxCount正确识别复选框的数量和位置。但是,当我检查多个盒子时,我得到了:

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

关于我的错误所在的任何线索?

提前致谢!

1 个答案:

答案 0 :(得分:0)

您需要覆盖方法getCount

std::string