首页面图:
点击搜索,输入要查询的联系人时出现:
清单文件中加权限:
<!-- 读写联系人的权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
主逻辑代码文件:
<span style="font-size:18px;">package com.example.day17_loadercontact;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MainActivity extends Activity implements LoaderCallbacks<Cursor>{
//清单文件中加权限
ListView lv;
TextView tv_show;
SimpleCursorAdapter adapter=null;
//ContentResolver cr;
LoaderManager manager;
@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.lv);
tv_show=(TextView) findViewById(R.id.tv_show);
//cr=getContentResolver();
//得到loader管理者对象
manager=getLoaderManager();
/*
* 参数1:loader的唯一标识
* 参数2:要传给loader对象的参数
* 参数3:回调接口
* 如果loader的id存在使用最后一个loader,不存在则创建loader
*/
manager.initLoader(1,null,this);
//最后一个参数为内容观察者,时刻观察数据变化
adapter=new SimpleCursorAdapter(getApplicationContext(), R.layout.style, null, new String[]{"_id","display_name"},new int[]{R.id.tv1,R.id.tv2},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lv.setAdapter(adapter);
lv.setEmptyView(tv_show);
}
//根据指定的loader的id创建loader对象
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String uri="content://com.android.contacts/raw_contacts";
/*
* 参数1:上下文对象
* 参数2:contentprovider对象 uri
* 参数3:要查询的字段数组
* 参数4:查询的条件
* 参数5:查询条件的值
* 参数6:排序
*/
//未加搜索菜单的
// CursorLoader loader=new CursorLoader(getApplicationContext(), Uri.parse(uri),new String[]{"_id","display_name"}, null, null, null);
// return loader;
if(args!=null)
{
String str=args.getString("my_key");
CursorLoader loader=new CursorLoader(getApplicationContext(), Uri.parse(uri),new String[]{"_id","display_name"}, "display_name like ?", new String[]{str+"%"}, null);
return loader;
}
CursorLoader loader=new CursorLoader(getApplicationContext(), Uri.parse(uri),new String[]{"_id","display_name"}, null, null, null);
return loader;
}
//loader加载完毕时调用,更新ui
//参数1:当前加载完毕的loader对象
//参数2:加载完后的数据
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
adapter.swapCursor(arg1);
}
//loader重启或退出时调用
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
adapter.swapCursor(null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main,menu);
MenuItem item=menu.findItem(R.id.item_search);
//得到动作(menu布局里加的动作)
SearchView sv=(SearchView) item.getActionView();
sv.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Bundle bundle=new Bundle();
bundle.putString("my_key", newText);
//因在方法里找不到,所以最后一个参数不能直接用this
getLoaderManager().restartLoader(1, bundle, MainActivity.this);
return false;
}
});
return true;
}
}
</span>
主布局文件:
<span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
/>
</RelativeLayout>
</span>
适配器布局文件:
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
</LinearLayout>
</span>
菜单文件:
<span style="font-size:18px;"><menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item_search"
android:orderInCategory="100"
android:showAsAction="always"
android:title="@string/action_settings"
android:actionViewClass="android.widget.SearchView"/>
<!--
android:actionViewClass="android.widget.SearchView"
参数为SearchView所在的包名
-->
</menu>
</span>