Using SimpleCursorAdapter and ArrayAdapter in a CustomAdapter - android

I have a ListView, the first 5 elements of which is to be populated by elements retrieved from an array defined in the strings.xml. These are static and will never change. The rest of the elements will be populated from the SQLite DB. These are dynamic and the user can remove from and add to these.
Is there a way I can implement a custom adapter where I can use a combination of ArrayAdapter and SimpleCursorAdapter.
I tried searching online for such a combination, but could not find anything that could help. Any help is appreciated.

Related

What is the difference between base-, array- and cursor-adapter

Should I use array adapter or base adapter or cursor adapter?
What do you use most? I found some code that uses base adapter for fragments. Can I use array adapter or cursoradapter for listfragments?
I know how to use listview in a simple way like using the android.r.simple. I want to know what I should use in creating a listview that uses listfragment and populating it with data that came from SQLite.
What is the easiest adapter to use to achieve this?
There are difference between the different adapter class. You should decide to use one depending on your model data.
ArrayAdapter is better if you have an ArrayList of objects.
CursorAdapter is better if you have a database query and a Cursor
BaseAdapter is the most customizable, so you can use it for anything (you have to customize it a little bit more than others)
I normally end up extending BaseAdapter. It's simple enough, and ArrayAdapter isn't flexible enough to handle the case where items have multiple values that need to go into different fields in your list item.

Advice with ArrayAdpters

Can anyone tell me what exactly does an array adapter do? I've tried searching the net but all I get is code examples. Please explain me what it does, I've visited the android developers as well.
An ArrayAdapter can be used as a data source for a number of different Android Views, such as a ListView or a Spinner.
Basically, you pass some kind of array or list to the constructor of an ArrayAdapter. Then, the adapter can be hooked up to a ListView by calling setAdapter(). You can also use the add and remove methods of the adapter to modify the underlying list itself.
You can also use an ArrayAdapter to customize the appearance of items in a ListView for example (or other Views) by using the constructor and passing in the resource of a layout to use, or by overriding the getView() method and building it yourself.
Typically, an adapter is some kind of translator. It's the "man in the middle" who know how to dialog with both sides and convert what is said.
An arrayAdapter is a class which get datas from an array and format it for a listview or spinner to understand it. When a listview need the data 4, for example, it will ask the adapter who will return him the 4 element of the array.
Ok, the listview could directly use the array.But with the adapter you're allowed to use any kind of data source. An ArrayAdapter(subclass of adapter) uses an Array, but another adapter could use a database or a file or anything else.That way the listview is able to get datas directly from any source without knowing how to access it.That is the adapter's role.

Creating Multi-Column Layout with ArrayAdapter

I've been trying to fix this problem for a while and have exhausted all the different Google searches that I could think of. I'm a beginner with Android, but understand most of the fundamentals (I think). Anyways, I'm trying to display a list of data in a multi-column layout spreadsheet style. I'm using a ListView for that because I need users to be able to click on a row to get a more detailed look at the data since only so much can fit in a row. I have it working perfectly using a SimpleAdapter, but since the amount of data can sometimes be large, up to 500 entries, I wanted to change over to a Lazy Loader system so users don't have to stare at a black screen for 20 seconds while everything loads. I think I can manage with the Lazy Loader part, but I can't figure out how to switch over from my multi-columned format SimpleAdapter to an ArrayAdapter that all the Lazy Loader examples use.
Here's an example of my SimpleAdapter code:
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
fillMaps is the List of HashMaps that are to be inserted. 'grid_item' is the layout that the entries are inserted into. 'from' is an array of column names (alternatively the keys in the HashMap) to be used. 'to' is an array of TextView items that will be filled in 'grid_item'. Any idea how to convert this to an ArrayAdapter? Or how to Lazy Load with a SimpleAdapter?
Thanks in advance!
To solve this issue for others, here is what I did to get it working perfectly:
Created my own class called Entry which simply contains the strings for the rows
Created a custom adapter that extends ArrayAdapter. For the constructor I pass into it an ArrayList of type Entry.
Create a ViewHolder for better UI efficiency
Inflate the row layout xml which is then added to my listview
To get the lazy loading working:
Added a method in my custom adapter that will append a new ArrayList of Entry objects to the ArrayList that I already have in my adapter
Created an onScrollListener and from some debugging test found that if the top visible item plus the remaining entries equalled the total entries, then I was scrolled all the way to the bottom
Once the bottom was detected, I would call my fetch method to retrieve another 30 entries, add these entries using the add method I created, and then use the notifyDataSetChanged() method for the ArrayAdapter to display the newly loaded items
Just write your own adapter. Create a private a class in your Activity that houses your ListView that extends BaseAdapter. Fill out the abstract methods and display your data however you want to. As useful as SimpleAdapter obviously is, you'll get a lot more traction out of your own adapter, and they're really not difficult to write.

Organize a spinner shown in a listview

How can I organize a spinner alphabetically that has its fields in a listView without manually changing the field data?
I'm not sure I totally understand the question, but here goes.
You can populate the spinner in a similar way that you populate the ListView. That is by using an Adapter. When you create the adapter that you're using for the ListView, make one that you'll use for the spinner.
The type of adapter you use will be based on where the information you're using is coming from.
Does this help?

Confused when to use Adapters and which one?

I have read a little bit about ListAdapter - ArrayAdapter - BaseAdapter - CursorAdapter. I don’t understand there usage in true sense.
I have scenario, in which I am showing word in a TextView and then there are 4 radio button options and user will select one.
I am confused if I can use any adapter functionality. Like when the word on the top is move to next word. Meaning selection shown as radio button options updates automatically because of binding.
In a nut shell i am looking for something like auto binding in .NET.
BaseAdapter is the most basic Adapter of ListView. All remaining adapters extend from BaseAdapter.
If you are confused which adapter is suitable for you scenario, let choose BaseAdapter first.
ArrayAdapter can be used to link say a list of items with an array. The ArrayAdapter works between an your array data and a list item layout to populate a list.
a CursorAdapter can work in a similar way but can link a database query result set (in a cursor) to a list by populating list items.
They are both subclasses of ListAdapter
If data changes in either the database tables or Array both the Array and Cursor Adapters can be refreshed via notifyDataSetChanged()

Categories

Resources