2017-03-06 26 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树? –