I have a custom ListView
. This ListView
contains 1 Image and 6 T开发者_运维技巧extViews. 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?
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
.
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 toTextView
bysetText()
method we getting text fromTextView
byView
object returned byonItemClick
method.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
,ImageView
etc.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(); } } );
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)
.
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.
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.
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!
精彩评论