2013-05-15 149 views
0

我已经使用阵列适配器创建了自定义列表视图。它包含一个Edittext和一些其他的单选按钮等。 我的列表视图显示每4个视图后重复的视图。也就是说,我输入的第一个Edittext也是在第五个EditText中输入的。 我已经读过,为了节省内存,android只创建了有限的视图并重复它。 那么,如何克服这个问题呢?如何使用bindView()方法?和哪里?如何?ListView中的重复项目

ublic类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

把你的代码片段 – Hemant

+0

@HemantVc PLZ看到的问题/ –

回答

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; 
} 
+0

那不工作。 –

+0

看到我的编辑答案。 – Bae

+0

有一个新问题。当我向上或向下滚动列表视图时,EditText中没有文本。我认为将文本保存到EditText不起作用。 –

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或重用一个“老” View

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(); 
+0

谢谢你,但问题没有解决。 –