1

リスト内のアイテムがクリックされたときに特定のIDを取得しようとしています。

+ id / nameの値を取得するにはどうすればよいですか?以下に示すアラートダイアログアッシュで使用したい。

私は現在、リストアイテムがクリックされた瞬間に「名前」が表示される単純なトーストを使用しています

これが私のリストアイテムのxmlコードです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">

<TextView
  android:id="@+id/text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_toLeftOf="@+id/image" android:textSize="20dip"   
      android:layout_marginLeft="10dip"/>

<ImageView
  android:id="@+id/image"
  android:layout_width="50dip"
  android:layout_height="50dip"
  android:layout_centerHorizontal="true"
  android:src="@drawable/stub" android:scaleType="centerCrop"/>

<TextView
  android:id="@+id/name"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_toRightOf="@+id/image"
  android:textSize="20dip" android:layout_marginLeft="10dip"/>

</RelativeLayout>

アラートダイアログコード:

list.setOnItemClickListener(new OnItemClickListener(){

                    String artistname = "get +id/name here";
                    String items[] = {"Youtube", "Soundcloud"};
                         @Override
                         public void onItemClick(AdapterView<?> parent, View view,
                                 int position, long id) {

                             AlertDialog.Builder builder = new 
                                     AlertDialog.Builder(JsonActivity.this);

                             builder.setTitle(artistname);
                             builder.setItems(items, new   
                                     DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int which) {

                             }
                             });

                             AlertDialog alert = builder.create();
                             alert.show();
                         }});




                }
4

2 に答える 2

2

これを試して

 public void onItemClick(AdapterView<?> parent, View view,
                                 int position, long id) {
//name of the artist...
String name=((TextView)view.findViewById(R.id.name)).getText();

}

これがあなたが望むものだと思います...これがあなたに役立つことを願っています

于 2013-02-20T10:30:51.847 に答える
0

Adapter を使用してリストを表示する場合。getView メソッドでは、リスト内の項目の位置に基づいてコレクションから値をフェッチします。私が話しているアダプターのデータソース。このような。

public View getView(){
   holder.name.setText(data.get(position));
}

リスト項目がクリックされると、listItemOnClickListener を使用してクリックされた位置を取得します。そのコールバックで、データ ソースの get メソッドを再度呼び出して、リスト項目に貼り付けた要素を取得します。あなたが要点を得たことを願っています。それをサポートするためのコードをあまり投稿しなかったためです。

于 2013-02-20T10:24:35.677 に答える