Android 获取checkedtextview选中的位置
我有一个列表活动,我在那里做这个Android 获取checkedtextview选中的位置,android,listadapter,Android,Listadapter,我有一个列表活动,我在那里做这个 Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[] { Phone._ID, Phone.DISPLAY_NAME }, null, null, null); startManagingCursor(cursor); String[] columns = new String[] { Phone
Cursor cursor = getContentResolver().query(Phone.CONTENT_URI,
new String[] { Phone._ID, Phone.DISPLAY_NAME }, null, null,
null);
startManagingCursor(cursor);
String[] columns = new String[] { Phone.DISPLAY_NAME };
int[] to = new int[] { R.id.name_row };
ListAdapter mAdapter = new SimpleCursorAdapter(this,
R.layout.contact_list_row, cursor, columns, to);
this.setListAdapter(mAdapter);
它绑定了我的联系人姓名,而不是我的listadapter。每一行都是checkedtext视图,所以我想创建一个方法来检索我的选中项,但我无法使它工作。有人能给我指出正确的方向吗?我对复选框不是100%确定,但我已经对按钮和其他元素做了这样的操作,并且快速查看了一下,我认为这个方法是相同的 在复选框元素布局XML中插入android:onClick=methodName 在您的代码中有类似于此函数的内容:
//Called from XML
public void methodName(View view) {
int cursorPosition= getListView().getPositionForView(view);
Bundle itemData = (Bundle) mAdapter.getItem(cursorPosition);
// do something with data
}
所以我找到了我的路。问题是我使用了checked_positions.geti而不是checked_positions.valueAti
ListView lv = getListView();
SparseBooleanArray checked_positions = lv.getCheckedItemPositions();
for (int i = 0; i < checked_positions.size(); i++) {
Log.d(TAG, "Checked " + i + " Value " + checked_positions.valueAt(i));
if (checked_positions.valueAt(i)) {
Cursor ctv = (Cursor) lv.getItemAtPosition(checked_positions.keyAt(i));
}
}