2012-12-25 136 views
0

我已經通過很多其他答案搜索了相同的問題,但沒有找到任何適合我的解決方案。正如標題所說,問題在於我的自定義適配器中的getView方法沒有被調用。Android:自定義適配器的getView方法不叫

下面的代碼(第一片段):

public class CategoryListFragment extends ListFragment 
          implements NewCategoryDialogListener { 

private GestoreAttivitaDbHelper mDbHelper; 
private CategoryListAdapter mAdapter; 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setHasOptionsMenu(true);   

    mAdapter = new CategoryListAdapter(getActivity()); 
    setListAdapter(mAdapter); 

    CategoryLoader categoryLoader = new CategoryLoader(); 

    if (mDbHelper == null) { 
     mDbHelper = new GestoreAttivitaDbHelper(getActivity().getApplicationContext()); 
    } 

    SQLiteDatabase db = mDbHelper.getReadableDatabase(); 

    mAdapter.addAll(categoryLoader.getAllCategories(db)); 
    mAdapter.notifyDataSetChanged(); 
    mAdapter.getCount(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.category_list, container); 
} 

這裏的適配器:

public class CategoryListAdapter extends ArrayAdapter<CategoryElement> { 

private LayoutInflater mInflater; 

public CategoryListAdapter(Context ctx) { 
    super(ctx, R.layout.category_element); 

    mInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View view; 

    Log.d("Adapter", "Restituisco la view per l'elemento"); 

    if (convertView == null) { 
     view = mInflater.inflate(R.layout.category_element, null); 
    } else { 
     view = convertView; 
    } 

    //((TextView) view.findViewById(R.id.category_element_text)).setText(getItem(position).getName()); 
    TextView textView = (TextView) view.findViewById(R.id.category_element_text); 
    textView.setText(getItem(position).getName()); 

    return view; 
} 

}

而且這裏是我的兩個佈局文件:

和:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/category_element" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/category_element_text" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" />  

</LinearLayout> 

我認爲在OnCreate設置適配器可能是一個問題,因爲它是onCreateView之前調用,並且當時該片段尚未使用ListView相關。所以我將代碼從onCreate移到了onStart方法,但沒有任何改變。

此外,getCount()正確地返回我6,從數據庫中紅色元素的確切數量。

任何幫助將非常感謝! 謝謝。

編輯:
解決了!
問題是在活動中,我有以下代碼:

fragmentTransaction.add(0, categoryListFragment); 

,我在

fragmentTransaction.add(R.id.activity_main, categoryListFragment); 

改變但沒有指定該片段應附視圖ID它從來沒有畫吧!在getView方法

view = mInflater.inflate(R.layout.category_element, null); 


此外,我不得不從這個

view = mInflater.inflate(R.layout.category_element, parent); 

到這種變化。 PS:我編輯這個原因等到晚上8小時過去了,我不能回答我的問題..

+1

您是否發現logcat中與您的適配器相關的任何內容? –

+0

它可能是您的適配器實際上正在加載,但沒有內容被返回來加載您的適配器'TextViews'。也許你可以檢查或記錄'getItem(position)'是否實際返回任何東西。否則,您可以只需要將「items」傳遞給構造函數,並將它們作爲適配器中的字段公開,就像傳統做法一樣。 – mango

+0

我在logcat中找不到任何東西。我也試着在getCount之後放置一個getItem,並且它返回正確的CategoryElement對象... – Maddocche

回答

0

我想在你的R.layout.category_list文件,你需要給ListView控件以下屬性:

android:id="@android:id/list"

ListFragment(和ListActivity)查找此ID以查找ListView,當您調用類似setListAdapter()的方法時。

此外,如果您只想要一個ListView,則不必提供佈局文件。只要不重寫onCreateView(),系統將自動爲您提供ListView。只有當你想要一個自定義的佈局時,你需要對其中一個進行充氣,如果你這樣做,ListView應該有上面指出的ID。

+0

我不這麼認爲。事實上,我在沒有在佈局中指定任何類似的東西的情況下工作。不管怎麼說,還是要謝謝你! – Maddocche

+4

你是怎麼修復它的? – Karakuri

相關問題