Update ListView based on SQLite backed ContentProvider

邮差的信 提交于 2019-12-11 01:24:32

问题


I'm a new Android Developer and seem to have gotten in a little over my head. I am trying to make a listView update when I add more content to the list.

The ListView is based off of a SQLite database. I was able to get the ListView to be based on the SQLite database by making a ContentProvider for the SQLite database (which was suggested here). Now my issue is that I want to update the SQLite database and have it reflected on the ListView. I am using a loader and according to this if I implemented the loader right it will monitor the data.

I tried updating the SQLite database directly but, that didn't cause the ListView to update without closing and reopening. My instinct from there is that I should implement the insert method in my contentProvider. I did a very simple implementation:

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        return ContentUris.withAppendedId(uri, mCollectionDB.insertCollection(contentValues));
}

Unfortunately, the result of this is my app crashes with a "java.lang.NullPointerException". This is especially confusing as using the exact same contentValues to make the .insertCollection call from my MainActivity works without issue.

The issue I'm really interested in is how to get my listView to update when I insert data into my SQLite database. If inserting into the ContentProvider is irrelevant then please ignore that. I'm not really sure where I went wrong so I'm not sure what other code may be useful, but I'm happy to edit in more code if it'll help.


回答1:


You can check loader concept. You can start with http://www.vogella.com/articles/AndroidSQLite/article.html#todo

Following are other 2 good tutorials http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/

http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/




回答2:


Hey here is one example to insert data into sqlite database and display it in list view. Have a look at it

The idea is simple insert data into database and on click of view button initialize List view with arraylist that contains data already inserted.

You can ask if you have any further queries.



来源:https://stackoverflow.com/questions/20185273/update-listview-based-on-sqlite-backed-contentprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!