我可以使用 Cursor loader 而不是 asynctask 吗?
Can i use Cursor loader instead the asynctask?
我正在研究和了解 Android Java 世界。
我正在构建我的第一个 Android 应用程序,使用 Json 从应用程序获取所有数据,并使用 Java 来显示它。我创建了一个 activity 两个用户之间的聊天,我正在使用 Asynctask 获取和插入数据。
但今天我遇到了一个问题,因为每次用户插入新消息时我都必须更新列表视图,而 Asynctask 不会这样做。我对此进行了更多搜索,一些用户对我说我应该使用 Cursor Loader 而不是 asynctask。
我的问题是,两者之间有什么区别?
每当新数据插入我的数据库时,Cursor Loader 是否会自动重新加载我的 Listview?
我可以使用 Cursor Loader 获取 Json 数据吗?
谢谢。
不幸的是,用一个简单的答案告诉您 CursorLoader 如何工作会让人感到困惑,因为如果您假装使用 Content Provider,它会涉及很多代码,但是 Google material解释它是如何工作的!
如 Android Developers - CursorLoader Tutorial 页所述:
A CursorLoader runs an asynchronous query in the background against a ContentProvider, and returns the results to the Activity or FragmentActivity from which it was called. This allows the Activity or FragmentActivity to continue to interact with the user while the query is ongoing.
此外,在该教程的 second part 中有一个部分可以回答您的第二个问题:
To display Cursor data returned by CursorLoader, use a View class that implements AdapterView and provide the view with an adapter that implements CursorAdapter. The system then automatically moves data from the Cursor to the view.
第三个问题:Java你可以做任何事情! :)
Voggela 也提供了一个很好的教程:Android SQLite database and content provider。
我正在研究和了解 Android Java 世界。
我正在构建我的第一个 Android 应用程序,使用 Json 从应用程序获取所有数据,并使用 Java 来显示它。我创建了一个 activity 两个用户之间的聊天,我正在使用 Asynctask 获取和插入数据。
但今天我遇到了一个问题,因为每次用户插入新消息时我都必须更新列表视图,而 Asynctask 不会这样做。我对此进行了更多搜索,一些用户对我说我应该使用 Cursor Loader 而不是 asynctask。
我的问题是,两者之间有什么区别?
每当新数据插入我的数据库时,Cursor Loader 是否会自动重新加载我的 Listview?
我可以使用 Cursor Loader 获取 Json 数据吗?
谢谢。
不幸的是,用一个简单的答案告诉您 CursorLoader 如何工作会让人感到困惑,因为如果您假装使用 Content Provider,它会涉及很多代码,但是 Google material解释它是如何工作的!
如 Android Developers - CursorLoader Tutorial 页所述:
A CursorLoader runs an asynchronous query in the background against a ContentProvider, and returns the results to the Activity or FragmentActivity from which it was called. This allows the Activity or FragmentActivity to continue to interact with the user while the query is ongoing.
此外,在该教程的 second part 中有一个部分可以回答您的第二个问题:
To display Cursor data returned by CursorLoader, use a View class that implements AdapterView and provide the view with an adapter that implements CursorAdapter. The system then automatically moves data from the Cursor to the view.
第三个问题:Java你可以做任何事情! :)
Voggela 也提供了一个很好的教程:Android SQLite database and content provider。