2013-05-15 120 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

謝謝你,但問題沒有解決。 –