我的應用程序有一個調查問卷,這是顯示感謝listView。 用戶可以通過單擊ListItem來通過true或false進行回答。 imageView指示答案爲真。 (每次ListItem被點擊時我改變圖像視圖)Android:爲什麼setImageResource在我的listView/onListItemClick中更改了幾個圖像?
全局它工作正常。我已經能夠動態地更改imageView,但是當我使用setImageResource時,圖像在幾行上發生了變化。 如果我點擊第2行(索引1),圖像在第2行也會在第12行和第22行上發生變化。
目前我一直無法找到解決方案。 如果您對我有任何線索,我將不勝感激。
這裏是我的列表項的佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
>
<ImageView
android:id="@+id/ivCheck"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:contentDescription="selection state"
android:src="@drawable/check_grey" />
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:layout_gravity="center_vertical"
android:text="dummy text"
android:visibility="gone"
/>
<TextView
android:id="@+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/ivCheck"
android:text="dummy text"
android:textIsSelectable="false" />
</RelativeLayout>
這裏是我的方法,改變圖像(在listActivity)
protected void onListItemClick(ListView l, View v, int position, long id) {
ImageView image = (ImageView)v.findViewById(R.id.ivCheck);
QuizAnswer currentQuizAnswer = (QuizAnswer) quizRequest.getListAnswers().get(position);
if(currentQuizAnswer.isSelected()) {
image.setImageResource(R.drawable.check_grey);
} else {
image.setImageResource(R.drawable.check_green);
}
currentQuizAnswer.setSelected(!currentQuizAnswer.isSelected());
}
謝謝你在先進的幫助
您的列表顯示正確?你有什麼機會重複這些項目?也許他們是一樣的觀點,所以這就是爲什麼它是這樣的。 – 2016-03-25 13:34:56