Pass hashmap to array adapter in Android - android

I just want to pass a hashmap to array adapter in android. It is not going to be used for display. It is for some manipulation purpose. For display already a arraylist is passed.
Suggest how to just pass hashmap to adapter ?
Thanks in advance.

two possibilities
Add a parameter to the Constructor
create a setter
of course you have to write your own subclass of ArrayAdapter

Related

When should I use BaseAdapter in Android?

I am working on android app in which I am using adapters to populate the data in a listview. I am confused where we should use BaseAdapter. I read many questions where it is written that we should use ArrayAdapter for arrays and arraylist that is ok and CursorAdapter in case of cursor.
I know BaseAdapter is the super class of ArrayAdapter and CursorAdapter. I have checked already this question What is the difference between ArrayAdapter , BaseAdapter and ListAdapter but it don't explain when we should use BaseAdapter.
When should I use BaseAdapter ?
You should use it:
if your model data is not already in a data structure for which there is a concrete ListAdapter class, and
if you determine that creating a custom adapter will be better for the user, or perhaps less development work for you, than would be reorganizing your data structure
For example, suppose that you use JSONArray to parse a snippet of JSON. JSONArray does not implement the List interface, and therefore you cannot use it with ArrayAdapter. None of the other adapters match. Yet, you want to show this JSONArray in an AdapterView. In that case, your choices are:
roll through the data and convert it into an ArrayList, so you can use ArrayAdapter, or
create a custom subclass of BaseAdapter that can adapt a JSONArray (a JSONArrayAdapter)
stop using JSONArray and instead use something else for parsing your JSON, like Gson, which can populate a List directly, allowing you to use ArrayAdapter
If your data is available in a Collection you can go with an ArrayAdapter. (use the addAll method to put your data in the adapter)
If your data is not in a Collection: then you can't and you must find another adapter that suits your needs. (typically: use a CursorAdapter when data comes from a database)
If you can't find any existing adapter working fine with your data structure/data source : you can write a subclass of BaseAdapter to support your own data structure.
If you plan to display your data in a ListView (this is a common use-case): then you must ensure that your adpter also implements ListAdapter (because the ListView needs a ListAdapter).
Note that ArrayAdapter and CursorAdapter implements ListAdapter.

android clear arraylist or adapter?

After working on an app for a while I realize I use
adapter.clear()
and
arraylist.clear()
I can see both are working just fine, I would like to know the difference between the two!
Both are called before I start and asyncTask that updates my list with information from my server!
You should not be clearing the ArrayList directly. The ArrayAdapter makes absolutely no guarantees that it maintains the same referenced list given to it. In fact it will change when you perform a search with it's filter. Which would make arrayList.clear() fail.
Rule of thumb, if you ever need to mutate or retrieve the associating data...do it directly from the adapter. Not the list you used to construct it.
Adapter = it contains copies of diff views,arrays
aaraylist holds the data which we want to display in our view.
ex: arraylist<HashMap<String,String>> ah= new ArrayList<HashMap<String,String>>();
the above list contains hashmap
if i clear the arraylist there will be no data to show on listview or gridview so it will be empty
if i clear adapter than it will destroy the copies of array and views so the output will be same

switch array object for ArrayAdapter dynamically?

I know I can update the array which was initally passed to the arrayAdapter's constructor, and call notifyDataSetChanged.
I wonder if it's possible to plug-in new arraylist into adapter?
simple:
mAdapter.clear();
mAdapter.addAll(collection);
also, do add individual elements you can call mAdapter.add(element); instead of having to keep a reference to the Array that was passed to the constructor.

What Adapter for bean Object?

I'm sending an bean Object from the first to the second View. Other times I use ArrayAdapter when I had an Array with data to show but this time I only have an bean Object that I want to show. What type of Adapter I must to use ?
You do not need to use any adapter, just pass the object itself. You can use the passed object in the activity directly, for setting the contents of the UI controls, for instance. You need adapters when you have a list of items. More information on this is provided on the class documentation of Adapter here: http://developer.android.com/reference/android/widget/Adapter.html

What is the alternative for android:entries in code?

How to I send an String array to a listView from the code and have the emulator display it?
That is a very general question... but the basic idea would be to create one of the usual Adapters, ArrayAdapter possibly in this case, initialize it with the String array and then call the ListView's setAdapter() method with that ArrayAdapter.

Categories

Resources