我已经使用数组适配器创建了自定义列表视图。它包含一个Edittext和一些其他的radiobuttons等。 我的列表视图显示每4个视图后重复的视图。即我输入第一个Edittext,它也输入了第五个EditText。 我已经读过,为了节省内存,android只创建有限的视图并重复它。 那么,如何克服这个问题呢?如何使用bindView()方法?在哪里?怎么样?
ublic class 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;
}
}
答案 0 :(得分:0)
在getView()中,您必须为每个ViewText手动分配每个视图的值。您可以在getView中执行edtitext.setText(“”),当您在一个edittext中编写时,您的列表视图将不会显示重复。
编辑:
您可以尝试使用一种结构来保存每个EditText中的文本,并在getView()内部将这些文本分配给其EditText
String [] text ;
Question data[] = null;
public QuestionAdapter(Context context, int layoutResourceId, Question[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
text = new String[data.length];
for(String s: text)
s="";
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final WeatherHolder holder;
final pos = position;
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);
//Add a listener and you'll know the text inside EditText
holder.editResponse.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
//Save text after typed it
text[pos] = s.toString();
}
});
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);
//Assign text
holder.editResponse.setText(text[position]);
return row;
}
答案 1 :(得分:0)
你的getView(...)实现如何?
它应该是这样的:
@Override
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
if(view == null)
view = inflate(view, parent);
MyObject obj = objects.get(position);
if(obj != null)
view = setUIControls(position, view, obj);
return view;
}
您要么全新{{1}},要么重复使用'旧'View
。
答案 2 :(得分:0)
首先使用ConvertView而不是VIEW 这是没用的,你用它们做了什么;
if(convertView == null){
convertView=inflater.inflate....
return convertView;
然后从viewHolderObject中删除final
修饰符;
Question data[] = null;// what the hell? if you want an array....
Question[] data=null;
并让您的观看者共享
class YourClass..{
private WeatherHolder holder;
以及converView==null
if(convertView==null){
....
holder=new WeatherHolder();