I’m making a new Android app that essentially mirrors data available on our website. The GUI will show either a ListView with images and text in each item, or a RelativeLayout that will display details of a single item.
In order to increase responsiveness in this app, I’d like to read data from the internal DB if the data is recent enough, and read data from the server’s API (JSON over http) if the internal data is too old (and then populate the internal DB with the new data).
From the basic tutorials, it seems that one should use the DB and SimpleCursorAdapter (*) when reading from the internal DB. But when reading from the web, I guess I’d be using an ArrayList and ArrayAdapter.
Is there some type of Adapter that can handle both situations?
(*) I know the latest thing is to use LoaderManager with a CursorLoader, but I’m trying to support Android 2.1. I figure I can put the SimpleCursorAdapter into an AsyncTask and avoid ANR.
I think you can write a custom Adapter, extending BaseAdapter class and use it for both cases.
In your class, that extends BaseAdapter, you will have a
List<DataEntry>, where DataEntry is Java POJO class, representing the data coming from web or db (assuming it has the same properties). Assuming you have populated theList<DataEntry>with DataEntry objects, already containing data you can do as follows:1) In the getView() method of the class that extends BaseAdapter, in the inflate, you will use an xml layout, that’s basically represents 1 data row. Assuming you will display data via TextView, the 1 data row layout will have as many TextView elemnts, as the number of data-fields of your DataEntry object. After the inflate, you put values in the TextViews like:
2) in the process where you update the UI in your layout you should have a ListView like:
and after that you initialize your class that extends BaseAdapter
the listOfEntryDataObjects is
List<DataEntry>already populated with data. The ‘this’ in the constructor is the context associated with the current Activity, you make the call from.Structure of class that extends BaseAdapter: