基于SQLite支持的ContentProvider更新ListView [英] Update ListView based on SQLite backed ContentProvider
问题描述
我是一名新的Android开发人员,似乎有点不知所措。当我向列表中添加更多内容时,我试图进行listView更新。
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.
ListView基于SQLite数据库。通过为SQLite数据库创建ContentProvider,我能够使ListView基于SQLite数据库(建议此处)。现在我的问题是我想更新SQLite数据库并将其反映在ListView上。我使用的是加载程序,并且如果我实现了加载程序,则根据此
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.
我尝试直接更新SQLite数据库,但这并没有导致ListView在不关闭和重新打开的情况下进行更新。我的直觉是,我应该在我的contentProvider中实现insert方法。我做了一个非常简单的实现:
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));
}
不幸的是,其结果是我的应用程序崩溃了,并带有 java.lang 。空指针异常。这尤其令人困惑,因为使用完全相同的contentValues可以从MainActivity进行.insertCollection调用而没有问题。
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.
我真正感兴趣的问题是如何获取当我将数据插入SQLite数据库时,我的listView会更新。如果插入到ContentProvider中无关紧要,请忽略它。我不太确定哪里出了问题,所以不确定其他什么代码可能有用,但是如果有帮助,我很乐意编辑更多代码。
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.
推荐答案
您可以检查加载程序的概念。
您可以从
开始 http://www.vogella .com / articles / AndroidSQLite / article.html#todo
You can check loader concept. You can start with http://www.vogella.com/articles/AndroidSQLite/article.html#todo
以下是其他2篇不错的教程
http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/
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/
这篇关于基于SQLite支持的ContentProvider更新ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!