我使用ListView构建音乐播放列表,例如:
我希望将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 :(得分:0)
不需要在ClickListener中使用Thread只需使用 -
@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;
}