Android中具有ArrayAdapter和ListView的大型数据集

问题描述

|| 出于学习目的,我想编写一个Android应用程序 将显示从0到Integer.MAX_VALUE的数字列表。我目前有一个 应用程序将显示从0到100的数字,这很容易,因为您可以 创建一个数字数组,然后将其传递给适配器,当前使用 ArrayAdapter。如果我尝试使用非常大的阵列,程序将崩溃 当它使用所有可用内存时。 在查看更高级的应用程序时,我注意到拥有大型数据集的人 使用数据库和CursorAdapters。如果那是正确的事情,我可以 开始阅读。我想做的(尽管随时告诉我 这是错误的)是我的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));
    }
} 在此先感谢您提供的所有帮助。     

解决方法

        您使用的适配器类型应取决于您要呈现的数据类型。理想情况下,适配器是一个非常薄的对象,它将数据集中的项目绑定到视图。 ArrayAdapter为小型有限数据集提供此功能,CursorAdapter为SQLite查询生成的数据集提供此功能。 并非所有数据集都适合Android框架中提供的Adapter类提供的模型,但是编写自己的数据很容易。提供所有正整数的列表是一个很好的例子,因为它根本不需要涉及与基础数据模型进行通信。虽然将滑动窗口保持为大数据集可能是一些数据的有用方法,但此处并不需要。 从
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     ,        可能最简单的解决方案是实施自己的
Adapter
(http://developer.android.com/reference/android/widget/Adapter.html),而不是尝试强制CursorAdapter或ArrayAdapter进行并非旨在的操作。 这些方法都应该非常容易实现。例如:
int getCount() => Integer.MAX_VALUE
Object getItem(int position) => position