Populate a ListAdapter or SimpleCursorAdapter

I am creating a new Android application that essentially reflects the data available on our website. In the graphical interface, either a ListView with images and text in each element will be displayed, or RelativeLayout , in which the details of one element will be displayed.

To increase responsiveness in this application, I would like to read data from the internal database if the data is fresh enough, and read data from the server API (JSON via http) if the internal data is too old (and then fill the internal database with new data).

From the main manuals, it seems that when reading from the internal database, you should use the database and SimpleCursorAdapter (*). But when reading from the Internet, I assume I am using ArrayList and ArrayAdapter .

Is there some kind of adapter that can handle both situations?

(*) I know that the most recent is to use the LoaderManager with CursorLoader, but I'm trying to support Android 2.1. I believe I can put a SimpleCursorAdapter in AsyncTask and avoid ANR.

+4
source share
2 answers

I think you can write a custom adapter extending the BaseAdapter class and use it for both cases.

In your class that extends the BaseAdapter, you will have a List<DataEntry> , where a DataEntry is a Java POJO class representing data coming from the Internet or db (assuming it has the same properties). Assuming you populate List<DataEntry> objects with DataEntry objects that already contain data, you can do the following:

1) In the getView () method of the class that extends the BaseAdapter, in the inflat you will use the xml layout, which basically represents 1 row of data. Assuming that you will display data through a TextView, in 1 data row layout there will be as many TextView text elements as the number of data fields of your DataEntry. After bloating, you add values ​​to TextViews, for example:

  TextView someTextViewToDisplayField = (TextView) convertView.findViewById(R.id.yourID); someTextViewToDisplayField.setText(String.valueOf(dataEntry.getWhateverProperty())); 

2) in the process of updating the user interface in the layout, you should have a ListView, for example:

  <ListView android:id="@+id/YourListViewID" android:layout_width="fill_parent" android:layout_height="wrap_content"></ListView> 

after which you initialize your class that extends BaseAdapter

  ListView listView = (ListView) findViewById(R.id.YourListViewID); YourClassExtendingBaseAdapter adapter = new YourClassExtendingBaseAdapter(this, listOfEntryDataObjects); listView.setAdapter(adapter); 

listOfEntryDataObjects List<DataEntry> already populated with data. 'this' in the constructor is the context associated with the current Activity, you are calling.

Class structure extending BaseAdapter:

 public class YourClassExtendingBaseAdapter extends BaseAdapter { private Context context; private List<DataEntry> entries; public YourClassExtendingBaseAdapter(Context context, List<DataEntry> entries) { this.context = context; this.entries = entries; } // Overwriting necessary methods } 
+2
source

Of course you can use SimpleCursorAdapter and AsyncTask . But with the Android Compatibility Package, you can start using the CursorLoader API with Android 1.6. The Loader API simplifies Cursors lifecycle management.

I would suggest using the CursorLoader API to fetch Cursor from a ContentProvider or database. And use AsyncTask (or a custom implementation of AsyncLoader) to retrieve data from the server and update the ContentProvider .

If you use two different AsyncTasks (or Loaders ) for each task (loading the cursor and updating the data), you can do your ListView update automatically if the data in db has changed.

To do this, set the Uri notification to Cursor that you extracted from db:

 cursor.setNotificationUri(getContentResplver(), Uri.parseString("mydata://someuri")); 

And if you updated the data, send a notification for this Uri :

 getContentResolver().notifyChange(Uri.parseString("mydata://someuri"), null); 
+3
source

Source: https://habr.com/ru/post/1380150/


All Articles