2011-06-07 17 views
3

在我的自定義ListAdapter,第一次GetView()被調用時,convertView在爲NULL通過一個觀點,但第二次它是通過在爲所創建的視圖第一次。我的ListView有4行,並且所有4個同時在屏幕上。從文檔看來,convertView應該是一個已經創建的視圖,現在已經從屏幕上滾動了。我預計convertView將全部爲空,所以它會創建/膨脹4個獨立的視圖。我第一次調用getView後應該有一個convertView嗎?謝謝。ConvertView傳遞作爲這仍然是在屏幕上

在OnCreate中():

Cursor questions = db.loadQuestions(b.getLong("categoryId"), inputLanguage.getLanguageId(), outputLanguage.getLanguageId()); 
    startManagingCursor(questions); 

    ListAdapter adapter = new QuestionsListAdapter(this, questions); 

    ListView list = (ListView)findViewById(R.id.list1); 
    setListAdapter(adapter); 

適配器類

private class QuestionsListAdapter extends BaseAdapter implements ListAdapter{ 

    private Cursor c; 
    private Context context; 

    public QuestionsListAdapter(Context context, Cursor c) { 
     this.c = c; 
     this.context = context; 
    } 

    public Object getItem(int position) { 
     c.moveToPosition(position); 
     return new Question(c); 
    } 

    public long getItemId(int position) { 
     c.moveToPosition(position); 
     return new Question(c).get_id(); 
    } 

    @Override 
    public int getItemViewType(int position) { 

     Question currentQuestion = (Question)this.getItem(position); 
     if (currentQuestion.getType().equalsIgnoreCase("text")) 
      return 0; 
     else if (currentQuestion.getType().equalsIgnoreCase("range")) 
      return 0; 
     else if (currentQuestion.getType().equalsIgnoreCase("yesNo")) 
      return 2; 
     else if (currentQuestion.getType().equalsIgnoreCase("picker")) 
      return 0; 
     else if (currentQuestion.getType().equalsIgnoreCase("command")) 
      return 0; 
     else if (currentQuestion.getType().equalsIgnoreCase("datePicker")) 
      return 0; 
     else if (currentQuestion.getType().equalsIgnoreCase("diagram")) 
      return 0; 
     else 
      return -1; 
    } 

    @Override 
    public int getViewTypeCount() { 
     return 7; 
    } 

    public int getCount() { 
     return c.getCount(); 
    } 

    public View getView(int position, View convertView, ViewGroup viewGroup) { 

     Question currentQuestion = (Question)this.getItem(position); 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.question_row_text, null); 
     } 
     //setup cell 

     return convertView; 
    } 
} 
+0

您對文檔的分析是正確的。你確定所有四個視圖都適合在屏幕上嗎?你能發佈一些代碼/ XML嗎? – 2011-06-07 22:23:11

+0

我的代碼編輯...我還沒有加入,因爲該行爲是明顯的,只要GetView()被調用......所以我不能與我的GetView()的代碼... – GendoIkari 2011-06-07 22:33:10

+0

還,列表基本上正常加載......我在屏幕上完全看到所有4行。 – GendoIkari 2011-06-07 22:34:13

回答

3

如果我記得沒錯,GetView將爲每個那是要顯示的行被調用兩次。我認爲第一組調用是爲了佈局目的而完成的,第二組調用返回將實際顯示的視圖。有意義的是,第二組調用應返回第一組調用中返回的相同視圖。

在任何情況下,你的代碼不應該關心是否它被稱爲佈局或顯示器。在幾乎所有情況下,如果convertView非空,那麼通常應該返回convertView;否則,你應該返回一個新的視圖。

+1

'getView()'通常不會每行顯示超過一次,除非'getView()'中的內容使佈局無效。發生這種情況的一種常見方式是,如果您在具有'layout_width ='wrap_content''的TextView上調用'TextView.setText()',或者TextView的高度將因'setText()' – 2011-06-15 21:13:06

+0

剛剛觀看了鏈接到問題評論中的視頻,這似乎也發生在前3個列表項目的正常場景中。不知道我完全明白,但是。 – 2011-06-15 21:16:37

0

說實話,我不能看到太多你的代碼錯誤。您可能想要嘗試擴展CursorAdapter並重寫一些方法,以便它可以爲您管理光標。

0

快進到2014年和我有同樣的問題...在這個職位的解決方案中沒有爲我工作。當我正在觀看上面的答案和評論中引用的Google I/O視頻時,答案(對於我的特定情況)在19分鐘左右立即變得明顯......

從GETVIEWTYPECOUNT返回的數字必須保留始終貫穿整個適配器的生活!

我已經創建了一個可以動態地擁有任意數量類型的視圖/數據的適配器......並且我的getViewTypeCount()方法返回了當前的視圖類型數量......所以,如果我添加了新的數據類型到適配器的返回值會改變。

使其成爲一個常數固定我的問題。希望這將有助於未來的其他人。

相關問題