我做了简单的测试应用程序基于此示例。有一个按钮,插入到数据库和列表视图的数据。都是在 MainActivity 中。在原来的代码是restartLoader() 仅从调用 onResume() ,但它刷新列表视图时才 onResume() 被执行死刑。我把 restartLoader() 在结束了displayListView() 和现在它显示新行在列表视图中后我按下按钮。但我不认为它是正确的解决方案。

 public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>{

      private SimpleCursorAdapter dataAdapter;

      @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); displayListView(); Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ContentValues values = new ContentValues();
values.put(SensorsDb.KEY_TYPE, "wld");
values.put(SensorsDb.KEY_TITLE, "Basement Water Detector");
values.put(SensorsDb.KEY_SERIAL, "");
values.put(SensorsDb.KEY_VALUE, "NO WATER"); getContentResolver().insert(MyContentProvider.CONTENT_URI,values); displayListView();
}
});
}
@Override
protected void onResume() {
super.onResume();
//Starts a new or restarts an existing Loader in this manager
getSupportLoaderManager().restartLoader(, null, MainActivity.this);
} private void displayListView() {
// The desired columns to be bound
String[] columns = new String[] {
SensorsDb.KEY_TITLE,
SensorsDb.KEY_VALUE
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.sensorTitle,
R.id.sensorState
}; // create an adapter from the SimpleCursorAdapter
dataAdapter = new SimpleCursorAdapter(this, R.layout.custom_row_view, null, columns, to, );
//Ensures a loader is initialized and active.
getSupportLoaderManager().initLoader(, null, this);
// get reference to the ListView
ListView listView = (ListView) findViewById(R.id.sensorList);
// Assign adapter to ListView
listView.setAdapter(dataAdapter); getSupportLoaderManager().restartLoader(, null, MainActivity.this);
} // This is called when a new Loader needs to be created.
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {
SensorsDb.KEY_ROWID,
SensorsDb.KEY_TYPE,
SensorsDb.KEY_TITLE,
SensorsDb.KEY_SERIAL,
SensorsDb.KEY_VALUE};
CursorLoader cursorLoader = new CursorLoader(this,
MyContentProvider.CONTENT_URI, projection, null, null, null);
return cursorLoader;
} @Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { dataAdapter.swapCursor(data);
} @Override
public void onLoaderReset(Loader<Cursor> loader) { dataAdapter.swapCursor(null);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
有 MyContentProvider 类 public class MyContentProvider extends ContentProvider{ private MyDatabaseHelper dbHelper; private static final int ALL_SENSORS = ;
private static final int SINGLE_SENSOR = ; // authority is the symbolic name of your provider
// To avoid conflicts with other providers, you should use
// Internet domain ownership (in reverse) as the basis of your provider authority.
private static final String AUTHORITY = "com.example.contproctest.contentprovider"; // create content URIs from the authority by appending path to database table
public static final Uri CONTENT_URI =
Uri.parse("content://" + AUTHORITY + "/sensors"); // a content URI pattern matches content URIs using wildcard characters:
// *: Matches a string of any valid characters of any length.
// #: Matches a string of numeric characters of any length.
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "sensors", ALL_SENSORS);
uriMatcher.addURI(AUTHORITY, "sensors/#", SINGLE_SENSOR);
} // system calls onCreate() when it starts up the provider.
@Override
public boolean onCreate() {
// get access to the database helper
dbHelper = new MyDatabaseHelper(getContext());
return false;
} //Return the MIME type corresponding to a content URI
@Override
public String getType(Uri uri) { switch (uriMatcher.match(uri)) {
case ALL_SENSORS:
return "vnd.android.cursor.dir/vnd.com.example.contproctest.contentprovider.sensors";
case SINGLE_SENSOR:
return "vnd.android.cursor.item/vnd.com.example.contproctest.contentprovider.sensors";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
} // The insert() method adds a new row to the appropriate table, using the values
// in the ContentValues argument. If a column name is not in the ContentValues argument,
// you may want to provide a default value for it either in your provider code or in
// your database schema.
@Override
public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = dbHelper.getWritableDatabase();
switch (uriMatcher.match(uri)) {
case ALL_SENSORS:
//do nothing
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
long id = db.insert(SensorsDb.SQLITE_TABLE, null, values);
getContext().getContentResolver().notifyChange(uri, null);
return Uri.parse(CONTENT_URI + "/" + id);
} // The query() method must return a Cursor object, or if it fails,
// throw an Exception. If you are using an SQLite database as your data storage,
// you can simply return the Cursor returned by one of the query() methods of the
// SQLiteDatabase class. If the query does not match any rows, you should return a
// Cursor instance whose getCount() method returns 0. You should return null only
// if an internal error occurred during the query process.
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(SensorsDb.SQLITE_TABLE); switch (uriMatcher.match(uri)) {
case ALL_SENSORS:
//do nothing
break;
case SINGLE_SENSOR:
String id = uri.getPathSegments().get();
queryBuilder.appendWhere(SensorsDb.KEY_ROWID + "=" + id);
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
} Cursor cursor = queryBuilder.query(db, projection, selection,
selectionArgs, null, null, sortOrder);
return cursor; } // The delete() method deletes rows based on the seletion or if an id is
// provided then it deleted a single row. The methods returns the numbers
// of records delete from the database. If you choose not to delete the data
// physically then just update a flag here.
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbHelper.getWritableDatabase();
switch (uriMatcher.match(uri)) {
case ALL_SENSORS:
//do nothing
break;
case SINGLE_SENSOR:
String id = uri.getPathSegments().get();
selection = SensorsDb.KEY_ROWID + "=" + id
+ (!TextUtils.isEmpty(selection) ?
" AND (" + selection + ')' : "");
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
int deleteCount = db.delete(SensorsDb.SQLITE_TABLE, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return deleteCount;
} // The update method() is same as delete() which updates multiple rows
// based on the selection or a single row if the row id is provided. The
// update method returns the number of updated rows.
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
switch (uriMatcher.match(uri)) {
case ALL_SENSORS:
//do nothing
break;
case SINGLE_SENSOR:
String id = uri.getPathSegments().get();
selection = SensorsDb.KEY_ROWID + "=" + id
+ (!TextUtils.isEmpty(selection) ?
" AND (" + selection + ')' : "");
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
int updateCount = db.update(SensorsDb.SQLITE_TABLE, values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return updateCount;
} }
解决方法 :
你不需要调用 restartLoader 后在 CP 中插入新的数据,因为 CursorLoader 可以听您的数据和 (ContentProvider) 的数据源中发生更改时自动更新。尝试在调用 cursor.setNotificationUri() 之前返回 Cursor 从你 CP 的查询方法。 @Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbHelper.getWritableDatabase();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(SensorsDb.SQLITE_TABLE); switch (uriMatcher.match(uri)) {
case ALL_SENSORS:
//do nothing
break;
case SINGLE_SENSOR:
String id = uri.getPathSegments().get();
queryBuilder.appendWhere(SensorsDb.KEY_ROWID + "=" + id);
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
} Cursor cursor = queryBuilder.query(db, projection, selection,
selectionArgs, null, null, sortOrder); cursor.setNotificationUri(getContext().getContentResolver(), uri); return cursor;
}

Android 用SQLite 使用 CursorLoader 中的数据填充列表视图的更多相关文章

  1. sqlite 删除表中重复数据(亲测可用)

    例子:表名  Paper .通过字段PaperID查找重复数据. 1 --查询某表中重复的数据       select * from Paper group by PaperID having co ...

  2. Android之读取 AndroidManifest.xml 中的数据

    转:http://www.2cto.com/kf/201208/151123.html 下来示例如何读取这些数据. 1 版本信息.应用名称 2 Appliction 的Meta-data 3 Acti ...

  3. Android之读取 AndroidManifest.xml 中的数据:版本号、应用名称、自定义K-V数据(meta-data)

    AndroidManifest.xml中的定义如下: <manifest xmlns:android="http://schemas.android.com/apk/res/andro ...

  4. android studio sqlite实际应用中存在的问题

    原项目已上传到github long f = dbdatabase.update("user", values, "id=?", new String[]{St ...

  5. 动态从数据库中获取数据填充Select

    JavaScript代码: $(document).ready(function () { getIntype(); });function getIntype(){ $.ajax({ type:&q ...

  6. JS数据交互:动态从数据库中获取数据填充Select

    JavaScript代码: $(document).ready(function () { getIntype(); });function getIntype(){ $.ajax({ type:&q ...

  7. 【Android Developers Training】 81. 解析XML数据

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. java向文件中添加数据---手动版日志添加

    核心代码为创建多级文件夹创建 //目标文件 File file=new File(filePath); //若不存在即创建文件 if(!file.exists()) { if (!file.getPa ...

  9. (转)SQLServer_十步优化SQL Server中的数据访问 三

    原文地址:http://tech.it168.com/a2009/1125/814/000000814758_all.shtml 第六步:应用高级索引 实施计算列并在这些列上创建索引 你可能曾经写过从 ...

随机推荐

  1. 变量get、set设置

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. linux 查找文件和文件夹与下载命令

    查找命令: 查找根目录下查找文件夹名称叫dir的目录地址 find / -name dir  -d 查找/var/www/目录下叫index.jsp的文件 find /var/www/ -name i ...

  3. Lusac定理

    转载大佬的模版:http://www.cnblogs.com/vongang/archive/2012/12/02/2798138.html

  4. WPF MVVM架构 EF、WCF、IOC 设计示例经典

    概要 该演示项目利用WPF应用程序构建的MVVM架构示例, 运用了Unity容器接口注入, MVVM的经典设计, 后台利用的EF+WCF. 后台实现: 从数据库生成的emdx 结合上下文进行数据交互, ...

  5. android 自己定义状态栏和导航栏分析与实现

    效果 android 4.4之后,系统是支持自己定义状态栏和导航栏的.举个最典型的样例就是bilibiliclient了(iOS版本号和android版本号能用两套全然不一样符合各自系统的设计ui,良 ...

  6. 高速数论变换(NTT)

    今天的A题.裸的ntt,但我不会,于是白送了50分. 于是跑来学一下ntt. 题面非常easy.就懒得贴了,那不是我要说的重点. 重点是NTT,也称高速数论变换. 在非常多问题中,我们可能会遇到在模意 ...

  7. 团队作业-Beta冲刺(3)

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 这个作业要求在哪里 https://edu.cnblo ...

  8. C#解析HTML源码

    刚做了一个小任务,需要抓取其他网站的部分数据,这里就顺便介绍使用Winista.Text.HtmlParser这个类库如何解析HTML并抓取部分数据 1.获取指定网站的页面源码 string url ...

  9. Android自定义系统分享面板

    在Android中实现分享有一种比较方便的方式,调用系统的分享面板来分享我们的应用.最基本的实现如下: public Intent getShareIntent(){ Intent intent = ...

  10. beego的orm ,用的数据库sqlite3

    测试 beego的orm ,用的数据库sqlite3 1 package main import ( "fmt" "github.com/astaxie/beego/or ...