Android - CursorAdapter-ish Subclass, for ListView - java

I currently use a bunch of subclassed CursorAdapters with custom layouts throughout my app, populating them with a Cursor returned by an SQLite query, then allowing them to populate my ListView, after setting them with
setListAdapter(new SearchAdapter(this, searchCursor));
Is there anyway I can get the same behavior but instead of passing in a Cursor pass in an
ArrayList<String[]>
Would there be a different class to subclass? (Obviously) Or, should I convert the ArrayList to a Cursor somehow? Or, is there a different method I am missing?
Thanks!

you can do it with an array Adapter (also you can subclass Base adapter) and implement your own way of displaying objects in a list. there are plenty of tutorials on google on how to do that.
Just search ArrayAdapter, Base adapter. :D
Hope this helps,
Take care.

Related

Handling empty lists - better to use ListFragment or Fragment+ListView?

I have some doubts. I'm trying to make an application with lists. I made one but with a ListAdapter extends BaseAdapter and ListView.
Then I realized that there is also ListFragment. And with the fragments you can easily display messages "empty list". What I want to do is a list of items and each item associated with this single_item.xml for example. I have seen several examples, but there are differences between them and me is not good either choose from.
Which is the updated method to do this and has the best performance? Are ListFragment better than ListAdapter extends BaseAdapter?
Thanks in advance.
ListFragment essentially provides shortcuts for interacting with a Fragment whose layout contains a single ListView (and nothing else, by default). The ListFragment does not provide any performance advantage, as far as I know. You can inspect the source code here if you would like to know more about how the class functions.
My preference is to avoid ListFragments - I find them inflexible if the layout of a screen ever needs to change, plus they use slightly differently-named APIs that can be somewhat fiddly to remember (onListItemClick, vs the onItemClick callback of a standard AdapterView, for example).
Here is a good article that describes how to handle empty ListViews. You are looking for the setEmptyView(View) method of ListView. This allows you to specify the view to be displayed when the ListView is empty.

Why does a ListView need an adapter assigned to it

I know a list view needs an adapter, but WHY what is the purpose of it, and can it show lists without an adapter ?
An adapter manages the data model and adapts it to the individual rows in the list view.
Filtering and sorting of the data is handled by the adapter.
The notifyDataSetChanged() method on the adapter is called if the data has changed or if new data is available.
The notifyDataSetInvalidated() method is called if the data is not available anymore.
Also see What's the role of adapters in Android?
From the docs:
An Adapter object acts as a bridge between an AdapterView and the
underlying data for that view. The Adapter provides access to the data
items. The Adapter is also responsible for making a View for each item
in the data set.
The ListView needs to know which items to show, and needs to get Views for these items. The Android team chose to implement this using Adapters.
Note that for showing simple lists of Strings, there is the ArrayAdapter class.

Save custom ArrayList on screen orientation change

I have a ListActivity where the ListView is driven by a custom array adapter which has custom objects in it. The custom object is a Site.
On orientation change, my current implementation is trying to hit the database again to populate the ListView and I want to save the current ArrayAdapter then re-attach it to the ListView after the orientation change.
Whilst I wasn't keen on it, I resigned myself to the fact that I would need Site to implement Parcelable so I could then save the entire ArrayAdpter to the bundle as an array of Paracables.
I have looked at this post which does pretty much what I need to do.
However, my Site object has variables within it which are also custom objects and those objects themselves also have other objects such as DateFormat in them. I can't figure out from the post that I linked to above how to implement this so that the objects within the objects that make up part of the Site object can be included in the Parcel, since this example only deals with strings and the parcel doesn't seem to have a writeObject() method!
Are there any alternatives? (I'd love to avoid using Parcelable at all, but if I have to that's ok)
thanks
Aaron
You are using ListActivity, that means your UI is similar in both orientations ?
If yes, you can avoid the condition of recreation of activity on orientation change by adding android:configChanges="orientation|screenSize" in your manifest for this activity.
If not, then you should use a FragmentActivity and make a listview in that, and then you can use onRetainCustomNonConfigurationInstance() to return your adapter object and after configuration changes get it using getLastCustomNonConfigurationInstance()

Populating a custom listview from List, not an ArrayList

I am trying to populate a custom Android ListView from a List. I am using sugar ORM to save and get data from a database. In my Activity I use the following 2 lines to get the data, and put it in a simple listview.
List<Fish> fishList = Select.from(Fish.class).orderBy("species").list();
setListAdapter(new ArrayAdapter<Fish>(this,android.R.layout.simple_list_item_1, fishList));
This works as expected and displays the getString() method from the Fish class.
Every method I have found for populating a custom listview uses ArrayList but fishList is just a List. I have tried probably 4 or 5 tutorials and they all come up short. Most just describe populating a list manually, but not how to do it if I already have the data in a database.
I have the ArrayAdapter classes set up and at one point had it working where it would just load the most recent object into the list. I suspected some kind of loop was needed but couldn't get that working after hours of trying.
Is there a way to populate a custom listview using my List, or do I need to convert it to an ArrayList somehow, and if so, how do I do that for all items in the List?
Can I replace the first line so it gets the data from the database and puts it in an ArrayList right away?
I apologize if this is super simple (I'm sure it is) but I have spent 4 days trying to find a solution before resorting to asking a question here. Thank you so much for any help you can provide.
Found Exactly what I needed here:ListView Populating using Custom Class
In my My onCreate()
adapter = new ListAdapter(this);
listView = getListView();
List<Fish> fishList = Select.from(Fish.class).orderBy("species").list(); //get data from database (SugarORM)
adapter.setData(fishList);
listView.setAdapter(adapter);
Use a BaseAdapter:
http://developer.android.com/reference/android/widget/BaseAdapter.html
Refer to this tutorial:
http://www.vogella.com/tutorials/AndroidListView/article.html

how to know in the main layout that a ListView has been changed?

Suppose that I have a main layout that has a ListView. This ListView has a custom cursor adapter. When my list change, is there anyway to know, in the main activity, if my list has been changed?. Inside my custom cursor I call to this.notifyDataSetChanged();. Is there any method that can I use to know when notifyDataSetChanged() has been called?.
You need to create a DataSetObserver and register it with your adapter by calling registerDataSetObserver().
p.s. I strongly suggest that you familiarize yourself with the Android API Reference. This is a great tool to help you find the classes and methods which are available in the Android API.

Categories