Android中使用ArrayAdapter和ListView的大型数据集

前端之家收集整理的这篇文章主要介绍了Android中使用ArrayAdapter和ListView的大型数据集前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
出于学习目的,我想编写一个 Android应用程序
显示从0到Integer.MAX_VALUE的数字列表.我现在有一个
应用程序将显示从0到100的数字,这很容易,因为你可以
创建一个数字数组,然后将其传递给当前正在使用的适配器
ArrayAdapter.如果我尝试使用非常大的数组,程序会崩溃
当它使用所有可用内存时.

看看更高级的应用程序,我注意到有大量数据集的人
使用数据库和CursorAdapters.如果这是正确的事我可以
开始阅读.我想做什么(虽然随时告诉我
这是错误的)种子我的ArrayAdapter有一个长度为100或一些的数组
相对较小的长度.当用户向上或向下滚动时,我想从那里开始
修改数组(或适配器)中的值以增加用户
向下滚动,删除列表中的最小项并添加更大的项
值到列表的末尾.如果用户向上滚动,我想删除
列表末尾的项目,并在开头添加新的较小值.

正如我在这个项目中所说,到目前为止我做得很少.

  1. package example.VariableListView;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.ListActivity;
  6. import android.os.Bundle;
  7. import android.widget.ArrayAdapter;
  8.  
  9. public class variablelistview extends ListActivity {
  10. @Override
  11. public void onCreate(Bundle icicle) {
  12. super.onCreate(icicle);
  13.  
  14. ArrayList<Integer> intItems = new ArrayList<Integer>();
  15. for (int ii = 0; ii < 100; ii++) {
  16. intItems.add(ii);
  17. }
  18. this.setListAdapter(new ArrayAdapter<Integer>(this,android.R.layout.simple_list_item_1,intItems));
  19. }

}

提前感谢任何和所有的帮助.

解决方法

您使用的适配器类型应取决于您尝试呈现的数据类型.理想情况下,适配器是一个非常瘦的对象,它将数据集中的项绑定到视图. ArrayAdapter为小型有界数据集提供此功能,CursorAdapter为sqlite查询产生的数据集提供此功能.

并非所有数据集都适合Android框架中提供的Adapter类所呈现的模具,但编写自己的模型很容易.呈现所有正整数的列表是一个很好的例子,因为它根本不需要涉及与底层数据模型的通信.虽然将滑动窗口保持为大型数据集对于某些数据来说是一种有用的方法,但这里不需要它.

从BaseAdapter开始:

  1. public class IntRangeAdapter extends BaseAdapter {
  2. private LayoutInflater mInflater;
  3. private int mItemResource;
  4.  
  5. public IntRangeAdapter(Context context,int itemLayout) {
  6. // We'll use this to generate new item layouts
  7. mInflater = LayoutInflater.from(context);
  8.  
  9. // This is the layout resource we'll use for each item
  10. mItemResource = itemLayout;
  11. }
  12.  
  13. public int getCount() {
  14. // Since this adapter presents all positive integers,// we have Integer.MAX_VALUE items.
  15. return Integer.MAX_VALUE;
  16. }
  17.  
  18. public Object getItem(int position) {
  19. // Each item is simply its position index.
  20. return position;
  21. }
  22.  
  23. public long getItemId(int position) {
  24. // Our items won't change and we don't need stable IDs,// so the position of an item is also its ID.
  25. return position;
  26. }
  27.  
  28. public View getView(int position,View convertView,ViewGroup parent) {
  29. if (convertView == null) {
  30. // Inflate a new item layout if we weren't given an existing
  31. // one to reuse via the convertView parameter.
  32. convertView = mInflater.inflate(mItemResource,parent,false);
  33. }
  34.  
  35. // Find the TextView where we will label the item.
  36. // (This can be optimized a bit for more complex layouts
  37. // but we won't bother for this example.)
  38. TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
  39.  
  40. // Set the item text based on its position.
  41. tv.setText("Item " + position);
  42.  
  43. return convertView;
  44. }
  45. }

从您发布的活动代码中使用它将是一个更改您的setAdapter调用删除循环以设置数据的问题:

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

如果您想了解更多关于使用ListViews的信息,Google I / O 2010的这个演讲给出了一个不错的介绍:http://www.youtube.com/watch?v=wDBM6wVEO70

猜你在找的Android相关文章