我用checkedtextview实现了listview。现在,我想访问那些已检查的listview项目。我已经设置了onItemClickListener,但是如何知道检查了哪些项?如果单击它,则会自动调用侦听器,它仅计算listview的第一项:
如何确定检查哪些checktext视图,我的意思是如何遍历listview如何?
public void onStart() {
super.onStart();
ListView ch1 = (ListView) findViewById(R.id.listviewdb);
ch1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
DBAdapter db = new DBAdapter(this);
db.openDB();
Cursor c = db.getAllNumbers();
final String[] items = new String[c.getCount()];
int i = 0;
while (c.moveToNext()) {
String name;
name = c.getString(1);
items[i] = name;
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.checkable_list_layout, R.id.txt_title, items);
ch1.setAdapter(adapter);
ch1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView c = (CheckedTextView) findViewById(R.id.txt_title);
if (c.isChecked() == true) {
String selectedItem = c.getText().toString();
Log.d("dbcontacts", selectedItem);
}
}
});
}
答案 0 :(得分:0)
您可以注意到position
方法中有一个名为onItemClick
的参数,它代表listView项目。
ch1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView c = (CheckedTextView) findViewById(R.id.txt_title);
if (c.isChecked() == true) {
int selectedItemPosition = position;
Log.d("dbcontacts", selectedItemPosition);
}
}
});
希望这会有所帮助