Android : Select a Text-view from a selected List Item

on Wednesday, September 17, 2014


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 same value for every item. here is 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. please help me guys. i hav spend a lot of time searching for solutions.



<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"/>

0 comments:

Post a Comment