当只检查1时,CheckedTextView显示多个已检查项目是否还有其他问题?单击CheckedTexView项时,OnClickListener的响应是检查单击项之前和之后的条目。
以下是代码:
mFriendDoneButton = (Button) findViewById(R.id.doneAddAFriendButton);
mListView = (ListView)findViewById(R.id.contactList);
populateContactList();
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mListView.setItemsCanFocus(false);
mListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,
int arg2, long arg3) {
int selectedPosition = arg2;
CheckedTextView textView = (CheckedTextView)view.findViewById(R.id.friendEntryText);
String mtext = textView.getText().toString();
Log.i("AddAFriendActivity", "Click on position "+selectedPosition);
Toast t = new Toast(AddAFriendActivity.this);
t = Toast.makeText(AddAFriendActivity.this, "Clicked on " + arg2+mtext+arg3, Toast.LENGTH_LONG);
t.show();
}
});
private void populateContactList(){ //使用联系人条目构建适配器 游标光标= getContacts(); String [] fields = new String [] { ContactsContract.Data.DISPLAY_NAME }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.friend_entry,cursor,fields,new int [] {R.id.friendEntryText}); mListView.setAdapter(适配器); }
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
XML如下:
答案 0 :(得分:0)
发现问题... textView需要声明为字段,否则managedQuery结果循环通过onClickListener。