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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...