我有一个这样的图像和字符串数组:
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};
在自定义列表视图中,如何在其他图像视图和texview中替换它们?
我需要每次使用单击项的图像和文本更改该图像视图和文本视图。
这是我的自定义适配器:
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;
}
这里的主要活动:
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。 为了更改图像和文本。
答案 0 :(得分:1)
您可以使用listview OnItemClickListener 获得当前项目的位置,它将返回被单击项目的位置,然后您可以将该位置用作所需的操作
{{1}}
答案 1 :(得分:0)
尝试这样的事情
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedTrackTitle.setText( stations[position] );
mSelectedTrackImage.setImageResource( images[positions] );
}
它将从阵列中单击的位置获取电台名称和图像,并更新视图。
答案 2 :(得分:0)
您可以这样实现:
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedTrackTitle.setText(stations[position]);
mSelectedTrackImage.setImageResource(images[position]);
}