我用数组适配器创建了一个自定义列表视图。它包含一个 Edittext 和一些其他单选按钮等。我的列表视图在每 4 个视图之后显示重复视图。即,当我输入第一个 EditText 时,它也输入到了第 5 个 EditText 中。我已经读过,为了节省内存,android 只创建有限的视图并重复它。那么,如何克服这个问题呢?如何使用 bindView() 方法?和在哪里?如何?
公共类 QuestionAdapter 扩展 ArrayAdapter{
Context context;
int layoutResourceId;
Question data[] = null;
public QuestionAdapter(Context context, int layoutResourceId, Question[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final WeatherHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//row = inflater.inflate(R.layout.listview_item_row, null);
holder = new WeatherHolder();
holder.txtquestion = (TextView)row.findViewById(R.id.txtquestion);
holder.radioYes = (RadioButton)row.findViewById(R.id.radioYes);
holder.radioNo= (RadioButton)row.findViewById(R.id.radioNo);
holder.editResponse=(EditText)row.findViewById(R.id.editResponse);
holder.radio_group=(RadioGroup)row.findViewById(R.id.radio_group);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Question question = data[position];
holder.txtquestion.setText(question.question);
holder.radioYes.setChecked(true);
holder.radioYes.setChecked(false);
//holder.editResponse.setText("Edit Response");
return row;
}
static class WeatherHolder
{
TextView txtquestion;
RadioButton radioYes;
RadioButton radioNo;
EditText editResponse;
RadioGroup radio_group;
}
}