我正在研究琐事应用程序,并在每个问题的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