package com.example.andday17cursorloader_contacts; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.LoaderManager; import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.support.v4.widget.CursorAdapter; 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; public class MainActivity extends FragmentActivity implements LoaderCallbacks<Cursor> { // // 操作手机通讯录 // private String uri_contacts = "content://com.android.contacts/contacts"; // private String uri_raw_contacts = // "content://com.android.contacts/raw_contacts"; // private String uri_data = "content://com.android.contacts/data"; // private String uri_data_phone = // "content://com.android.contacts/data/phones"; // private String uri_data_email = // "content://com.android.contacts/data/emails"; private ListView listview; private LoaderManager loaderManager; private SimpleCursorAdapter adapter; private String uri_raw_contacts = "content://com.android.contacts/raw_contacts"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) this.findViewById(R.id.listview); loaderManager = getSupportLoaderManager(); loaderManager.initLoader(1, null, this); adapter = new SimpleCursorAdapter(this, R.layout.item_listview, null, new String[] { "_id", "display_name" }, new int[] { R.id.text_id, R.id.text_displayname }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listview.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); // 相当于得到这个视图(布局文件),放到menu里面,(menu文件夹的控件获取方法) // 再通过view跟findViewById()类似,通过查找控件id,查找出SearchVeiw MenuItem item = menu.findItem(R.id.action_searchview);// 查找出SearchVeiw所在的item SearchView searchview = (SearchView) item.getActionView(); // 用SearchView时要将版本给位11,否则会一直显示检查错误 searchview.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String keywords) { // 点击的时候弹出搜索输入文本框,将数据传给onCreateLoader()加载器加载数据 Bundle bundle = new Bundle(); bundle.putString("keywords", keywords); loaderManager.restartLoader(1, bundle, MainActivity.this);// 重新查询 return false; } }); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_contacts:// 显示联系人界面 Intent intent = new Intent(); intent.setAction("com.android.contacts.action.LIST_CONTACTS"); startActivity(intent); break; case R.id.action_restore:// 恢复被删除的联系人 // 将deleted=0,即修改为0后代表都恢复了 通过ContentProvider修改raw_contacts表 ContentResolver resolver = getContentResolver(); ContentValues values = new ContentValues(); // 修改添加等要通过ContentValues values.put("deleted", "0"); int count = resolver.update(Uri.parse(uri_raw_contacts), values, "deleted=1", null); // 大于0代表删除成功了 if (count > 0) { loaderManager.restartLoader(1, null, MainActivity.this); } break; } return super.onOptionsItemSelected(item); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle bundle) { // 注意:new CursorLoader()其实就是主线程,不要以为没有,只是CursorLoader内部封装运行了 if (bundle == null) {// 没有条件则查询全部 没有被删除的联系人,投影查询两个加字段就行了,id跟displayname return new CursorLoader(this, Uri.parse(uri_raw_contacts), new String[] { "_id", "display_name" }, "deleted=0", null, null); } else {// keywords是你输入时SearchView传过来的 // 注意:这里keywords要放到这里,因为如果这里不执行的话,会多于,不要放在if上面 String keywords = bundle.getString("keywords"); return new CursorLoader(this, Uri.parse(uri_raw_contacts), new String[] { "_id", "display_name" }, "display_name like ?", new String[] { keywords + "%" }, null); } } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { adapter.changeCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { adapter.changeCursor(null); } } //布局 <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> //listview里面自定义的布局 <TextView android:id="@+id/text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/text_displayname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" />