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 =
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.
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.
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.
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 .
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));
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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With