Search code examples
javaandroidlistviewlistviewitem

Select a text-view from a selected list item


This is my code:

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

            // TODO Auto-generated method stub
            View vie = (View)parent.getParent();
            TextView tv = (TextView) vie.findViewById(R.id.tv_id);
            String genus = (String) tv.getText();
            Toast.makeText(getBaseContext(), genus+"--"+id, Toast.LENGTH_SHORT).show();
        }
    });

    return adapter;
}

Every time it is returning the same value for every item. Here are my list items:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="20dp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/iv_flag"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_below="@id/tv_country"
        android:layout_centerVertical="true"
        android:padding="5dp"
       />

    <TextView
        android:id="@+id/tv_country_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_flag"
        android:layout_below="@id/tv_country"  />
     <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_country_details"
       />
 </RelativeLayout>

Here is my view file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/lv_countries"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".NearbyGymList" />
    <Button android:id="@+id/btnNoResults"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/lv_countries"
                android:text="No More Results"/>

</RelativeLayout>

How can fix this problem?


Solution

  • You're not adding a reference to your others TextViews. Do it this way:

    Remove this line

    View vie = (View)parent.getParent();
    

    so your code will become:

        mListView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
    
                // TODO Auto-generated method stub
                TextView tv1 = (TextView) view.findViewById(R.id.tv_id);
                TextView tv2 = (TextView) view.findViewById(R.id.tv_country_details);
                String genus = (String) tv1.getText().toString();
                String genus2 = (String) tv2.getText().toString();
    
                Toast.makeText(getBaseContext(),genus+"--"+id+" "+genus2 , Toast.LENGTH_SHORT).show();
            }
        });
    
        return adapter;
    }