简体   繁体   中英

Accessing child views inside a ListItem defined as LinearLayout

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 !

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM