I am a new android learner. I am working on a project which needs custom List View with 50 or more rows. Each row contains a Text View indicating row numbers and a Radio Group which have four Radio Buttons. Problem is when I check any of the radio buttons and then scroll down, I see many radio buttons automatically checked. And when scroll up I see some random radio button are checked!
I am guessing its happening because of that recycle issue of the getView() method said here. Frankly speaking as a novice I couldn't understand the whole lecture.
I have also looked at this thread. Which is similar to my problem but couldn't understand clearly and solve my problem.
So how can I solve this problem and get data from the radio groups. As I am a new learner please explain easily.
Thanks!
[image]Here is my list view with a radio button checked of 1st row.
[image]When I scroll down I see 15th row is auto checked!
[image]When scroll up again I see radio group of the 1st row is in initial state again!
Here is my code:
MainActivity:
public class MainActivity extends Activity {
/* ListView */
ListView lv;
/* Data source for numbering */
String[] numbering = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"};
/* Custom adapter */
CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* initialization */
adapter = new CustomListAdapter(this, R.layout.list_row_item, numbering);
lv = (ListView) findViewById(R.id.lvQuestion);
/* show list view */
lv.setAdapter(adapter);
}
}
list_row_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvNumbering"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/answer0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@drawable/radio_button"
android:checked="true" />
<RadioButton
android:id="@+id/answer1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@drawable/radio_button" />
<RadioButton
android:id="@+id/answer2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@drawable/radio_button" />
<RadioButton
android:id="@+id/answer3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@drawable/radio_button" />
</RadioGroup>
</LinearLayout>
Custom List Adapter:
public class CustomListAdapter extends ArrayAdapter<String> {
Context context;
int layoutResourceId;
String[] numbers;
public CustomListAdapter(Context context, int layoutResourceId,
String[] numbers) {
super(context, layoutResourceId, numbers);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.numbers = numbers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
/* Add layout inflater */
LayoutInflater infletar = ((Activity) context).getLayoutInflater();
convertView = infletar.inflate(layoutResourceId, null);
/* initialization */
holder = new ViewHolder();
holder.tvNumbering = (TextView) convertView
.findViewById(R.id.tvNumbering);
holder.rg = (RadioGroup) convertView.findViewById(R.id.radioGroup);
/* set tag */
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvNumbering.setText(numbers[position]);
return convertView;
}
static class ViewHolder {
TextView tvNumbering;
RadioGroup rg;
}
}