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

use*_*585 6 android listview android-arrayadapter

出于学习目的,我想编写一个Android应用程序,它将显示从0到Integer.MAX_VALUE的数字列表.我目前有一个应用程序将显示0到100之间的数字,这很简单,因为你可以创建一个数字数组,然后将其传递给适配器,目前使用ArrayAdapter.如果我尝试使用一个非常大的数组,程序在使用所有可用内存时会崩溃.

查看更高级的应用程序,我注意到人们使用数据库和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));
    }
Run Code Online (Sandbox Code Playgroud)

}

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

ada*_*amp 8

您使用的适配器类型应取决于您尝试呈现的数据类型.理想情况下,适配器是一个非常瘦的对象,它将数据集中的项绑定到视图.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

从您发布的活动代码中使用它将改变您的setAdapter调用并删除循环以设置数据:

this.setListAdapter(new IntRangeAdapter(this,
        android.R.layout.simple_list_item_1));
Run Code Online (Sandbox Code Playgroud)

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