2011-09-12 88 views
15

我在讓我的列表視圖在一個簡單的應用程序,我正在寫展現出來的一些問題。出於某種原因,我的適配器類的getView()方法沒有被調用。但是,當在我的適配器類中調用getCount()時,它不會返回0,而是實際返回適當的值。我很困惑,爲什麼這不起作用。列表視圖沒有得到填充,getView()是沒有得到所謂的

這裏是我的活動:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ListView; 

public class CardListActivity extends Activity { 


protected ListView lv; 
protected int nCards = 12; 

protected String[] cardList = new String[nCards]; 

public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.cardlist); 

    this.loadCardStrings(); 

    lv = (ListView) findViewById(R.id.CardList_ListView); 
    EditorListAdapter adapter = new EditorListAdapter(this, this.cardList); 
    lv.setAdapter(adapter); 
} 

protected void loadCardStrings(){ 
    cardList = getResources().getStringArray(R.array.card_list); 

    Util.logD("Created the string arrays with:" + Integer.toString(cardList.length) + " items."); 
} 
public void onStart(){ 
    super.onStart(); 
} 
public void onResume(){ 
    super.onResume(); 
} 
public void onPause(){ 
    super.onPause(); 
} 
public void onStop(){ 
    super.onStop(); 

} 
public void onDestroy(){ 
    super.onDestroy(); 
} 
} 

這裏是由活動使用的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/CardList_LinearLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TextView android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="Hello"/> 
    <ListView android:id="@+id/CardList_ListView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

最後,這裏是我的適配器類:

import android.app.Activity; 
import android.graphics.Color; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

public class EditorListAdapter extends BaseAdapter 
{ 
    Activity context; 
    String names[]; 

    public EditorListAdapter(Activity context, String[] title) { 
     super(); 
     this.context = context; 
     this.names = title; 
    } 

    public int getCount() { 
     Util.logD("EditorListAdapter.getCount() ret:" + Integer.toString(names.length)); 
     return names.length; 
    } 

    public Object getItem(int position) { 
     Util.logD("EditorListAdapter.getItem()"); 
     return null; 
    } 

    public long getItemId(int position) { 
     Util.logD("EditorListAdapter.getItemId()"); 
     return 0; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     TextView t; 
     if (convertView == null) 
      t = new TextView(parent.getContext()); 
     else 
      t = (TextView) convertView; 

     t.setText(this.names[position]); 

     convertView = t; 

     Util.logD("EditorListAdapter.getView() + " + t.getText().toString()); 

     return convertView; 
    } 

    // Added per K-Ballo suggestion 
    public int getViewTypeCount(){ 
    return 1; 
} 
public int getItemViewType(){ 
    return 0; 
} 
} 
+0

嘗試實現getViewTypeCount並使其返回1;然後getItemViewType應該返回0 –

+0

我加入他們,但沒有效果... – slayton

+0

您是否嘗試實現所有的適配器的方法呢?如果你的代碼有什麼根本性的錯誤,我看不到它。 –

回答

30

試着改變android:layout_height="fill_parent"替換爲wrap_content的文本視圖。我認爲這是從來沒有得到顯示列表視圖,所以從來沒有一個需要調用getView()

+2

+1,我的觀點是隱藏所以'getView()'從來沒有叫。 – Cody

+3

噢,是的,解決我的問題(幾乎1年後),我愛計算器。 – acedanger

+0

該死的。謝謝。 – mtb

4

我知道已經有一個回答這個問題。

但是,我有一個不同的根本原因和與原始問題類似的症狀(沒有調用getView()),因此我認爲在這裏記錄具有不同根本原因的其他人是有益的。

正如http://hissain.in/wordpress/?p=659

給「有時它似乎notifyDataSetChanged()將不會爲你工作(因此getView()將不會被調用)。 原因可能是您的適配器失去對您的列表的引用。當你第一次初始化適配器時,它會引用你的數組列表並傳遞給它的超類。但是,如果你重新初始化現有的數組列表失去參考因此與適配器通信信道」

因此從來沒有重新初始化列表,而是在名單上使用clear()。

list.clear(); // Right Thing to Do 
list = new List<T>(); // Wrong Thing to Do, will lose reference in the adapter