我正在研究瑣事應用程序,並在每個問題的4個單選按鈕的列表視圖中顯示問題。這是我的XML代碼列表視圖中的RadioGroup問題
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radioButtonLayout">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<RadioButton
android:id="@+id/radioID_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:saveEnabled="true"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radioID_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:saveEnabled="true"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radioID_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:saveEnabled="true"
android:focusableInTouchMode="false"
android:textColor="#000000" />
<RadioButton
android:id="@+id/radioID_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:saveEnabled="true"
android:focusableInTouchMode="false"
android:textColor="#000000" />
</RadioGroup>
</LinearLayout>
這就是我如何實現單選按鈕的選擇。 getView方法在這裏。
@Override
public int getCount() {
// TODO Auto-generated method stub
return mObjects.size();
}
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null) {
view = inflater.inflate(R.layout.question_option, parent, false);
holder = new ViewHolder();
holder.label = (TextView) view.findViewById(R.id.textView1);
holder.radioButtonLayout = (RadioGroup) view.findViewById(R.id.radioGroup);
holder.radioButtonFirst = (RadioButton) view.findViewById(R.id.radioID_1);
holder.radioButtonSecond = (RadioButton) view.findViewById(R.id.radioID_2);
holder.radioButtonThird = (RadioButton) view.findViewById(R.id.radioID_3);
holder.radioButtonFourth = (RadioButton) view.findViewById(R.id.radioID_4);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.radioButtonFirst.setOnCheckedChangeListener(null);
holder.radioButtonSecond.setOnCheckedChangeListener(null);
holder.radioButtonThird.setOnCheckedChangeListener(null);
holder.radioButtonFourth.setOnCheckedChangeListener(null);
holder.radioButtonLayout.setVisibility(View.VISIBLE);
holder.checkboxLayout.setVisibility(View.GONE);
holder.radioButtonFirst.setChecked(firstItemChecked[position]);
holder.radioButtonSecond.setChecked(secondItemChecked[position]);
holder.radioButtonThird.setChecked(thirdItemChecked[position]);
holder.radioButtonFourth.setChecked(fourthItemChecked[position]);
holder.radioButtonFirst.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
firstItemChecked[position] = true;
} else {
firstItemChecked[position] = false;
}
}
});
holder.radioButtonSecond.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
secondItemChecked[position] = true;
} else {
secondItemChecked[position] = false;
}
}
});
holder.radioButtonThird.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
thirdItemChecked[position] = true;
} else {
thirdItemChecked[position] = false;
}
}
});
holder.radioButtonFourth.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
fourthItemChecked[position] = true;
} else {
fourthItemChecked[position] = false;
}
}
});
return view;
}
現在具有應用5個問題,並mObjects.size()是5 getCount將方法。即使getview方法被調用超過5次。爲什麼如此呢?
選擇第一個單選按鈕後,我向上滾動列表視圖,當我滾動查看第一個問題時(我選擇第一個單選按鈕進行回答),失去焦點並顯示爲未選中狀態。並再次嘗試檢查第一個選項,這次無法選擇第一個單選按鈕。一旦我去第二個選項通過點擊第二個單選按鈕並嘗試,第一個工作正常。
在調試,我都會讓firstItemchecked(0)(第一個問題第一個選項)爲真,即使其與未被選中的到來。
這個問題掙扎了4天以上。請幫助我的朋友。 非常感謝。
感謝您的快速回復。是的,我很確定firstItemChecked [0]是真的,一旦我點擊第一個單選按鈕從第一個問題,這裏是每個檢查的數組。真的意味着它被檢查。我查了第一個單選按鈕,它變成了真的。我向下滾動選擇第三個,它被檢查。當我再次滾動選擇第四個提出的問題時,我選擇了第一個單選按鈕,但我無法選擇它,它繼續作爲未選中狀態來到,我將第四個問題的第二個單選按鈕作爲選中狀態,然後返回第一個單選按鈕第四個問題,它工作正常。 – 2012-08-12 08:21:07
當我再次向上滾動時,即使值爲true,firstItemChecked [0]也未選中。不知道發生了什麼事。這裏是選擇第一,第三和第四個問題單選按鈕的數組值。 [真,假,假,假,假] [真,假,真,假,假] [true,false,true,true,false] – 2012-08-12 08:21:47
如何在onclicklistener執行時跟蹤未記錄狀態? – 2012-08-12 08:22:35