首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

功课五之AsyncTask,SQLiteCommand,GetContactsData,AutoCompleteTextView(Android)

2012-11-26 
作业五之AsyncTask,SQLiteCommand,GetContactsData,AutoCompleteTextView(Android)这次的作业主要是考察SQ

作业五之AsyncTask,SQLiteCommand,GetContactsData,AutoCompleteTextView(Android)

这次的作业主要是考察SQLite的使用,获取手机联系人的信息(姓名,手机号码,QQ/备注),然后导入到数据库中,这里我采用了AsyncTask(异步任务)来处理大量数据的插入,这是Android推荐的多线程用法,当然也可以用Thread和Handler.作业还要求使用自动完成文本编辑框来完成数据搜索任务,所以也加入到标题关键词中去了.(图中那几个test是上一次作业从xls文件文件导入到联系人中的,有兴趣可以去看看)

哦,对,还有一个小地方就是关闭输入法面板,这也是我第一次用到,详情看源码吧: )

功课五之AsyncTask,SQLiteCommand,GetContactsData,AutoCompleteTextView(Android)功课五之AsyncTask,SQLiteCommand,GetContactsData,AutoCompleteTextView(Android)

 

感觉比较重要的代码就是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();}@Overrideprotected String doInBackground(Object... params) {// 获得所有的联系人Cursor cur = d.a.get().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, 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, null, 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();// 查看该联系人有多少个电话号码。如果一个也没有的话这里会返回值为0int 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,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contact_id, null, 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 "操作完成.";}@Overrideprotected 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();}@Overrideprotected 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;        }    }}


 

源码下载:(请使用最新4.1.2SDK载入以确保没有错误)

http://dl.vmall.com/c0na5zn88a

 

热点排行