gpt4 book ai didi

java - 在另一个 ImageView 和 TextView 中设置 ListView 项的图像和文本

转载 作者:行者123 更新时间:2023-12-02 10:44:11 41 4
gpt4 key购买 nike

我有一个图像和字符串数组,如下所示:

String[] stations = new String[] {
"GHOTKI 91 Radio FM","UmerKot 91.4 Radio","TMK 100.20 Radio"
};

public static int [] images ={R.drawable.ghotkifmlogo,R.drawable.umerkotfmlogo,R.drawable.tmkfmlogo};

在自定义 ListView 中,如何在另一个 ImageView 和 TextView 中替换它们?

我每次都需要使用单击项目的图像和文本更改该 ImageView 和 TextView 。

这是我的自定义适配器:

public class TrackAdapter extends  BaseAdapter{
String [] description;
Context context;
int [] imageId;

public TrackAdapter(Context c, String[] d, int[] prgmImages) {

description=d;
context= c;
imageId=prgmImages;

}

@Override
public int getCount() {
return description.length;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.cardviewlayout,null);
}
//get the textview and set its text
TextView tv=(TextView) convertView.findViewById(R.id.tracktitle);
//get the img view and set its img icon
ImageView im=(ImageView) convertView.findViewById(R.id.trackimage);

tv.setText(description[position]);
im.setImageResource(imageId[position]);

return convertView;
}

这是主要 Activity :

private TextView mSelectedTrackTitle;
private ImageView mSelectedTrackImage;
private MediaPlayer mMediaPlayer;
private ImageView mPlayerControl;
ListView lv_tracks;
TrackAdapter track_adapter;

String[] stations = new String[] {
"GHOTKI 91 Radio FM","UmerKot 91.4 Radio","TMK 100.20 Radio"
};
public static int [] images ={R.drawable.ghotkifmlogo,R.drawable.umerkotfmlogo,R.drawable.tmkfmlogo};



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mMediaPlayer = new MediaPlayer();



//get a reference to our ListView so we can associate it with our custom ArrayAdapter
lv_tracks = (ListView) findViewById(R.id.track_list);
//create a new CustomAdapter
track_adapter =new TrackAdapter(this,stations,images);
lv_tracks.setAdapter(track_adapter);//connect the ListView with myCustomAdapter

//Want to set selected image and title here in these
mSelectedTrackTitle = (TextView)findViewById(R.id.selected_track_title);
mSelectedTrackImage =(ImageView)findViewById(R.id.selected_track_image);
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
??
}

不太明白这里的 onitemselect 是做什么用的。以更改图像和文本。

最佳答案

您可以使用listview OnItemClickListener获取当前项目位置,它将返回点击项目的位置,然后您可以使用该位置作为您想要的操作

   lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mSelectedTrackTitle.setText(stations[position]);
mSelectedTrackImage.setImageDrawable(ContextCompat.getDrawable(this,images [position]));
// or
mSelectedTrackImage.setImageResource(images [position]);
}

关于java - 在另一个 ImageView 和 TextView 中设置 ListView 项的图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52715568/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com