问题描述
- setonclicklistener事件中如何改变其他控件的属性
-
public View getView(int arg0, View arg1, ViewGroup arg2) {if(arg1==null){ arg1=layoutInflater.inflate(R.layout.items, null); } ImageView musicflag = (ImageView) arg1.findViewById(R.id.musicflag); TextView musicname=(TextView) arg1.findViewById(R.id.musicname); TextView musicsinger=(TextView) arg1.findViewById(R.id.musicsingers); final ImageView musicplay = (ImageView) arg1.findViewById(R.id.musicatt); musicflag.setImageResource(R.drawable.music); musicname.setText(musics[arg0]); musicsinger.setText(musicsingers[arg0]); musicplay.setImageResource(R.drawable.play); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //Toast.makeText(MainActivity.this, arg2+"", Toast.LENGTH_SHORT).show(); /*if (flag==0) { musicplay.setImageResource(R.drawable.pause); flag=1; }else if (flag==1) { musicplay.setImageResource(R.drawable.play); flag=0; }*/ **musicplay.setImageResource(R.drawable.pause);_** Toast.makeText(MainActivity.this, flag+"", Toast.LENGTH_SHORT).show(); Intent intent = new Intent("MusicPlay"); Bundle bundle = new Bundle(); bundle.putInt("song", arg2); intent.putExtra("int", bundle); sendBroadcast(intent); } });
为什么musicplay.setImageResource(R.drawable.pause);这句话不能执行,图片没有更改
解决方案
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ImageView musicplay = (ImageView) arg1.findViewById(R.id.musicatt);//这里重新获取当前点击的item对应的musicplay,不然musicplay是指向最后一个item的。
//Toast.makeText(MainActivity.this, arg2+"", Toast.LENGTH_SHORT).show();
/*if (flag==0) {
musicplay.setImageResource(R.drawable.pause);
时间: 2025-01-29 18:39:35