在看Android的文档时,看到了这么一个东西: Loader

究竟是什么东西呢?

Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment. Loaders have these characteristics:

1、They are available to every Activity and Fragment.  //支持Activity和Fragment

2、They provide asynchronous loading of data.    //可以进行异步下载

3、They monitor the source of their data and deliver new results when the content changes. //当数据源改变时能及时通知客户端

4、They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data. //发生configuration change时自动重连接

Loader技术为我们提供的核心类有:

    • LoaderManager:可以通过Activity或者的Fragment的getLoaderManager()方法得到LoaderManager,用来对Loader进行管理,一个Activity或者Fragment只能有一个LoaderManager。
    • LoaderManager.LoaderCallbacks:用于同LoaderManager进行交互,可以在其中创建Loader对象。
    • AsyncTaskLoader:抽象类,可以进行异步加载数据的Loader,貌似内部也是通过AsynTask实现的,可以通过继承它构建自己的Loader,也可以使用现有的子类,例如异步查询数据库可以使用CursorLoader。

      使用Loader一般可以经过以下步骤:

      1、初始化Loader,可以使用initLoader(intid, Bundle args, LoaderManager.LoaderCallbackscallback);方法进行初始化。

      id:标识Loader的ID,一个Activity或者Fragment只能有一个LoaderManager,但可以有多个Loader,通过ID区 分。在新建Loader时,如果发现已经有相同ID的Loader就会复用该Loader,而不会重新创建。
      args:传给新建Loader的参数。

      Callback:回调接口。

      2、实现LoaderManager.LoaderCallbacks中的方法,LoaderManager.LoaderCallbacks中需要实现的方法有:
      publicLoader onCreateLoader(int id, Bundle args):创建新的Loader,id为LoaderID,如果已经有相同ID的Loader就会复用该Loader,而不会重新创建。 args为初始化时传递的参数。该方法开始异步查询,并返回一个泛型类,如果是查询数据库可以返回一个CursorLoader,可以返回自定义的Loader。public voidonLoadFinished(Loader loader, D data):异步查询结束的会调用这个方法,并返回查询结果 data。public void onLoaderReset(Loader loader): 当调用Loader.reset()将Loader数据清空时,但在系统销毁Loader时会自动调用Loader.reset()方法,我们一般不需要手动调用,只需要在onLoaderReset方法中,将使用Loader的移除。

      3、使用 restartLoader(intid, Bundle args, LoaderManager.LoaderCallbacks callback)方法进行数据更新,和初始化一个,如果有相同id的Loader存在,会复用Loader,并清空原有Loader中的数据,如果没有就新建一个。这个方法一般使用在需要更新数据时,例如下面例子中,在搜索关键改变时,需要调用这个方法,从新异步查询数据。

public class CollectDetail extends Activity implements LoaderCallbacks<Cursor> {

private ListView listview;
private SimpleCursorAdapter adapter;
private LoaderManager loaderManager;
private MySQLiteOpenHelper openHelper;
private static SQLiteDatabase db;
private TextView textview_empty;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//去掉标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_collect_detail);

openHelper = new MySQLiteOpenHelper(this);

//获取DB对象
db = openHelper.getWritableDatabase();

listview = (ListView) findViewById(R.id.collect_listView);
textview_empty = (TextView) findViewById(R.id.textview_collect_empty);//当listview空时显示

//创建适配器(此时数据为空"null")
adapter = new SimpleCursorAdapter(this, R.layout.collect_listview_item, null, new String []{"title","nickname","create_time"}, new int[]{R.id.collect_item_title,R.id.collect_item_nickname,R.id.collect_item_create_time},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

listview.setAdapter(adapter);

listview.setEmptyView(textview_empty);//设置无数据时显示的view

//获取loadermanager管理者对象
loaderManager = getLoaderManager();
//初始化loader

//第一个参数用于标记一个loader.因为loaderManager可以管理多个loader

第二个参数"Bundle args"  为查询条件,无条件是为null即可

第三个参数为初始化loader之后的回调方法(LoaderCallbacks<Cursor>) 一般让此时的Activity去实现

loaderManager.initLoader(1, null,this );

//给显示数据的listview设置条目点击监听
listview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//跳转到详情界面
String sql ="select _id,title,nickname,create_time,flag from collect";

Cursor cursor = db.rawQuery(sql, null);
cursor.moveToPosition(position);//将cursor游标移动至此position的位置

//从cursor中获取此条目的详细信息
String _id = cursor.getString(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String nickname = cursor.getString(cursor.getColumnIndex("nickname"));
String create_time = cursor.getString(cursor.getColumnIndex("create_time"));
int flag = cursor.getInt(cursor.getColumnIndex("flag"));

Intent intent = new Intent();
Bundle bundle =new Bundle();
bundle.putString("id", _id);
bundle.putString("title", title);
bundle.putString("nickname", nickname);
bundle.putString("create_time", create_time);
bundle.putInt("flag",flag);
intent.putExtras(bundle);

intent.setClass(CollectDetail.this, ItemListview.class);

startActivity(intent);

}
});

//注册上下文菜,用于 删除此条目,并实施更新listview上的数据
registerForContextMenu(listview);

}
//创建出上下文菜单

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.collect_listview, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
//当上下文菜单中的条目被点击时,删除此条目,并重启loader
@Override
public boolean onContextItemSelected(MenuItem item) {

AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
int position = menuInfo.position;

Cursor cursor = adapter.getCursor();//从适配器中获取到cursor

cursor.moveToPosition(position);
String _id = cursor.getString(cursor.getColumnIndex("_id"));//获取到此位置信息的_id更具此_id进行删除操作

String sql ="delete from collect where _id='"+_id+"'";
db.execSQL(sql);

loaderManager.restartLoader(1, null, this);//通过loaderManager重启loader
return super.onContextItemSelected(item);
}

//执行接口回调时,必须要执行的3个方法

//首先获取到loader对象

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
   MyAsyncTaskLoader loader = new MyAsyncTaskLoader(this);
    return loader;  
}

//当数据加载完成时调用的方法,此时使适配器置换数据为"data"

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

adapter.swapCursor(data);

}

//当loader重启是调用的方法,此时将适配器中的数据置换为空

@Override
public void onLoaderReset(Loader<Cursor> loader) {

adapter.swapCursor(null);

}

//写一个类继承AsyncTaskLoader<Cursor>(此类必须定义为静态static类型的)
public static class MyAsyncTaskLoader extends AsyncTaskLoader<Cursor>{

public MyAsyncTaskLoader(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onStartLoading() {
// TODO Auto-generated method stub
super.onStartLoading();
  forceLoad();//必须要写此方法,使其能够向下执行loadInBackground
}

@Override
public Cursor loadInBackground() {
String sql = "select _id,title,nickname,create_time from collect";
Cursor cursor_collect = db.rawQuery(sql, null);

return cursor_collect;
}

}

}

使用Loader实时查询本地数据库用法的更多相关文章

  1. Windows Phone本地数据库(SQLCE):11、使用LINQ查询数据库(翻译) (转)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十一篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...

  2. mysql数据库(三):查询的其他用法

    一. 查询—IN的用法 语法:select ... from 表名 where 字段 a in (值b, 值c, 值d...) 等价于 select ... from 表名 where 字段a=值b ...

  3. java定时器和实时查询数据库

    定时器: Timer timer = new Timer();                    timer.schedule(new TimerTask() {                  ...

  4. HTML5权威指南--Web Storage,本地数据库,本地缓存API,Web Sockets API,Geolocation API(简要学习笔记二)

    1.Web Storage HTML5除了Canvas元素之外,还有一个非常重要的功能那就是客户端本地保存数据的Web Storage功能. 以前都是用cookies保存用户名等简单信息.   但是c ...

  5. SQL Server2016 新功能实时查询统计信息

    SQL Server2016 新功能实时查询统计信息 很多时候有这样的场景,开发抱怨DBA没有调优好数据库,DBA抱怨开发写的程序代码差,因此,DBA和开发都成为了死对头,无法真正排查问题. DBA只 ...

  6. sql server 警报管理,实时监听数据库动向,运筹帷幄之中

    工作这么多年了,无论是身边的同学还是同事,发现只要搞程序员的都有一个通病---懒.懒到谁都不愿意加班,尤其是"义务"加班.即使大家都不愿意加班,但是很多时候项目赶着上线或者上线之后 ...

  7. HTML5本地数据库(WebSQL)[转]

    除了sessionStorage和localStorage外,HTML5还支持通过本地数据库进行本地数据存储,HTML5采用的是"SQLite"这种文件型数据库,该数据库多集中在嵌 ...

  8. Html5 学习系列(六)Html5本地存储和本地数据库

    一个网站如何能在客户的浏览器存储更多的数据呢? 在Html4的时代在浏览器端存储点网站个性化的数据,尤其是用户浏览器的痕迹,用户的相关数据等一般只能存储在Cookie中,但是大多是浏览器对于Cooki ...

  9. Html5本地存储和本地数据库

    一个网站如何能在客户的浏览器存储更多的数据呢? 在Html4的时代在浏览器端存储点网站个性化的数据,尤其是用户浏览器的痕迹,用户的相关数据等一般只能存储在Cookie中,但是大多是浏览器对于Cooki ...

随机推荐

  1. 编写WPF程序,完成弹框打印和直接打印

    弹框打印 PrintDialog pd = new PrintDialog(); pd.ShowDialog(); //↓第一个参数是StackPanel控件里面放一个label放打印的文字 pd.P ...

  2. Linux check whether hyperthreading is enabled or not

    There parameters need to be obained: no. of physical CPU; no. of cores on each CPU; no. of all threa ...

  3. JSON与Javabean转换的几种形式

    JSON格式的数据传递是最常用的方法之一,以下列出了常用的几种形态以及与Javabean之间的转换: String json1="{'name':'zhangsan','age':23,'i ...

  4. 在活动之间切换(隐式Intent)

    实验名称:在活动之间切换 实验现象:在主活动中点击button1可以进入下一个活动 使用技术:隐式Intent 步骤: 1.创建一个项目,加载布局并在布局中添加一个button 部分截图未截,直接Ne ...

  5. [转]Eclipse 项目转移到Android Studio遇到的问题

    1.Android Studio直接导入项目是copy原项目的,无法纳入代码管控 解决方案: 英文地址:http://developer.android.com/sdk/installing/migr ...

  6. Backbone.js 的最佳应用场景有哪些?#zhihu#

    这段时间,想再次了解下backbone js的相关知识,就把一些认为不错的拿过来了: 新版的有道笔记 Web 版(http://note.youdao.com)也使用了 Backbone.就像其他答案 ...

  7. vscode 插件设置

    VS Code 安装插件 prettier Beautify vscode 首选项 --> 设置 "editor.detectIndentation": false, &qu ...

  8. OpenWrt包管理软件opkg的使用(极路由)

    说明: 1.OpenWrt本身系统没什么问题,关键点是一些路由器尝试的限制,比如一些厂商设置成内存分区为只读,那么这个安装软件就变得没什么意义了. 2.opkg的操作有点反人类,正常步骤是查询,安装: ...

  9. Windows蓝屏dump文件查看器(转)

    Windbg-分析Windows蓝屏原因利器[转]下载地址先声明下,虽然用windbg诊断蓝屏之前网络上已经有人发过教程了,但就我而言, 学会使用windbg来诊断蓝屏也算是自己的原创吧.以前看一个微 ...

  10. nginx出现504 Gateway Time-out的解决思路

    http://www.xbc.me/nginx-fix-504-gateway-timeout/ 在安装完Nginx+PHP-fpm+Mysql后 (如何安装LNMP环境,请参考快速配置LNMP环境N ...