0
我正面臨着一個非常奇怪的行爲,我在自定義適配器中的複選框及其標籤上設置了onClickListener。 問題是,當我檢查第一個問題的答案時,也檢查第三個問題上的同一個項目。複選框似乎是同步的,但我不明白爲什麼。setOnClickListener getView不良行爲
以下是一些需要了解的屏幕。
這裏是我的適配器getView():
public View getView(final int position, View convertView, ViewGroup parent)
{
View vi = convertView;
if(convertView == null)
{
vi = inflater.inflate(R.layout.question_item, null);
}
Qcm qcm = qcms.get(position);
List<Proposition> propositions = qcm.getPropositionsList();
TextView numQuestion = (TextView)vi.findViewById(R.id.question_number_item);
TextView question = (TextView)vi.findViewById(R.id.question_text);
LinearLayout[] labels = new LinearLayout[5];
labels[0] = (LinearLayout)vi.findViewById(R.id.question_label1);
labels[1] = (LinearLayout)vi.findViewById(R.id.question_label2);
labels[2] = (LinearLayout)vi.findViewById(R.id.question_label3);
labels[3] = (LinearLayout)vi.findViewById(R.id.question_label4);
labels[4] = (LinearLayout)vi.findViewById(R.id.question_label5);
final CheckBox[] checkBoxes = new CheckBox[5];
checkBoxes[0] = (CheckBox)vi.findViewById(R.id.question_check1);
checkBoxes[1] = (CheckBox)vi.findViewById(R.id.question_check2);
checkBoxes[2] = (CheckBox)vi.findViewById(R.id.question_check3);
checkBoxes[3] = (CheckBox)vi.findViewById(R.id.question_check4);
checkBoxes[4] = (CheckBox)vi.findViewById(R.id.question_check5);
numQuestion.setText(String.valueOf(position+1));
question.setText(Html.fromHtml(qcm.getQuestion()));
for(int i=0; i<5; i++)
{
final int cpt = i;
checkBoxes[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Checked[i] -> !Checked[i]
QcmListAdapter.this.checked[position][cpt] = !QcmListAdapter.this.checked[position][cpt];
APIHelper.log('d', "position="+position);
// TODO : Faire la methode PlaySerieFragment.saveSession();
//QcmListAdapter.this.activity.getFragmentManager().getFragment(QcmListAdapter.this.);
}
});
labels[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkBoxes[cpt].performClick();
APIHelper.log('d', "onClick:cpt="+cpt);
}
});
Proposition p = propositions.get(i);
p.setFormattedView(this.activity, labels[i], (i + 1));
}
return vi;
}
,這裏是一個項目的觀點:我懷疑這
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/question_block"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<RelativeLayout android:id="@+id/question" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="@+id/question_number_item"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:text="X"
android:textSize="30sp"
android:textColor="@android:color/white"
android:gravity="center"
android:background="@drawable/question_number" />
<TextView
android:id="@+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Question 1 : Texte descriptif de la question tellement long"
android:textSize="16sp"
android:textColor="@color/MPQ_blue"
android:layout_toRightOf="@id/question_number_item"/>
</RelativeLayout>
<RelativeLayout android:id="@+id/question_item1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/question" android:layout_marginTop="16dp">
<CheckBox android:id="@+id/question_check1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="@drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="@+id/question_label1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/question_check1"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="@+id/question_item2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/question_item1" android:layout_marginTop="8dp">
<CheckBox android:id="@+id/question_check2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="@drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="@+id/question_label2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/question_check2"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="@+id/question_item3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/question_item2" android:layout_marginTop="8dp">
<CheckBox android:id="@+id/question_check3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="@drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="@+id/question_label3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/question_check3"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="@+id/question_item4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/question_item3" android:layout_marginTop="8dp">
<CheckBox android:id="@+id/question_check4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="@drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="@+id/question_label4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/question_check4"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="@+id/question_item5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/question_item4" android:layout_marginTop="8dp">
<CheckBox android:id="@+id/question_check5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="@drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="@+id/question_label5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@id/question_check5"></LinearLayout>
</RelativeLayout>
</RelativeLayout>
有趣的是,我沒有想到它是這樣重複使用的。 有沒有什麼辦法強制重新創建視圖的getView()中的任何發生? 我的意思是,刪除if語句只是給我一個崩潰,因爲convertView爲null。 – 2014-11-06 14:50:43
對不起,我的錯是一個不好的解釋。刪除if,但不包含其內容。 所以你只需要這一行: vi = inflater.inflate(R.layout.question_item,null); – James 2014-11-06 15:13:58
這是我首先想到的,但每次滾動時都會重新創建視圖,例如,創建的每個視圖上的複選框狀態都不是持久的。 – 2014-11-06 15:21:00