1

我正在尝试在 setOnItemClickListener 方法中检索在我的 ListItem 布局中定义的 TextView 的 getText() 值。

基本上,我想记录数据(Id、LookupKey、DisplayName 或 PhoneNumber),以便我检索选择的联系人,而不管使用的列表视图/适配器如何。

我显示了一个简单的 TextView 和一个 Imageview,其可见性是根据数据设置的。这是布局:contact_entry.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:baselineAligned="false"
android:layout_width="fill_parent" android:padding="5px">
            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:textSize="16px"
                android:text="@+id/contactEntryText" android:id="@+id/contactEntryText">
            </TextView>
            <TableRow android:paddingLeft="5px" android:visibility="visible"
                android:paddingRight="5px" android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/indicator_logo_table"
                android:gravity="right">
                <ImageView android:layout_width="wrap_content"
                    android:id="@+id/imageView1"
                    android:layout_height="fill_parent"
                    android:src="@drawable/indicator">
                </ImageView>
            </TableRow>
</LinearLayout>

我使用的适配器定义如下:

SimpleAdapterWithImageView adapter = new SimpleAdapterWithImageView(
            this, R.layout.contact_entry, cursor, fields,
            new int[] { R.id.contactEntryText });
    mContactList.setAdapter(adapter);

(您可能已经猜到它是用于从 ContactsContract ContentProvider 显示联系人的 Display_Name。)

最后是 mContactList.setOnItemClickListener ...

mContactList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);
Log.i("done","linear layout"); // it's not done at all ! findViewById returns null;

//this part is currently useless
TextView displayed_name = (TextView) linear.findViewById(R.id.contactEntryText);
Log.i("done","Displayed name = linear.findViewById contactentrytext");


 Toast.makeText(getApplicationContext(),
             // displayed_name.getText(), //
             //view.getContentDescription(), //
             Toast.LENGTH_SHORT).show();
    }       

正如你所看到的,我现在只是想展示一个祝酒词,最后这个名字将作为一个额外的意图。

所以:我在过去 36 小时(叹息)中得到的最好结果是显示吐司中显示的第一个联系人的姓名,即使单击第二项也是如此......

TextView displayed_name = (TextView) findViewById(R.id.contactEntryText);
Toast.makeText(getApplicationContext(),displayed_name.getText(),
                          Toast.LENGTH_SHORT).show();
                    }

我一直在尝试像谷歌在此处提供的教程之类的东西,其中(我错了吗?我是 Android 新手,所以不要犹豫打我耳光……)实际上将整个视图投射到 TextView 中。 .

加上其他技巧,如 view.getChildAt() 等等......几个小时。

我确信这并不难,但我正在兜圈子!我不认为这是因为我的适配器,但如果有人认为这可能是原因,我可以在另一篇文章中提供代码。

提前致谢 !

4

1 回答 1

1
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);

这是一个错误 - 你不能使用R.layoutin findViewById。通常,您需要在那里使用,或者通过扩展布局来R.id替换。findViewById但在这种情况下,您只需要viewfrom 参数。

用这个替换引用的代码:
LinearLayout linear = (LinearLayout) view;

于 2011-03-17T15:37:12.357 回答