2011-06-13 71 views
6

爲了學習目的,我想編寫一個Android應用程序, 將顯示一個從0到Integer.MAX_VALUE的數字列表。我目前有一個 應用程序,它將顯示0到100之間的數字,這很容易,因爲您可以只需 創建一個數字數組,然後將其傳遞給適配器,目前使用 ArrayAdapter。如果我嘗試使用非常大的數組,則程序在使用所有可用內存時會崩潰 。在Android中使用ArrayAdapter和ListView的大型數據集

看着更高級的應用程序,我注意到使用數據庫和CursorAdapter的大數據集 。如果這是正確的事情我可以 開始閱讀。我想做什麼(儘管隨時告訴我 這是錯誤的)是我的ArrayAdapter種子的長度爲100,或一些 相對較小的長度。從用戶向上或向下滾動時,我希望 修改陣列(或適配器)中的值,以隨着用戶向下滾動,刪除列表中的最小項目並將較大的 值附加到列表。如果用戶向上滾動,我想從列表末尾刪除 項目,並在開頭添加新的較小值。

正如我在這個項目中所陳述的,我迄今爲止做得很少。

package example.VariableListView; 

import java.util.ArrayList; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 

public class variablelistview extends ListActivity {  
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

      ArrayList<Integer> intItems = new ArrayList<Integer>(); 
      for (int ii = 0; ii < 100; ii++) { 
        intItems.add(ii); 
      } 
     this.setListAdapter(new ArrayAdapter<Integer>(this, 
          android.R.layout.simple_list_item_1, intItems)); 
    } 

}

先感謝您的任何和所有幫助。

回答

8

您使用的適配器類型應該取決於您嘗試呈現的數據類型。理想情況下,適配器是一個非常薄的對象,可將數據集中的項目綁定到視圖。 ArrayAdapter爲小型有界數據集提供了這個功能,CursorAdapter爲從SQLite查詢產生的數據集提供了此功能。

並非所有的數據集都適合Android框架中提供的適配器類提供的模具,但編寫您自己的模型非常簡單。呈現所有正整數的列表就是一個很好的例子,因爲它根本不需要涉及與底層數據模型的通信。雖然在大型數據集中保留滑動窗口對於某些數據可能是一種有用的方法,但在此不需要。

開始從BaseAdapter:然後

public class IntRangeAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 
    private int mItemResource; 

    public IntRangeAdapter(Context context, int itemLayout) { 
     // We'll use this to generate new item layouts 
     mInflater = LayoutInflater.from(context); 

     // This is the layout resource we'll use for each item 
     mItemResource = itemLayout; 
    } 

    public int getCount() { 
     // Since this adapter presents all positive integers, 
     // we have Integer.MAX_VALUE items. 
     return Integer.MAX_VALUE; 
    } 

    public Object getItem(int position) { 
     // Each item is simply its position index. 
     return position; 
    } 

    public long getItemId(int position) { 
     // Our items won't change and we don't need stable IDs, 
     // so the position of an item is also its ID. 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      // Inflate a new item layout if we weren't given an existing 
      // one to reuse via the convertView parameter. 
      convertView = mInflater.inflate(mItemResource, parent, false); 
     } 

     // Find the TextView where we will label the item. 
     // (This can be optimized a bit for more complex layouts 
     // but we won't bother for this example.) 
     TextView tv = (TextView) convertView.findViewById(android.R.id.text1); 

     // Set the item text based on its position. 
     tv.setText("Item " + position); 

     return convertView; 
    } 
} 

從張貼活動代碼中使用它會改變你的setAdapter呼叫並消除環路來設置數據的問題:

this.setListAdapter(new IntRangeAdapter(this, 
     android.R.layout.simple_list_item_1)); 

如果你想了解更多關於使用ListViews的信息,本次來自Google I/O 2010的演講給出了一個體面的介紹:http://www.youtube.com/watch?v=wDBM6wVEO70

+0

感謝這樣的細節愚蠢和優秀的答案和例子 – user442585 2011-06-13 02:57:24

1

可能最簡單的解決方案是實現您自己的Adapterhttp://developer.android.com/reference/android/widget/Adapter.html),而不是試圖強制CursorAdapter或ArrayAdapter執行某些未設計的操作。

這些方法都應該很容易實現。例如:

int getCount() => Integer.MAX_VALUE 
Object getItem(int position) => position 
相關問題