0

我用数组适配器创建了一个自定义列表视图。它包含一个 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;
}

}

4

3 回答 3

0

首先使用 ConvertView 而不是你的 VIEW 这是没用的,你用它们做了什么;

if(convertView == null){
convertView=inflater.inflate....



return convertView;

然后final从 viewHolderObject 中移除修饰符;

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();
于 2013-05-15T11:41:03.163 回答
0

在 getView() 中,您必须手动为每个视图分配值,在您的情况下为每个 EditText。您可以在 getView 中执行 edtitext.setText("") ,当您写入一个编辑文本时,您的列表视图将不会重复显示。

编辑:

您可以尝试有一个结构来保存每个 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;
}
于 2013-05-15T10:59:46.393 回答
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;
}

你要么膨胀一个新View的,要么重用一个 'old' View

于 2013-05-15T11:04:39.813 回答