【发布时间】:2011-02-21 13:36:37
【问题描述】:
这是我的 ListView 结构和元素:
// Get a cursor with all phones
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
startManagingCursor(c);
/** ----Display the Contacts on the device----- */
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c,
new String[] { Phones.NAME, Phones.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
setListAdapter(adapter);
而我对 setOnItemClickListener 的实现如下:
/** ----Defining the ItemOnClickListener for the displayed List---- */
final ListView contactlistview = getListView();
contactlistview.setTextFilterEnabled(true);
contactlistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(TAG, String.valueOf(position));
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
contactlistview.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
Log.d(TAG, contactlistview.getItemAtPosition(position).toString());
}
});
最后得到 ListView 为:
当我单击任何项目时,我会看到 Toast 显示以下内容:
但我希望我的 toast 显示所选项目的联系人姓名
你能帮我写代码吗:)
很抱歉给您带来不便:)
谢谢:) :)
【问题讨论】:
标签: android eclipse listview onclick