大型数据集ArrayAdapter和ListView在Android中 [英] Large dataset with ArrayAdapter and ListView in Android

查看:100
本文介绍了大型数据集ArrayAdapter和ListView在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关学习的目的,我想编写一个Android应用程序, 将显示数字从0为Integer.MAX_VALUE列表。我现在有一个 应用程序,将0到100的数字显示,这是容易的,因为你可以 创建数字数组,然后使用它传递给适配器,目前 ArrayAdapter。如果我尝试了一个非常大的数组程序崩溃 当它使用所有可用内存。

For learning purposes I would like to write an Android application that will display a list of numbers from 0 to Integer.MAX_VALUE. I currently have an app that will display numbers from 0 to 100, this is easy because you can just create an array of numbers and then pass that to the Adapter, currently using ArrayAdapter. If I try that with an extremely large array the program crashes when it uses all available memory.

看着更高级的应用,我发现人的大数据集 利用数据库​​和CursorAdapters。如果这是正确的事情,我可以 开始读这一点。我想这样做(尽管随时告诉我 这是错误的)是种子我ArrayAdapter长度为100,或者一些数组 相对较小的长度。从那里,当用户滚动向上或向下,我想 修改数组中的值(或适配器),以增加作为用户 向下滚动,在列表中删除最小的项目和附加较大 值,以该列表的末尾。如果用户滚动了起来,我想删除 从列表中的最后项目添加新的较小值的开头。

Looking at more advanced applications I noticed people with large data sets using databases and CursorAdapters. If that is the correct thing to do I can start reading on that. What I would like to do (although feel free to tell me this is wrong) is seed my ArrayAdapter with an array of length 100, or some relatively small length. From there as the user scrolls up or down I would like to modify the values in the array (or the Adapter) to increase as the user scrolls down, removing the smallest items in the list and appending larger values to the end of the list. If the user scrolls up, I would like to remove items from the end of the list and add new smaller values to the beginning.

正如我所说的这个项目我已经很少这样做的远。

As I stated for this project I have very little done so far.

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));
    }

}

在此先感谢任何及所有的帮助。

thanks in advance for any and all help.

推荐答案

您使用应取决于数据你想present类型适配器的类型。适配器理想的结合从数据设置为浏览项目非常薄的对象。 ArrayAdapter提供这对小界的数据集,CursorAdapter的提供这从一个SQLite查询得到的数据集。

The type of Adapter you use should depend on the type of data you're trying to present. Adapters ideally are a very thin object that binds items from your data set to Views. ArrayAdapter provides this for small bounded data sets, CursorAdapter provides this for data sets resulting from a SQLite query.

不是所有的数据集,将适用于由提供的适配器类Android框架psented模具$ P $,不过,自己写是很容易。 presenting所有正整数的列表是一个很好的例子,因为它并不需要涉及到与底层数据模型进行通信的。在保持滑动窗口成一个大的数据集可以是一些数据的有用的方法,它不是必须。

Not all data sets will fit into the molds presented by the provided Adapter classes in the Android framework, but writing your own is easy. Presenting a list of all positive integers is a good example because it doesn't need to involve communicating with an underlying data model at all. While maintaining a sliding window into a large data set can be a useful approach for some data, it's not needed here.

从开始 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;
    }
}

从您发布的活动code使用它,然后将改变你的 setAdapter 通话和消除环路设置数据的问题:

Using it from your posted Activity code would then be a matter of changing your setAdapter call and removing the loop to set up the data:

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

如果你想一些有关使用列表视图的详细信息,这次演讲从谷歌I / O 2010提供了一个体面的介绍: http://www.youtube.com/watch?v=wDBM6wVEO70

If you'd like some more info about using ListViews, this talk from Google I/O 2010 gives a decent intro: http://www.youtube.com/watch?v=wDBM6wVEO70

这篇关于大型数据集ArrayAdapter和ListView在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆