Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get original ArrayList from ArrayAdapter

Tags:

android

How can I retrieve the underlying ArrayList from an ArrayAdapter in Android?

Is this possible or do I need to keep track of them seperately?

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
ArrayList<String> underlying = 
like image 516
jsj Avatar asked May 01 '13 06:05

jsj


People also ask

How do I use ArrayAdapter?

Go to app > res > layout > right-click > New > Layout Resource File and create a new layout file and name this file as item_view. xml and make the root element as a LinearLayout. This will contain a TextView that is used to display the array objects as output.

What is ArrayAdapter and BaseAdapter?

Here is the difference: BaseAdapter is a very generic adapter that allows you to do pretty much whatever you want. However, you have to do a bit more coding yourself to get it working. ArrayAdapter is a more complete implementation that works well for data in arrays or ArrayList s.

How do you use ArrayAdapter in Kotlin?

This example demonstrates how to use ArrayAdapter in android to create a simple listview in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What do you mean by ArrayAdapter in Android?

android.widget.ArrayAdapter<T> You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .


2 Answers

You can do this to get the underlying ArrayList

ArrayList<String> underlying =  = new ArrayList<String>();
for (int i = 0; i < adapter .getCount(); i++)
    underlying .add(adapter .getItem(i));
like image 168
AnujMathur_07 Avatar answered Oct 26 '22 04:10

AnujMathur_07


I think that if you don't have this feature, you can easily create an adapter that fits your needs by extending the BaseAdapter instead .

just send the list to the adapter via its CTOR, and you can change it later from outside of the adapter and call notifyDataSetChanged when you are done.

word of caution though: if you have the original instance of the list of the adapter, you should handle multi-threading issues (or access it only via the UI thread), and if you modify it (especially add/remove items) you should always call notifyDataSetChanged .

like image 23
android developer Avatar answered Oct 26 '22 04:10

android developer