Big dataset with ArrayAdapter and ListView on Android

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

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

As I said for this project, I have not done much 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)); } 

}

in advance for any help.

+6
source share
2 answers

The type of adapter you are using should depend on the type of data you are trying to present. Adapters ideally represent a very thin object that associates elements from your dataset with representations. The ArrayAdapter provides this for small, limited datasets; the CursorAdapter provides this for datasets resulting from an SQLite query.

Not all data sets will fit into the forms presented in adapter classes in the Android platform, but writing them is easy. Listing all positive integers is a good example, because it doesn't need to bind to the underlying data model at all. Although saving a sliding window to a large data set may be a useful approach for some data, it is not needed here.

Start with the 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; } } 

Using it from your published operation code will consist of modifying the setAdapter call and removing the loop to configure the data:

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

If you need more information on using ListViews, this talk from Google I / O 2010 provides a decent introduction: http://www.youtube.com/watch?v=wDBM6wVEO70

+8
source

Probably the easiest solution is to implement your own Adapter ( http://developer.android.com/reference/android/widget/Adapter.html ) instead of trying to get the CursorAdapter or ArrayAdapter to do what it is not intended for.

All methods should be very easy to implement. For instance:

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

Source: https://habr.com/ru/post/890372/


All Articles