0

我有一个将自定义 ArrayAdapter 作为 ListAdapter 的视图。客户是 Poco:

public class Customer {
    public String LastName;
    public boolean IsActive;
}

什么有效:我已经成功地将我的 ArrayList 绑定到我的 ListView,LastName 显示为文本并且 CheckBox 是否被选中(取决于 IsActive)但现在我想在每次选中复选框时更新 ArrayList 但我不能访问 OnClickListener 中的位置:

setListAdapter(new ArrayAdapter<Customer>(this, android.R.layout.simple_list_item_checked, items) {

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

        View row;
        if (null == convertView) {
            row =  mInflater.inflate(android.R.layout.simple_list_item_checked, null);
        } else {
            row = convertView;
        }

        CheckedTextView ctv = (CheckedTextView) row.findViewById(android.R.id.text1);
        ctv.setText(getItem(position).LastName);
        ctv.setChecked(getItem(position).IsChecked);

        ctv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                CheckedTextView ctv = (CheckedTextView) v;
                ctv.toggle();

                // this does not work because I don't have the position
                getItem(position).IsChecked = (ctv).isChecked();

            }

        });

        return row;
    }

});

我考虑过从 CheckedTextBox 继承并添加一个自定义属性setPosition/getPosition来存储位置以供以后使用,甚至添加对相关对象的引用,但我想有一个我找不到 atm 的现有解决方案。

4

2 回答 2

4

您应该使用 ListView 给它一个 onItemClickListener 并向它传递一个 AdapterView ,它看起来像这样:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
    // here you have th view, the position, everything you need to have fun ;=)
    //you can acces the content of your adapter here with :   
    mAdapter.getItem(pos);
    }
}
于 2010-08-17T14:43:03.957 回答
1

我不会按行处理点击,而是按 ListActivity

public void onListItemClick(ListView l, View v, int position, long id)

为您提供所需的一切。覆盖 ListActivity 中的方法。

于 2010-08-17T14:43:02.783 回答