Reputation: 11
I have an activity that queries from sqllite and display the output to xml
this is part of the activity
( (TextView) view.findViewById(R.id.tv_id) ).setText( listItem.getId()+"");
( (TextView) view.findViewById(R.id.tv_name) ).setText( listItem.getName() );
( (TextView) view.findViewById(R.id.tv_age) ).setText( listItem.getAge()+"" );
and this is the xml:
<TableLayout
android:layout_width="wrap_content"
android:id="@+id/TableLayout01"
android:layout_below="@+id/tv_age"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_name">
<TableRow
android:layout_width="wrap_content"
android:layout_below="@+id/tv_age"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/TableLayout01"
android:id="@+id/TableRow01">
<TextView
android:layout_width="10dip"
android:layout_height="30dip"
android:id="@+id/tv_id"
android:text=""
android:layout_centerVertical="true">
</TextView>
<TextView
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="" android:id="@+id/tv_name"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/tv_id"
android:layout_marginLeft="10px">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" android:id="@+id/tv_age"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/tv_name"
android:layout_marginLeft="10px">
</TextView>
</TableRow>
</TableLayout>
Now this works fine. It displays the resulting query. Now, I want to create another activity which responds to the long press or just one touch when the query is displayed. How can I get the ID of the pressed textview so I can use it for the other activity?
many thanks for your help.
Upvotes: 1
Views: 61238
Reputation: 2872
Do like this instead:
TextView tvId = (TextView) findViewById(R.id.tv_id);
tvId.setText( listItem.getId()+"");
tvId.setOnClickListener(this);
and then in the onClickEvent
you get the id of the text view like "apps" write, with the getId()
method.
Upvotes: 13
Reputation: 23000
if you want the String of the id you can use the following method:
String name = getIDName(MyTextView, R.id.class);
public static String getIDName(View view, Class<?> clazz) throws Exception {
Integer id = view.getId();
Field[] ids = clazz.getFields();
for (int i = 0; i < ids.length; i++) {
Object val = ids[i].get(null);
if (val != null && val instanceof Integer
&& ((Integer) val).intValue() == id.intValue()) {
return ids[i].getName();
}
}
return "";
}
Upvotes: 1