【问题标题】:ListView items with two rows具有两行的 ListView 项目
【发布时间】: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


    【解决方案1】:

    您在此处打印对象的值。

    现在试试这样:

    Cursor c = ((Cursor) parent.getAdapter().getItem(position));
    
    Toast.makeText(getApplicationContext(), 
                       c.getString(Phone.NAME),Toast.LENGTH_SHORT).show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      相关资源
      最近更新 更多