问题
I have a listview with three Radio Buttons in a list item, Issue is that when I scroll list view, the radio buttons selected position gets changed. So please let me know how to keep radio button's selection intact even if I scroll list view. My code is,
RadioGroupAdapter.java
public RadioGroupAdapter(Context context, int layoutResourceId,
Option[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MatrixHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MatrixHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.heading);
holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
final RadioButton[] rb = new RadioButton[2];
for(int i=0; i<2; i++){
rb[i] = new RadioButton(context);
// rb[i].setButtonDrawable(R.drawable.single_radio_chice);
rb[i].setId(i);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight=1.0f;
params.setMargins(5, 0, 5, 10);
holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
}
// ((MatrixHolder)holder).group.clearCheck();
row.setTag(holder);
} else {
holder = (MatrixHolder) row.getTag();
}
Option option = data[position];
holder.txtTitle.setText(option.title);
return row;
}
回答1:
Solved my problem by adding two different functions: I know its almost a hack but it works. So never mind ;)
@Override
public int getViewTypeCount() {
//Count=Size of ArrayList.
return data.length;
}
@Override
public int getItemViewType(int position) {
return position;
}
回答2:
Try out as below:
Check out Listview with RadioButtons It may help you to solve your issue.
回答3:
You should use Map<Integer, <Value>>
then put your checked radio items in it. Key is listview's item positions. Value; your radio button view value.
回答4:
In my case my listview
Items contained a radiogroup
with the 3 radioButtons
and this is how I solved the problem
First, I declared 3 maps for my 3 rows (Containing radio buttons 1, 2, 3)
private static Map<Integer, Boolean> firstRow = new HashMap<Integer, Boolean>();
private static Map<Integer, Boolean> secondRow = new HashMap<Integer, Boolean>();
private static Map<Integer, Boolean> thirdRow = new HashMap<Integer, Boolean>();
Radio Group onCheckedCangedListener:
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId) {
case R.id.opt1:
if(!firstRow.containsKey(pstn)){
firstRow.put(pstn, true);
secondRow.remove(pstn);
thirdRow.remove(pstn);
}
break;
case R.id.opt2:
if(!secondRow.containsKey(pstn)){
secondRow.put(pstn, true);
firstRow.remove(pstn);
thirdRow.remove(pstn);
}
break;
case R.id.opt3:
if(!thirdRow.containsKey(pstn)){
thirdRow.put(pstn, true);
secondRow.remove(pstn);
firstRow.remove(pstn);
}
break;
}
}
});
Finally, Updating the Radio buttons checked state:
if(firstRow.containsKey(position)){
r1.setChecked(firstRow.get(position));
}
if(secondRow.containsKey(position)){
r2.setChecked(secondRow.get(position));
}
if(thirdRow.containsKey(position)){
r3.setChecked(thirdRow.get(position));
}
来源:https://stackoverflow.com/questions/19087096/android-radio-buttons-keep-changing-their-state-when-list-view-scrolled