通过装载机的Andr​​oid插入记录 [英] Inserting records via Loaders in android

查看:189
本文介绍了通过装载机的Andr​​oid插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题将是一个从一个跟进问<一个href="http://stackoverflow.com/questions/11131058/how-to-properly-insert-values-into-the-sqlite-database-using-contentproviders-i">Content解析器VS光标装载机 如果答案明确规定了如何将记录插入到一​​个sqlite的分贝使用一个内容解析器。我的问题是这样的:

  1. 我可以使用Loader(正常加载)来实现这个功能?

例如:

 公共装载机&LT;光标&GT; onCreateLoader(INT ID,束束){
            开关(ID){

    案例AppConstants.LOADER_ADD_FAV_PHONE_NUM:

        ContentValues​​值=新ContentValues​​();
        values​​.put(DBHelper.TM_DB_COLUMN_PHONE_NUMBER,
                directionOrder);
        values​​.put(TransitMeDBHelper.TM_DB_COLUMN_NAME_ID,selectionArgs两个[0]);
        乌里insertFavStop = getContentResolver()。插入(TransitMeContentProvider.CONTENT_URI_INSERT_FAV_PHONE,
                        值);
        打破;
    }

返回null;
}
 

这会运行在主UI线程或工作线程的功能。我明白,我不会接受回调以此为内容的解析器将返回一个包含数据光标的URI来代替。

我试图运行此code,但它的行为那种怪异的......重复插入记录其不consistient,有人能告诉我一些正确的资源,它使用一个Loader做插入操作(我见过很多例子与查询的例子,它就像一个魅力:))

感谢您的时间。

解决方案
  

我可以使用Loader(正常加载器)来实现此功能?

您不能使用默认的 CursorLoader 插入数据。如果你实现自己的装载机你可以的也许的使用 loadInBackground 方法做插入但是,这将使你的code狼狈最好(我不知道这是否会实际工作)。

  

这会运行在主UI线程或工作者的功能   线 。我明白,我不会接受回调这是   内容解析器将返回光标的一个URI代替   包含的数据。

装载机回调将运行在那里实现(在UI主线程最有可能的),所以你在做的线程上执行 getContentResolver()。将()上主UI线程与阻塞主UI线程的可能性。

  

我试图运行此code,但它的行为那种怪异的......重复   插入记录其不consistient,

你有没有测试过多少次叫 onCreateLoader 的方法?

  

有人能告诉我一些正确的资源,它使用一个Loader做   插入操作(我见过很多例子与查询例子,   它的工作原理就像一个魅力:))

不要使用/尝试使用装载机的ContentProvider /数据库插入的数据。它的工作原理就像一个魅力的查询,因为装载机被设计为从数据源加载数据(未插入数据),这是他们的目的。如果你想插入数据使用的AsyncTask (也有看的 AsyncQueryhandler ),一个普通线程。

This question would be a follow up from the one asked Content Resolver vs Cursor Loader Where the answer clearly states how to insert records into a sqlite Db using a Content Resolver . My question is the following :

  1. Can i use a loader(Normal Loader) to implement this functionality ?

eg :

    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
            switch (id) {

    case AppConstants.LOADER_ADD_FAV_PHONE_NUM:

        ContentValues values = new ContentValues();
        values.put(DBHelper.TM_DB_COLUMN_PHONE_NUMBER,
                directionOrder);
        values.put(TransitMeDBHelper.TM_DB_COLUMN_NAME_ID, selectionArgs[0]);
        Uri insertFavStop =getContentResolver().insert(TransitMeContentProvider.CONTENT_URI_INSERT_FAV_PHONE,
                        values);
        break;
    }

return null;
}

Would this run the functionality in the Main UI thread or a worker Thread . I understand that i would not receive callbacks for this as the content resolver will return a URI instead of the cursor containing data .

I tried running this code but it behaves kind of wierd .. Duplicate inserts are recorded and its not consistient , Could someone show me to some right resource which uses a Loader to do Insert operations ( I have seen many examples with query examples and it works like a charm :) )

Thank you for your time .

解决方案

Can i use a loader(Normal Loader) to implement this functionality ?

You can't use a default CursorLoader to insert data. If you implement your own Loader you could probably use the loadInBackground method to do the insert but that will make your code awkward at best(and I don't know if it will actually work).

Would this run the functionality in the Main UI thread or a worker Thread . I understand that i would not receive callbacks for this as the content resolver will return a URI instead of the cursor containing data .

The loader callbacks will run on the thread where they are implemented(the UI main thread most likely) so you're doing the getContentResolver().insert() on the main UI thread with the possibility of blocking the main UI thread.

I tried running this code but it behaves kind of wierd .. Duplicate inserts are recorded and its not consistient ,

Have you tested how many times is the onCreateLoader method called?

Could someone show me to some right resource which uses a Loader to do Insert operations ( I have seen many examples with query examples and it works like a charm :) )

Don't use/try to use a Loader to insert data in the ContentProvider/Database. It works like a charm for queries because the loaders were designed to load data from a data source(not insert data), that is their purpose. If you want to insert data use an AsyncTask(also have a look at AsyncQueryhandler), a regular thread.

这篇关于通过装载机的Andr​​oid插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆