2016-02-23 89 views
0

因此,我有一個不同種類的單元格的列表以及應該放入其中的內容。我試圖加載每一個,但我的適配器只會加載這麼多,然後重複這些單元格。這裏是我的適配器類重複單元格的多個單元格類型的列表適配器

public class FormCellAdapter extends ArrayAdapter<Cell> { 

    private final Context context; 

    private static class ViewHolder { 
     // All cells 
     TextView questionLabel; 

     // Checkbox cell 
     LinearLayout checkGroup; 

     // Date cell 
     Button dateButton; 

     // Dropdown cell 
     Spinner spinner; 

     // Slider cell 
     SeekBar seekBar; 
     TextView sliderLabel; 

     // Text area cell 
     EditText textArea; 

     // Dropdown cell, text field cell, text field toggle cell 
     EditText textField; 

     // Text field toggle cell, toggle cell 
     RadioGroup radioGroup; 
    } 

    public FormCellAdapter(Context context, ArrayList<Cell> cells) { 
     super(context, 0, cells); 

     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final Cell cell = getItem(position); 

     final int layout = cell.getLayoutFile(); 
     final CellType type = cell.getCellType(); 

     final ViewHolder viewHolder; 

     if (convertView == null) { 
      viewHolder = new ViewHolder(); 
      convertView = LayoutInflater.from(context).inflate(layout, parent, false); 

      viewHolder.questionLabel = (TextView) convertView.findViewById(R.id.questionTextLabel); 
      viewHolder.questionLabel.setText(cell.getQuestion()); 

      switch (type) { 
       case CHECKBOX: 
        viewHolder.checkGroup = (LinearLayout) convertView.findViewById(R.id.checkGroup); 
        break; 
       case DATE: 
        viewHolder.dateButton = (Button) convertView.findViewById(R.id.dateButton); 
        break; 
       case DROPDOWN: 
        viewHolder.spinner = (Spinner) convertView.findViewById(R.id.spinner); 
        viewHolder.textField = (EditText) convertView.findViewById(R.id.textField); 
        break; 
       case SLIDER: 
        viewHolder.seekBar = (SeekBar) convertView.findViewById(R.id.seekbar); 
        viewHolder.sliderLabel = (TextView) convertView.findViewById(R.id.sliderLabel); 
        break; 
       case TEXTAREA: 
        viewHolder.textArea = (EditText) convertView.findViewById(R.id.textArea); 
        break; 
       case TEXTFIELD: 
        viewHolder.textField = (EditText) convertView.findViewById(R.id.textField); 
        break; 
       case TEXTFIELDTOGGLE: 
        viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup); 
        viewHolder.textField = (EditText) convertView.findViewById(R.id.textField); 
       case TOGGLE: 
        viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup); 
        break; 
      } 

      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     // This will be filled in later, populating the labels and whatever else needs done. 
     switch (type) { 
      case CHECKBOX: 

       break; 
      case DATE: 

       break; 
      case DROPDOWN: 

       break; 
      case SLIDER: 

       break; 
      case TEXTAREA: 

       break; 
      case TEXTFIELD: 

       break; 
      case TEXTFIELDTOGGLE: 

      case TOGGLE: 

       break; 
     } 

     return convertView; 
    } 
} 

我已經修剪了很多代碼,因爲我想我可能剛剛重新開始。在它隨機挑選細胞進入之前,往往會在頂部的細胞在向下滾動然後備份後會有所不同。

任何想法,爲什麼我沒有看到我的所有細胞在這裏,以及他們爲什麼重複自己?

回答

0

重寫getCount()返回要顯示的單元格的數量。

+0

它已經回到了正確數量的細胞,只是沒有正確的單元格。無論如何,這個方法在超級方面得到了照顧。 –

+0

哦,我誤解了你的問題。 –

0

你的問題是你正在使用ViewHolder模式。使用ViewHolder模式的目標是使佈局文件jsut膨脹一次,然後在下一個列表項中重用該膨脹視圖。爲了膨脹不同的佈局文件,每當佈局文件在getView中更改時,都必須對它們進行膨脹。 您getView方法看起來就像這樣:

 // All cells 
     TextView questionLabel; 

     // Checkbox cell 
     LinearLayout checkGroup; 

     // Date cell 
     Button dateButton; 

     // Dropdown cell 
     Spinner spinner; 

     // Slider cell 
     SeekBar seekBar; 
     TextView sliderLabel; 

     // Text area cell 
     EditText textArea; 

     // Dropdown cell, text field cell, text field toggle cell 
     EditText textField; 

     // Text field toggle cell, toggle cell 
     RadioGroup radioGroup; 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final Cell cell = getItem(position); 

    final int layout = cell.getLayoutFile(); 
    final CellType type = cell.getCellType(); 

     View view = LayoutInflater.from(context).inflate(layout, parent, false); 

     questionLabel = (TextView) view.findViewById(R.id.questionTextLabel); 
     questionLabel.setText(cell.getQuestion()); 

     switch (type) { 
      case CHECKBOX: 
       checkGroup = (LinearLayout) view.findViewById(R.id.checkGroup); 
       break; 
      case DATE: 
       dateButton = (Button) view.findViewById(R.id.dateButton); 
       break; 
      case DROPDOWN: 
       spinner = (Spinner) view.findViewById(R.id.spinner); 
       textField = (EditText) view.findViewById(R.id.textField); 
       break; 
      case SLIDER: 
       seekBar = (SeekBar) view.findViewById(R.id.seekbar); 
       sliderLabel = (TextView) view.findViewById(R.id.sliderLabel); 
       break; 
      case TEXTAREA: 
       textArea = (EditText) view.findViewById(R.id.textArea); 
       break; 
      case TEXTFIELD: 
       textField = (EditText) view.findViewById(R.id.textField); 
       break; 
      case TEXTFIELDTOGGLE: 
       radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup); 
       textField = (EditText) view.findViewById(R.id.textField); 
      case TOGGLE: 
       radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup); 
       break; 
     } 

    // This will be filled in later, populating the labels and whatever else needs done. 
    switch (type) { 
     case CHECKBOX: 

      break; 
     case DATE: 

      break; 
     case DROPDOWN: 

      break; 
     case SLIDER: 

      break; 
     case TEXTAREA: 

      break; 
     case TEXTFIELD: 

      break; 
     case TEXTFIELDTOGGLE: 

     case TOGGLE: 

      break; 
    } 

    return view; 
} 
+0

這不會導致內存泄漏?這就是爲什麼我切換到ViewHolder。此外,當使用此方法時,Android會發出警告「視圖適配器無條件通貨膨脹」,並告訴我使用視圖持有者模式。 –

+0

@CodyHarness只是否認這一警告。您無法使用ViewHolder模式達到您的目標。 – Mahfa

+0

那麼這有點解決了我的問題。這些單元格都按照正確的順序出現,並且從不轉移,但是每當我設置任何控件的值時,都會將其移出視線以清除該值,因爲它在視圖重新顯示時會重新生成視圖。有關如何解決這個問題的任何想法? –