I have a Listview that originally listed items from a menu item with one single TextView. The code I used to retrieve the item selected is
menuList.setOnItemClickListener(new Ada开发者_JS百科pterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id)
{
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
});
I've now added another TextView so that I have two values for each item one under the other and my menu_item.xml looks like this
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="@+id/rowid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/exhibitor_header" />
<TextView
android:id="@+id/rowidtwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/listview_background"
android:layout_below="@+id/rowid"/>
</RelativeLayout>
How can I retrieve the value for rowid when the item is selected?
Then instead of casting the whole view, find inside that view the correct TextView
object. You can reference it by using the findViewById
method and the TextView
's id:
TextView textView = (TextView) itemClicked.findViewById(R.id.rowid);
String strText = textView.getText().toString();
TextView textView2 = (TextView) itemClicked.findViewById(R.id.rowidtwo);
String strText2 = textView2.getText().toString();
I suggest to use custom view and custom adapter to have 2 textviews in the list view items and bundle those 2 text views in a LinearLayout.
Now when the onClick
called, just cast it in linearlayout and on basis of that by using findviewbyid you can have the access to both of the text views.
精彩评论