加载器优于异步任务有什么优势吗?此外,如何使用Android froyo兼容手机的加载器.
编辑:
这里的主要问题是我没有使用本机DB(SqlLite).在开发服务器上使用DB.显然,我不能再使用CursorLoader了.AsyncTaskLoader没有任何例子.如果有,请做链接.
将所需数据加载到本地数据库然后使用它进行查询是否更好CursorLoader?
android android-asynctask android-cursorloader android-loader
我有一个ContentProvider,它有两个表1. OnlineContacts2 AllContacts.然后我有一个方法,我查询两个表并分别得到他们的结果cursors.然后使用并列出一个列表加入他们.将此列表传递给我,我正在填充我的.喜欢 :CursorJoinerContactsCustomAdapter extending BaseAdapterlistview
public static List<Contact> getContacts(Context context){
List<Contact> contactList = new ArrayList<Contact>();
// Getting First Cursor
String URL = xyz;
Uri baseUri1 = Uri.parse(URL);
String[] select = xyz;
String where =xyz;
Cursor cursor = context.getContentResolver().query(baseUri1, select, where, null, "pid");
// Getting 2nd Cursor
Uri baseUri = xyz;
String[] projection =xyz;
String selection =xyz;
String[] selectionArgs = null;
String sortOrder = xyz;
Cursor mCursor= context.getContentResolver().query(baseUri, …Run Code Online (Sandbox Code Playgroud) 如何GROUP BY为CursorLoader 定义查询?
这两个构造一个CursorLoader我看拿一个单一的Context或Context,Uri,projection,selection,selectionArgs和sortOrder.
但没有groupBy.
(我正在使用Android 2.3设备的支持包)
我正在尝试使用联系人的电子邮件ID.为此,我使用Cursor Loader.有一个问题我也得到重复的电子邮件ID.如何删除电子邮件重复项.我应该使用原始查询"SELECT DISTINCT"而不是使用CursorLoader还是有其他解决方案?
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Email.DATA};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +"='1' AND " + Email.DATA +" IS NOT NULL AND " + Email.DATA +" != \"\" " ;
//showing only visible contacts
String[] selectionArgs = null;
return new CursorLoader(this, ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
}
Run Code Online (Sandbox Code Playgroud) 我有两个1:1关系的表,我正在使用内容提供程序和cursorloader.
如何使用游标加载器进行连接查询?我可以用内容提供程序中的rawSql以某种方式破解它,但是如何在游标加载器构造函数中执行它是超出我的.
非常感谢 !
private static final String CREATE_TABLE_ARTICLES = "create table "
+ TABLE_ARTICLES + "("
+ COLUMN_ARTICLE_ID + " integer primary key autoincrement, "
+ COLUMN_URL + " text not null unique, "
+ COLUMN_TITLE + " text not null, "
+ COLUMN_PRICE + " text not null, "
+ COLUMN_ADDED + " text not null, "
+ COLUMN_IMG_URL + " text);";
private static final String CREATE_TABLE_ARTICLE_DETAIL = "create table "
+ TABLE_ARTICLE_DETAILS + "("
+ COLUMN_ARTICLE_DETAIL_ID …Run Code Online (Sandbox Code Playgroud) 该应用程序包含SQLite数据库中的数据.UI主要是RecyclerView.问题是如何最好地将数据从数据库传输到UI,同时避开主线程?
我原计划使用CursorLoader,ContentProvider和RecyclerView.但阅读它看起来像RecyclerView没有对Cursor提供的数据的开箱即用支持.荡.
那给我留下了一些其他选择......
AsyncTask加载数据,将其放入模型对象,并传递到RecyclerView适配器.除了丑陋之外,它不是配置改变友好的.
一个自定义Loader,它从SQL加载数据并将其推送到模型对象中.
使用Cursor加载器,当它返回Cursor迭代它时将数据推送到模型对象.我怀疑这会发生在主线程上,可能会损害性能.
使用Otto发送请求数据的请求消息,然后通过返回消息返回模型对象集合.可能有大约500个物体,所以我想我可能宁愿滥用奥托这样做.
如果我使用的是模型对象的集合而不是Cursor我看到ContentProvider的好处较少,而且我也失去了UI在数据更改时自动刷新的能力(这可能很有用).
这些选项都没有吸引力,有没有更好的方法?该应用程序在时间压力下,所以无论它需要相当快速实施.不幸的是,UI需要水平滚动并且只针对Lollipop,所以RecyclerView似乎比ListView更好.
android android-contentprovider android-cursorloader android-sqlite android-recyclerview
我需要实现分页列表,使用Loader获得Cursor的ContentProvider.我有页脚,当用户点击它 - 我需要添加下一个记录列表.
如何刷新列表中的数据Loader?或者我该怎么办?
android android-listview android-cursorloader android-cursoradapter
我正在做一个引用应用程序,它从数据库中提取引号,我需要在ViewPager中显示引号.
我创建了我的CursorPagerAdapter似乎运行良好.
public class MyCursorPagerAdapter extends PagerAdapter {
private Cursor cursor;
private LayoutInflater inflater;
public MyCursorPagerAdapter(Context context, Cursor cursor) {
Log.d(MainActivity.TAG, "MyCursorPagerAdapter.onCreate()");
this.cursor = cursor;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void swapCursor(Cursor cursor) {
//if(cursor != null) {
Log.d(MainActivity.TAG, "swapCursor().");
this.cursor = cursor;
//notifyDataSetChanged();
//}
}
/**
* Metóda destroyItem
*/
@Override
public void destroyItem(View view, int position, Object object) {
//cursor.moveToPosition(position);
((ViewPager) view).removeView((LinearLayout) object);
}
/**
* Vráti po?et prvkov
*/
@Override
public int getCount() {
if(cursor == …Run Code Online (Sandbox Code Playgroud) 我使用CursorLoader查询结果,这不是我想在ListFramgenet中显示的顺序.怎么排序呢?
我用它来设置适配器:
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_2, null,
new String[] { "name", "distance"},
new int[] { android.R.id.text1, android.R.id.text2 }, 0);
setListAdapter(mAdapter);
// Start out with a progress indicator.
setListShown(false);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
Run Code Online (Sandbox Code Playgroud)
创建加载器:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(),
Uri.withAppendedPath(TraceTable.CONTENT_URI, "latest"),
MEMBERS_PROJECTION,
null,
null,
null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.changeCursor(data);
// The list should now be …Run Code Online (Sandbox Code Playgroud) 我的应用程序的设置方式是我有一个由CursorLoader维护的ListView,由用户发布的帖子.每个帖子都有与之关联的用户评论.每个listitem底部都有一个自定义文本视图,可以滚动注释(使用向右或向左滑动).在每个自定义文本视图中,是与其特定帖子关联的注释列表.它类似于Google+应用中的评论,但项目是可滚动的.注释存储在与帖子不同的数据库表中.
我遇到的问题是,每次调用BindView时,我都在查询数据库并检索带有相关注释的游标并将其添加到自定义textview列表中.查询每个项目的注释数据库表似乎效率很低.所以我想知道是否有理想的方法来处理这个问题.我的代码看起来像:
public void bindView(View view, Context context, Cursor cursor)
final String rowid = cursor.getString(cursor.getColumnIndexOrThrow(Database.ROWID));
Cursor commentcursor = c.getContentResolver().query(DatabaseProvider.CONTENT_URI_COMMENTTABLE, freeWriteCommentColumns, Database.PARENTPOSTROWID + " =?", new String[]{rowid}, null);
commentcursor.moveToFirst();
while (commentcursor.isAfterLast() == false){
//get comment
//add to comment text view list
}
Run Code Online (Sandbox Code Playgroud)
我已经研究过CursorJoiners,但这似乎并没有用.我玩过JOINS,但这会使行数大于它需要的数量.我正在玩一个注册表游标的持有者,该游标是在创建适配器时创建的并设置为全局变量.这似乎是一个不错的途径,因为我不必每次都重新查询.我只是不确定如何处理这种情况.
sqlite android android-listview android-cursorloader android-cursoradapter