2017-03-06 36 views
0

我使用一個ListView創建音樂播放列表,例如:如何獲得的ListViewItem的一個TextView值

enter image description here

我想要得到的TextView的綠色對應的項目的價值點擊按鈕(即右​​側的星號)。請任何想法嗎?

activity_row_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/rowItem_layout" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.RowItemActivity" > 

      <TextView 
       android:id="@+id/songName_text" 
       android:layout_width="40dp" 
       android:layout_height="fill_parent" 
       android:layout_gravity="left" 
       android:layout_weight="1" 
       android:background="#8800FF00" 
       android:text="TextView" /> 

      <TextView 
       android:id="@+id/score_text" 
       android:layout_width="5dp" 
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:layout_weight="0.3" 
       android:background="#88FF0000" 
       android:text="TextView" /> 

      <ImageButton 
       android:id="@+id/button_like" 
       android:layout_width="7dp" 
       android:layout_height="fill_parent" 
       android:layout_gravity="right" 
       android:layout_weight="0.20" 
       android:src="@android:drawable/btn_star" /> 

     </LinearLayout> 

activity_list_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.PlayListActivity" > 

    <ListView 
     android:id="@+id/playlist_listView" 
     android:layout_width="372dp" 
     android:layout_height="394dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="1.42" /> 

</RelativeLayout> 

PlayListAdapter.java

public class PlayListAdapter extends ArrayAdapter<Song> { 

    Context context; 
    int layoutResourceId; 
    Song[] data = null; 
    SongHolder holder = null; 
    ConnectionWrapper connectionWrapper; 

    public PlayListAdapter(Context context, int layoutResourceId, Song[] data) { 
     super(context, layoutResourceId, data); 
     this.context = context; 
     this.layoutResourceId = layoutResourceId;   
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView;  

     if(row == null) 
     { 
      LayoutInflater inflater = LayoutInflater.from(context); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      holder = new SongHolder(); 
      holder.songName = (TextView)row.findViewById(R.id.songName_text); 
      holder.score = (TextView)row.findViewById(R.id.score_text); 
      holder.likeButton = (ImageButton)row.findViewById(R.id.button_like); 
      holder.likeButton.setOnClickListener(new View.OnClickListener(){ 
       @Override 
       public void onClick(View v){ 
        Runnable r = new Runnable() { 
         public void run() { 
          try { 
           //get the SongHolder#songName of the item to which belongs the clicked button 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
        }; 
        new Thread(r).start(); 
       } 
      }); 

      row.setTag(holder); 
     } 
     else 
     { 
      holder = (SongHolder)row.getTag(); 
     } 

     Song song = data[position]; 
     holder.songName.setText(String.valueOf(song.getSongName())); 
     holder.score.setText(String.valueOf(song.getScore())); 

     return row; 
    } 

    class SongHolder{ 
     TextView songName; 
     TextView score; 
     ImageButton likeButton; 

    } 
} 
+0

我已經想出瞭解決方案。感謝馬爾科姆的回答:http://stackoverflow.com/a/5004133/7558560 –

回答

0

無需使用線程內ClickListener
只需使用 -

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView;  

    if(row == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     row = inflater.inflate(layoutResourceId, parent, false); 

     holder = new SongHolder(); 
     holder.songName = (TextView)row.findViewById(R.id.songName_text); 
     holder.score = (TextView)row.findViewById(R.id.score_text); 
     holder.likeButton = (ImageButton)row.findViewById(R.id.button_like); 

     row.setTag(holder); 
    } 
    else 
    { 
     holder = (SongHolder)row.getTag(); 
    } 

    Song song = data[position]; 
    holder.songName.setText(String.valueOf(song.getSongName())); 
    holder.score.setText(String.valueOf(song.getScore())); 

    holder.likeButton.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v){ 
       // TextView 
       TextView tvSongName = holder.songName; 
       // if you want to get song name 
       String songName = String.valueOf(song.getSongName()); 
       Log.v("SongName", "" +songName); 
      } 
     }); 

    return row; 
} 
+0

我已經試過這個,它沒有工作。呈現的索引與所點擊的項目不符。 –

+0

啊..好吧!你需要將'setClickListener'設置爲'== null'狀態。編輯代碼。 – Wizard

+0

它也沒有工作:/是否有一種方法來獲取屬於按鈕的父項的xml樹? –