我的代碼有問題。當你點擊列表中的一個項目時,它應該改變顯示在文本旁邊的圖像。但是,當我點擊時沒有任何變化。更改圖片點擊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