2013-02-20 94 views
1

我試圖檢索列表中的項目時點擊一個確定的ID。從Android中的多個ID列表項獲取ID值

如何檢索值+標識/名稱?我想在下面顯示的警告對話框中使用它。

我目前用一個簡單的烤麪包這只是顯示在目前「名稱」時,點擊

下面列表項工作是我的列表項的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(新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(); 
         }}); 




       } 
+0

你的問題是....? – Pragnani 2013-02-20 10:21:17

+0

我不明白你的問題,請詳細說明 – Shoshi 2013-02-20 10:26:51

+0

@Pragnani如何檢索值+標識/名稱?我想在上面顯示的警告對話灰中使用它。 – AndroidEnthusiast 2013-02-20 10:26:52

回答

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

} 

我想這是你想要的...我希望這會幫助你

+0

使用這個名字在AlertDialog ... – Pragnani 2013-02-20 10:33:31

+0

我只是在這條線得到這個「多標記 \t - 視圖不能解決 \t - 類型匹配。從CharSequence的末 – AndroidEnthusiast 2013-02-20 11:08:22

+0

附加的toString()((的TextView)view.findViewById(R.id.name))的getText()的toString() – Pragnani 2013-02-20 11:27:48

0

當我們使用Adapter顯示一個列表。 在getView方法中,我們根據列表中項目的位置從集合中獲取值。我正在討論的適配器的數據源。 是這樣的。

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

當列表項獲得點擊你會得到開始使用listItemOnClickListener點擊的位置。在該回調中, 再次調用數據源的get方法以獲取粘貼到列表項上的元素。 希望你明白了。因爲我沒有發佈太多的代碼來支持它。

相關問題