【问题标题】:Issues looping through listview Android循环通过listview Android的问题
【发布时间】:2017-09-12 08:57:21
【问题描述】:

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

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

错误发生在我将列表项分配给“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;
    }

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

提前致谢!

【问题讨论】:

  • 您是否在某个地方删除了列表中的所有项目?
  • @Override public int getCount() { returnfriendList .size(); }
  • 该死,你是对的。是时候拜访我的配镜师了...

标签: java android listview arraylist


【解决方案1】:

你需要重写getCount方法

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 2018-09-23
    • 1970-01-01
    相关资源
    最近更新 更多