IPOD 3 之搜索自动刷新数据

使用CursorAdpter可以实现,对数据库的监视,当数据库发生变化的时候,自动刷新数据显示。


首先实现CursorAdapter

package com.example.test_ipod_demo.db;

import com.example.test_ipod_demo.R;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class MyCursorAdapter extends CursorAdapter {

	public MyCursorAdapter(Context context, Cursor c, boolean autoRequery) {
		super(context, c, autoRequery);
		// TODO Auto-generated constructor stub
	}

	// 为视图填充数据
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		System.out.println("----bindView-----");
		TextView textView=(TextView) view.findViewById(R.id.textView_foot);
		textView.setText(Utils.getBean(cursor).getSourceString());
	}
	// 创建视图
	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		System.out.println("-----newView----");
		View view=LayoutInflater.from(context).inflate(R.layout.layout_search_listview, null);
		return view;
	}

}

为ListView 绑定CursorAdapter

cursorAdapter=new MyCursorAdapter(this, null, false);
		listViewSearch.setAdapter(cursorAdapter);

再来看 SearchView 搜索变化时候的处理:


			@Override
			public boolean onQueryTextChange(String newText) {

				if (newText.isEmpty()) {
					getLoaderManager().getLoader(0).reset();//清空数据 回调 onLoaderReset方法 但是貌似好像 这个方法没有回调,呵呵
					MainActivity.this.findViewById(R.id.textView_listview_empty).setVisibility(View.GONE);
					return false;
				}

				Bundle bundle=new Bundle();
				bundle.putString("key", newText);
			//重新加载数据 0 加载器标识
			getLoaderManager().restartLoader(0, bundle, loaderCallbacks);

				return false;
			}

现在看加载器 CursorLoader 的实现:

	private LoaderCallbacks<Cursor> loaderCallbacks=new LoaderCallbacks<Cursor>() {

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			// TODO Auto-generated method stub
			System.out.println("-----onCreateLoader-----");
			if (args==null) {
				System.out.println("---onCreateLoader---null--");
				return null;
			}
			SongInfoBean songInfoBean = Utils.getQueryBean( args.getString("key"));
			CursorLoader cursorLoader=null;
			if (songInfoBean.isHasHanzi()) {// 中文 或者 含有其他字符的全段匹配
				cursorLoader=new CursorLoader(MainActivity.this, Uri.parse("content://"+MyDBProvider.AUTHORITY+"/"+MyDBHelper.TABLE_SONG), null, MyDBHelper.ZH_NAME + " like '%" + songInfoBean.getSourceString() + "%' "	+ " order by fir_name DESC" + " limit 50 ", null, null);
			} else {
				cursorLoader=new CursorLoader(MainActivity.this, Uri.parse("content://"+MyDBProvider.AUTHORITY+"/"+MyDBHelper.TABLE_SONG), null, MyDBHelper.EN_PINYIN_NAME + " like '%" + songInfoBean.getTargetString().trim()+ "%' " + " or " + MyDBHelper.FIR_NAME + " like '%" + songInfoBean.getFirNameString() + "%' " + " order by fir_name DESC" + " limit 50 ", null, null);
			}
			return cursorLoader;
		}

		@Override
		public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
			// TODO Auto-generated method stub
			System.out.println("-----onLoadFinished-----");
			//更新Cursor
			cursorAdapter.swapCursor(cursor);
		}

		@Override
		public void onLoaderReset(Loader<Cursor> arg0) {
			// TODO Auto-generated method stub
			System.out.println("-----onLoaderReset-----");
			//重置,清空数据
			cursorAdapter.swapCursor(null);
		}
	};

restartLoader 加载器 后,会回调 onCreateLoader  重新到指定的ContentProvider 中按指定的条件去 查询数据 ,然后 回调 onLoadFinished 方法,swap 更新Cursor 

这样,当搜索框输入条件发生变化是,就会重新启动加载器去查询数据,然后更新关联的 Cursor


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值