【发布时间】:2015-02-05 07:03:39
【问题描述】:
我是一个新的安卓学习者。我正在开发一个需要 50 行或更多行的自定义列表视图的项目。每行包含一个指示行号的文本视图和一个具有四个单选按钮的单选组。问题是当我检查任何单选按钮然后向下滚动时,我看到许多单选按钮自动选中。当向上滚动时,我看到一些随机单选按钮被选中!
我猜测它的发生是因为 here 所说的 getView() 方法的回收问题。坦率地说,作为一个新手,我听不懂整个讲座。
我也看过this 线程。这与我的问题相似,但无法清楚地理解并解决我的问题。
那么我该如何解决这个问题并从广播组获取数据。由于我是新手,请简单解释一下。
谢谢!
[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!
这是我的代码:
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>
自定义列表适配器:
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;
}
}
【问题讨论】:
标签: android