I have a custom ListView. This ListView contains 1 Image and 6 TextViews. To retrieve the value I have created a setOnItemClickListener(...). Whenever I click on the ListView how could I actually retrieve all the data from the 6 TextViews?
- 3,685
- 3
- 27
- 39
- 341
- 2
- 5
- 7
6 Answers
Sample Code:
ListView list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = list.getItemAtPosition(position);
}
});
In the sample code above, the listItem should contain the selected data for the textView.
-
6guys i am not able to sense the touch even if i write a toast inside onItemClick it is not showing – abhishek Jul 15 '11 at 13:32
-
1@abhishek , same issue with me also – Arj 1411 Jun 23 '17 at 05:44
I too had that same problem.. If we think logically little bit we can get the answer.. It worked for me very well.. I hope u will get it..
listviewdemo.xml<ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="30dp" android:paddingLeft="10dp" android:paddingRight="10dp" />listviewcontent.xml- note thatTextView-android:id="@+id/txtLstItem"<LinearLayout android:id="@+id/listviewcontentlayout" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal"> <ImageView android:id="@+id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="6dp" /> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/txtLstItem" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="left" android:shadowColor="@android:color/black" android:shadowRadius="5" android:textColor="@android:color/white" /> </LinearLayout> <ImageView android:id="@+id/img2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="6dp" /> </LinearLayout>ListViewActivity.java- Note thatview.findViewById(R.id.txtLstItem)- as we setting the value toTextViewbysetText()method we getting text fromTextViewbyViewobject returned byonItemClickmethod.OnItemClick()returns the current view.TextView v=(TextView) view.findViewById(R.id.txtLstItem); Toast.makeText(getApplicationContext(), "selected Item Name is "+v.getText(), Toast.LENGTH_LONG).show();**Using this simple logic we can get other values like
CheckBox,RadioButton,ImageViewetc.ListView List = (ListView) findViewById(R.id.listview); cursor = cr.query(CONTENT_URI,projection,null,null,null); adapter = new ListViewCursorAdapter(ListViewActivity.this, R.layout.listviewcontent, cursor, from, to); cursor.moveToFirst(); // Let activity manage the cursor startManagingCursor(cursor); List.setAdapter(adapter); List.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick (AdapterView < ? > adapter, View view,int position, long arg){ // TODO Auto-generated method stub TextView v = (TextView) view.findViewById(R.id.txtLstItem); Toast.makeText(getApplicationContext(), "selected Item Name is " + v.getText(), Toast.LENGTH_LONG).show(); } } );
- 405
- 7
- 17
- 141
- 1
- 2
If it helps anyone, I found that the problem was I already had an android:onClick event in my layout file (which I inflated for the ListView rows). This was superseding the onItemClick event.
- 751
- 2
- 12
- 22
If in the listener you get the root layout of the item (say itemLayout), and you gave some id's to the textviews, you can then get them with something like itemLayout.findViewById(R.id.textView1).
- 15,087
- 7
- 65
- 82
-
@kangalert you're welcome. remember to mark the accepted answers (click on the checkmark next to the answer), to let people know that's a working solution. it also raises yours and answerer's reputation :) – bigstones Jan 18 '11 at 14:16
If above answers don't work maybe you didn't add return value into getItem method in the custom adapter see this question and check out first answer.
- 1
- 1
- 1,023
- 2
- 13
- 29
Sorry for coding with Kotlin. But I faced the same problem. I solved with the code below.
list.setOnItemClickListener{ _, view, _, _ ->
val text1 = view.find<TextView>(R.id.~~).text
}
You can put an id which shows a TextView that you want in "~~".
Hope it'll help someone!
- 99
- 9