【问题标题】:Android Custom Content ProviderAndroid 自定义内容提供程序
【发布时间】:2016-05-21 15:17:12
【问题描述】:

我正在我的应用中开发自定义内容提供程序。并在从自定义内容提供商获取列表时面临问题。详细信息,我的自定义内容提供程序包含一个表格。我只想获取此表中的所有对象。但它不起作用。这是我的代码,请告诉我我做错了什么?

 public void onAdd(View v) {
    String name = edtname.getText().toString();
    Uri uri = Uri.parse("content://homework.iuh.hh.customcontentprovider.AccountProvider/accounts");
    ContentValues values = new ContentValues();
    values.put("NAME", name);
    getContentResolver().insert(uri, values);
}

public void getList(View v) {
    Uri uri = Uri.parse("content://homework.iuh.hh.customcontentprovider.AccountProvider/accounts");

    Cursor c =  getContentResolver().query(uri,null,null,null,null);
    c.moveToFirst();
    String res=  "";
    while(!c.isAfterLast())
    {
        res += c.getString(0);
        c.moveToNext();
    }
    c.close();
    Log.i(TAG,res);

And it is query method in content provider

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    Log.i(TAG,"query()");
    Cursor c = getContext().getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);
    c.setNotificationUri(getContext().getContentResolver(),uri);
    return c;
}

在 onAdd() 方法中,它工作正常。但是使用getList方法,它显示日志,查询函数被调用的时间很长。并使用消息 E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! 05-21 11:07:43.661 5876-5876/? E/AndroidRuntime: Error reporting crash android.os.TransactionTooLargeException 崩溃应用程序

【问题讨论】:

    标签: android provider


    【解决方案1】:

    尝试将您的代码更改为如下:
    Cursor c = getContentResolver().query(uri,null,null,null,null); String res= ""; while(c.moveToNext()){ res += c.getString(0); } c.close(); Log.i(TAG,res);

    真正的问题是您的 Content Provider 只是在您的查询方法中无休止地循环。您需要在此处实际执行 SQL,而不是调用内容解析器。看看this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      相关资源
      最近更新 更多