将显示从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中使用ArrayAdapter和ListView的大型数据集全部内容,希望文章能够帮你解决Android中使用ArrayAdapter和ListView的大型数据集所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)