单击Android Listview更改图像

时间:2014-03-24 17:58:14

标签: android listview onclick

我的代码出了问题。当您单击列表中的项目时,它应该更改文本旁边显示的图像。但是,单击时没有任何变化。

我做错了什么?

public class ChangeImage extends ListActivity {

boolean state1 = true;
boolean state2 = false;
boolean state3 = false;

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String[] values = new String[] {
            "ListItem1", 
            "ListItem2", 
            "ListItem3"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.change_image, R.id.label, values);
    setListAdapter(adapter);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id){
    if(state1){
       ImageView arrowView=(ImageView) findViewById(R.id.labelimage);
       arrowView.setImageResource(R.drawable.icon1);}
   else if(state2){
       ImageView arrowView=(ImageView) findViewById(R.id.labelimage);
       arrowView.setImageResource(R.drawable.icon2);}
   else if(state3){
       ImageView arrowView=(ImageView) findViewById(R.id.labelimage);
       arrowView.setImageResource(R.drawable.icon3);}
   }
}    

感谢您的回答^^

2 个答案:

答案 0 :(得分:0)

使用此功能。

 public void onListItemClick(ListView l, View v, int position, long id){

    switch (position) {
       case 0:
         ImageView imageView=(ImageView) findViewById(R.id.labelimage);
         imageView.setImageResource(R.drawable.icon2);
         break;
       case 0:
         ImageView arrowView=(ImageView) findViewById(R.id.labelimage);
         imageView.setImageResource(R.drawable.icon3);
         break;
       case 0:
         ImageView arrowView=(ImageView) findViewById(R.id.labelimage);
         imageView.setImageResource(R.drawable.icon1);
         break;
    default:
        break;
    }

 }    

答案 1 :(得分:0)

正确设置并重置布尔标志,并使用触摸操作更改图片...

if(state1) { state3=false;state1=false;state2=true; }