0

是否可以一次加載適配器中的所有項目?我如何使用光標填充ListView?

我正在使用一個CursorLoader,一個ListView和一個光標適配器,適配器中的項目被加載滾動。 我知道我只有幾個簡單的項目(約10),所以我不會有性能問題

感謝

+0

你的實際問題是什麼? – JoxTraex

+1

如果您已經知道您的列表視圖項目的內容是什麼,請更好地將其設置爲arraylist並使用該列表定義適配器。沒有必要使用遊標 –

+0

@Karan Mer:我需要更改我的應用程序結構,因爲您不知道有效的解決方案?不,謝謝,我的問題是清楚的,我需要什麼,謝謝 – aorlando

回答

1

閱讀本Populating-a-ListView-with-a-CursorAdapter或本Android Cursor Example

如果你想使用ArrayList(從Populating a ListView using an ArrayList getted ):

lv = (ListView) findViewById(R.id.your_list_view_id); 

    // Instanciating an array list (you don't need to do this, 
    // you already have yours). 
    List<String> your_array_list = new ArrayList<String>(); 
    your_array_list.add("foo"); 
    your_array_list.add("bar"); 

    // This is the array adapter, it takes the context of the activity as a 
    // first parameter, the type of list view as a second parameter and your 
    // array as a third parameter. 
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      your_array_list); 

    lv.setAdapter(arrayAdapter); 
+0

謝謝,在第二個鏈接中使用CursorAdapter與我的相同。每次滾動列表視圖時都會調用bindView方法,並且會加載光標中的新數據。我需要的是類似的東西來加載bindView的第一個調用中的所有項目,我不知道ListView或CursorAdapter對象中是否有可用的東西 – aorlando

+0

您可以使用Cursor的數據創建List並使用此列表。我編輯了我的答案。看它。 – MAOL

+0

有用,類似的東西是我的實際解決方案,但這樣我需要掃描光標數據兩次,一個填充列表,另一個掃描將從CursorAdapter執行...我相信我們可以做得更好,並使用只有CursorAdapter掃描:) – aorlando