我的代码有问题。当你点击列表中的一个项目时,它应该改变显示在文本旁边的图像。但是,当我点击时没有任何变化。更改图片点击Android列表查看
我在做什么错?
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);}
}
}
谢谢您的解答^^
根据您发布的代码,** **状态1始终是真实的,并** state2 **和** state3 **始终为false。这将导致箭头视图始终设置为R.drawable.icon1。图像的默认图标是什么? – anthonycr
默认图标是icon3 – user3410806
这是您所有活动的代码吗?尝试把print语句放在每个** if if语句中的onListItemClick方法中,看看它们是否被执行过,可能是因为你在代码的其他地方将所有布尔值设置为false。 – anthonycr