Java 使用自定义适配器获取已检查项

Java 使用自定义适配器获取已检查项,java,android,listview,Java,Android,Listview,我有自定义适配器的listview。此listview的每个元素都有复选框。Standart函数.getCheckEditePositions()不起作用 一旦创建: final String[] words = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" }; MySimpleArrayAdapter

我有自定义适配器的listview。此listview的每个元素都有复选框。Standart函数.getCheckEditePositions()不起作用

一旦创建:

  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;
    }
}
公共类mysimplearlayadapter扩展了ArrayAdapter{
私人最终语境;
私有最终字符串[]值;
数据库助手myDbHelper;
int-id=1;
公共MySimpleArrayAdapter(上下文上下文,字符串[]值){
super(上下文、R.layout.listitem、值);
this.context=上下文;
这个值=值;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行视图=充气机。充气(R.layout.listitem,父项,false);
CheckBox=(CheckBox)rowView.findviewbyd(R.id.CheckBox);
TextView newwordview=(TextView)rowView.findViewById(R.id.newwordview);
newwordview.setText(“lalala”);
返回行视图;
}
}
在这里,我尝试获取检查项目:

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) {

            }
        }
    }
public void addbtnclick(视图){
int cntChoice=listView.getCount();
SparseBooleanArray SparseBooleanArray=listView.getCheckedItemPositions();
for(int i=0;i
在调试sparseBooleanArray中,始终有0项。
我该怎么办?

您可能应该让您的
适配器
成为可以保留其选中状态的对象列表,而不是字符串

public class Item
{
    public String title;
    public boolean checked;
}
然后:

公共类mysimplearlayadapter扩展了ArrayAdapter
{
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视图行视图=充气机。充气(R.layout.listitem,父项,false);
CheckBox=(CheckBox)rowView.findviewbyd(R.id.CheckBox);
TextView newwordview=(TextView)rowView.findViewById(R.id.newwordview);
项目=获取项目(位置);
newwordview.setText(item.title);
checkBox.setChecked(item.checked);
setOnCheckedChangeListener(新的OnCheckedChangeListener()
{
@凌驾
检查更改后的公共void(复合按钮视图,布尔值已检查)
{
项目=获取项目(位置);
item.checked=已检查;
}
});
返回行视图;
}
}
另外:出于性能原因,您确实应该重新使用convertView。我建议查看视图持有者模式:

您收到的问题/错误是什么?在内部类中访问变量“position”;需要声明finalDid您是否注意到并在上面的方法签名中包含
final
关键字
getView(最终int位置,
…在一次checkedChanged中,我必须添加setItemChecked:
myListView.setItemChecked(position,isChecked);
以获得getCheckedItemPositions()的效果。myListView在构造函数中使用
public customAdapter(上下文上下文,列表cat,ListView mListView){..}初始化
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;
    }
}