访问定义为 LinearLayout 的 ListItem 内的子视图

发布于 2024-10-24 05:11:28 字数 3232 浏览 1 评论 0原文

我正在尝试检索在 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 小时(叹气)我得到的最好结果是显示 toast 中显示的第一个联系人的姓名,即使单击第二个项目也是如此...

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

我一直在尝试像 google 提供的教程这样的东西 这里,(我错了吗?我是 Android 新手,所以不要犹豫,打我的脸...)实际上将整个 View 投射到 TextView 中...

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

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

提前致谢 !

I'm trying to retrieve the getText() value of a TextView defined in my ListItem Layout, inside the setOnItemClickListener method.

Basically, I want to log data (Id, LookupKey, DisplayName, or PhoneNumber) allowing me to retrieve the contact selected regardless of the listview/adapter used.

I display a simple TextView and an Imageview which visibility is set according to the data.
here is the layout : 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>

the adapter i use is defined below :

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

( You could have guessed it's for displaying the contact's Display_Name from the ContactsContract ContentProvider. )

and here is finally the 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();
    }       

As you can see i'm only trying to display a toast for the moment, in the end the name will be put as an extra for an intent.

So : the best result i got these last 36 hours (sigh) is displaying the name of the first contact displayed in the toast, even when clicking on the second item...

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

I've been trying stuff like the tutorial google provided here, which (am I wrong? I'm new to Android so don't hesitate to slap me in the face ...) actually casts the whole View in a TextView ...

plus other tricks like view.getChildAt() and so on... for hours.

I'm convinced it's not that hard, yet i'm running in circles !
I don't think it is because of my adapter, but if anyone thinks it could be the cause, i could provide the code in another post.

Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ペ泪落弦音 2024-10-31 05:11:28
LinearLayout linear = (LinearLayout) view.findViewById(R.layout.contact_entry);

这里有一个错误 - 你不能在 findViewById 中使用 R.layout。通常,您需要在那里使用R.id,或者通过膨胀布局来替换findViewById。但在这种情况下,您只需要参数中的view

将引用的代码替换为以下代码:
LinearLayout 线性 = (LinearLayout) 视图;

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

Here is a mistake - you shan't use R.layout in findViewById. Usually, you need to use R.id there, or replace findViewById by inflating a layout. But in this case you just need the view from arguments.

Replace the quted code with this one:
LinearLayout linear = (LinearLayout) view;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文