【问题标题】:Large dataset with ArrayAdapter and ListView in AndroidAndroid 中带有 ArrayAdapter 和 ListView 的大型数据集
【发布时间】:2011-06-13 01:45:58
【问题描述】:

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

查看更高级的应用程序时,我注意到拥有大量数据集的人 使用数据库和 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));
    }

}

提前感谢所有帮助。

【问题讨论】:

    标签: android listview android-arrayadapter


    【解决方案1】:

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

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

    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

    【讨论】:

      【解决方案2】:

      可能最简单的解决方案是实现您自己的 Adapter (http://developer.android.com/reference/android/widget/Adapter.html),而不是试图强制 CursorAdapter 或 ArrayAdapter 做一些它不适合做的事情。

      这些方法都应该很容易实现。例如:

      int getCount() => Integer.MAX_VALUE
      Object getItem(int position) => position
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-09
        相关资源
        最近更新 更多