Java 如何获取ListViewItem的TextView值
我正在使用ListView构建音乐播放列表,例如: 我想得到文本视图的绿色值,对应于点击按钮的项目(即右边的星)。有什么想法吗 activity_row_item.xmlJava 如何获取ListViewItem的TextView值,java,android,listview,Java,Android,Listview,我正在使用ListView构建音乐播放列表,例如: 我想得到文本视图的绿色值,对应于点击按钮的项目(即右边的星)。有什么想法吗 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_pare
<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>
活动列表视图.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>
playlayadapter.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;
}
}
公共类播放适配器扩展了ArrayAdapter{
语境;
国际布局资源;
歌曲[]数据=null;
歌曲持有者=空;
连接包装器连接包装器;
公共播放适配器(上下文上下文、int-layoutResourceId、歌曲[]数据){
超级(上下文、布局资源ID、数据);
this.context=上下文;
this.layoutResourceId=layoutResourceId;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
if(行==null)
{
LayoutFlater充气机=LayoutFlater.from(上下文);
行=充气机。充气(layoutResourceId,父级,false);
holder=新歌曲持有者();
holder.songName=(TextView)row.findViewById(R.id.songName\u text);
holder.score=(TextView)row.findViewById(R.id.score\u text);
holder.likeButton=(ImageButton)row.findViewById(R.id.button_like);
holder.likeButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Runnable r=新的Runnable(){
公开募捐{
试一试{
//获取所单击按钮所属项目的SongHolder#songName
}捕获(IOE异常){
e、 printStackTrace();
}
}
};
新线程(r.start();
}
});
row.setTag(支架);
}
其他的
{
holder=(SongHolder)row.getTag();
}
宋宋=数据[位置];
holder.songName.setText(String.valueOf(song.getSongName());
holder.score.setText(String.valueOf(song.getScore());
返回行;
}
类歌曲持有者{
TextView歌曲名;
文本视图分数;
图像按钮式按钮;
}
}
无需在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;
}
我已经试过了,但没有效果。呈现的索引与单击的项目不对应。啊。。可以您需要设置ClickListenerout-side`==null`条件。编辑了代码。它也不起作用:/n是否有方法获取按钮所属父项的xml树?您的
textview
行中的值设置是否正确?是的,我已仔细检查过。TextView的值似乎是正确的,我已经找到了解决方案。感谢马尔科姆的回答: