这次的作业主要是考察sqlite的使用,获取手机联系人的信息(姓名,手机号码,QQ/备注),然后导入到数据库中,这里我采用了AsyncTask(异步任务)来处理大量数据的插入,这是Android推荐的多线程用法,当然也可以用Thread和Handler.作业还要求使用自动完成文本编辑框来完成数据搜索任务,所以也加入到标题关键词中去了.(图中那几个test是上一次作业从xls文件文件导入到联系人中的,有兴趣可以去看看)
哦,对,还有一个小地方就是关闭输入法面板,这也是我第一次用到,详情看源码吧: )
感觉比较重要的代码就是AsyncTask这一块吧,以前都是用Thread和Handler的模型来做的,所以只贴这一部分的代码了,源码依然在最后: )
import java.util.Iterator; import com.fsproject.contactsdatabase.DatabaseControl.ContactsData; import android.content.ContentValues; import android.database.Cursor; import android.os.AsyncTask; import android.provider.ContactsContract; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast; public class InputTask extends AsyncTask<Object,Object,Object> { DatabaseControl d; LoadingAdapter la; public InputTask(DatabaseControl _d){ d = _d; la = new LoadingAdapter(); d.a.get().lv_contacts.setAdapter(la); d.db_deleteAll(); } @Override protected String doInBackground(Object... params) { // 获得所有的联系人 Cursor cur = d.a.get().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null); int id = 1; // 循环遍历 if (cur.moveToFirst()) { int id_column = cur.getColumnIndex(ContactsContract.Contacts._ID); int name_column = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); do { // 获得联系人的ID号 String contact_id = cur.getString(id_column); // 获得联系人姓名 String name = cur.getString(name_column); String qq = null; String phone_number = null; String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; String[] noteWhereParams = new String[] { contact_id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE }; Cursor noteCur = d.a.get().getContentResolver().query(ContactsContract.Data.CONTENT_URI,noteWhere,noteWhereParams,null); if (noteCur.moveToFirst()) { String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE)); if (note.contains("QQ:")) { qq = note.substring(3); } } noteCur.close(); // 查看该联系人有多少个电话号码。如果一个也没有的话这里会返回值为0 int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (phoneCount > 0) { // 获得联系人的电话号码 Cursor phones = d.a.get().getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contact_id,null); if (phones.moveToFirst()) { phone_number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } } d.list_contacts.add(new ContactsData(id,name,qq,phone_number)); publishProgress(name,0); id++; } while (cur.moveToNext()); } cur.close(); publishProgress("",1); ContentValues values = new ContentValues(); for (Iterator<ContactsData> iter = d.list_contacts.iterator(); iter.hasNext();) { ContactsData cd = (ContactsData) iter.next(); values.put("name",cd.name); values.put("qq",cd.qq); values.put("phone_number",cd.phone_number); d.db.insert("contacts","_id",values); } return "操作完成."; } @Override protected void onPostExecute(Object result) { Toast.makeText(d.a.get(),(String)result,Toast.LENGTH_SHORT).show(); d.a.get().lv_contacts.setAdapter(d.a.get().lc_adapter); d.a.get().lc_adapter.notifyDataSetChanged(); } @Override protected void onProgressUpdate(Object... values) { if (((Integer) values[1]).intValue() == 0) { la.progress = "读取联系人数据中:" + (String) values[0] + "..."; la.notifyDataSetChanged(); } else if(((Integer) values[1]).intValue() == 1) { la.progress = "正在写入数据库..."; la.notifyDataSetChanged(); } } public class LoadingAdapter extends BaseAdapter { public String progress; public int getCount() { return 1; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position,View convertView,ViewGroup parent) { TextView tv = null; if(convertView==null) { tv = new TextView(d.a.get()); tv.setGravity(Gravity.CENTER); tv.setTextSize(20); convertView = tv; } else tv = (TextView)convertView; tv.setText(progress); return convertView; } } }