I am working on an app which combines the SQL and the adapter. The main issue is that once I updated the database, the adapter will not reflect my update directly. Here’s the code:
public class RSSItemActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rssitem);
long channelid = getIntent().getExtras().getLong(RSSReaderApp.CHANNEL_KEY);
DBAdaptor dbAdaptor = ((RSSReaderApp)getApplication()).getDBAdaptor();
Cursor cursor = dbAdaptor.getRssItems(channelid);
adapter = new RSSItemAdapter(this, R.layout.rssitemview, cursor);
setListAdapter(adapter);
}
As you can see, this code get the data from the SQL at the line Cursor cursor = dbAdaptor.getRssItems(channelid);(The get RssItems is just loading data we want to present on the screen from the SQL). However, I have some bottoms in the same activity that if I press that, it will change the values in the SQL, which should affect the data it retrieve. (For example, there’s a column decide what’s these item’s color, it is originally set to white, after I press that, it should be blue.) However, once I press that, seems although the data in the SQL is changed, but the real presentation is still the same color. I had tried using adapter.swapCursor(dbAdapter.getRssItems(id)); on the bottom to update the cursor everytime I press the buttom, but it will let my original display disappear (Possibly because the original adapter loses it’s cursor,so it can display nothing), I have to reload that page to get the correct output. Is there a way that I can instantly update the output? Thanks for your reply in advance =)
You will have to update the cursor in the adapter. You could call the following:
Also, the difference with swapCursor() is that changeCursor, automatically closes the old cursor, while swapCursor does not.