2010-11-17 18 views
1

は私がsqlliteから照会活性を有し、XMLandroid:クリックするとテキストビューのIDを取得する方法は?

に出力を表示するには、この活動

((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()+""); 

の一部であり、これは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> 

さて、これは正常に動作します。結果のクエリが表示されます。ここでは、長押しに応答する別のアクティビティ、またはクエリが表示されたときにワンタッチで反応するアクティビティを作成します。押されたテキストビューのIDを取得して他のアクティビティに使用するにはどうしたらいいですか?

ご協力いただきありがとうございます。

答えて

8

view.getId()?

8

代わりに、次のように行います

TextView tvId = (TextView) findViewById(R.id.tv_id); 
tvId.setText(listItem.getId()+""); 
tvId.setOnClickListener(this); 

、その後onClickEventにあなたがのgetId()メソッドで、「アプリケーション」の書き込みなどのテキストビューのIDを取得します。

1

あなたは、IDの文字列をしたい場合は、以下の方法で使用することができます。

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

    } 
関連する問題