【问题标题】:Getting checked items with custom adapter使用自定义适配器获取检查项目
【发布时间】:2015-11-15 18:20:50
【问题描述】:

我有带有自定义适配器的列表视图。此列表视图的每个元素都有复选框。标准功能 .getCheckedItemPositions() 不起作用。

创建:

  final String[] words = new String[] {
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"
        };


        MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, words);
        listView.setAdapter(adapter);

我的适配器:

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;
    DataBaseHelper myDbHelper;
    int id = 1;


    public MySimpleArrayAdapter(Context context, String[] values) {
        super(context, R.layout.listitem, values);
        this.context = context;
        this.values = values;
    }

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

        View rowView = inflater.inflate(R.layout.listitem, parent, false);
        CheckBox checkBox = (CheckBox)rowView.findViewById(R.id.checkBox);
        TextView newwordview = (TextView)rowView.findViewById(R.id.newwordview);


        newwordview.setText("lalala");

            return rowView;
    }
}

我在这里尝试检查项目:

public void addbtnclick(View view){
        int cntChoice = listView.getCount();
        SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();

        for (int i = 0; i < cntChoice; i++) {

            if (sparseBooleanArray.get(i) == true) {
                String a = listView.getItemAtPosition(i).toString();
                myDbHelper.setadd(a, "en");
            } else if (sparseBooleanArray.get(i) == false) {

            }
        }
    }

在调试中 sparseBooleanArray 总是有 0 个项目。 我该怎么办?

【问题讨论】:

  • 您收到的问题/错误是什么?

标签: java android listview


【解决方案1】:

您可能应该将您的Adapter 设为可以保留其选中状态的对象列表,而不是字符串。

public class Item
{
    public String title;
    public boolean checked;
}

然后:

public class MySimpleArrayAdapter extends ArrayAdapter<Item>
{
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View rowView = inflater.inflate(R.layout.listitem, parent, false);
        CheckBox checkBox = (CheckBox)rowView.findViewById(R.id.checkBox);
        TextView newwordview = (TextView)rowView.findViewById(R.id.newwordview);

        Item item = getItem(position);
        newwordview.setText(item.title);
        checkBox.setChecked(item.checked);


        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton view, boolean isChecked)
            {
                Item item = getItem(position);
                item.checked = isChecked;
            }
        });

        return rowView;
    }
}

另外:出于性能原因,您确实应该重新使用 convertView。我建议查看视图持有者模式: How can I make my ArrayAdapter follow the ViewHolder pattern?

【讨论】:

  • 在内部类中访问变量“位置”;需要声明为final
  • 您是否注意到并在上面的方法签名中包含了final 关键字? getView(final int position, ...
  • 在 onCheckedChanged 我必须添加 setItemCheckedmyListView.setItemChecked(position, isChecked); 才能让 getCheckedItemPositions() 工作。 myListView 在构造函数中初始化为public customAdapter(Context context,List&lt;myObject&gt; cat,ListView mListView) { ..}
猜你喜欢
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多