AsyncTask, SQLite, Contacts, AutoComplete TextView (Android)

Keywords: Android Database SQLite Mobile

Links to the original text: https://my.oschina.net/gal/blog/200185

This job is mainly to investigate the use of SQLite, get the information of mobile contacts (name, cell phone number, QQ/ remarks), and then import it into the database. Here I use AsyncTask (asynchronous task) to deal with the insertion of large amounts of data. This is Android recommended multithread usage, of course, it can also use Thread and Handler. operation, and also requires the use of automatic completion text edit box to complete data search. Ask for a task, so it's added to the title keyword. (The test s in the figure are imported from the xls file to the contact for the last assignment, if you're interested, you can see them.)

Oh, yeah, there's a small place to close the input method panel, which is the first time I've used it. See the source code for details.

 

The most important part of the code is AsyncTask, which used to be modeled by Thread and Handler, so only this part of the code is posted, and the source code is still in the end:)

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) {
		// Get all contacts
		Cursor cur = d.a.get().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null,null);

		int id = 1;
		// Cyclic traversal
		if (cur.moveToFirst()) {
			int id_column = cur.getColumnIndex(ContactsContract.Contacts._ID);
			int name_column = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

			do {
				// Get the ID number of the contact
				String contact_id = cur.getString(id_column);
				// Get contact name
				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();

				// See how many phone numbers the contact has. If none of them is there, the return value is 0.
				int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

				if (phoneCount > 0) {
					// Get the contact number
					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 "Operation completed.";
	}

	@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 = "Read Contact Data:" + (String) values[0] + "...";
			la.notifyDataSetChanged();
		} else if(((Integer) values[1]).intValue() == 1) {
			la.progress = "Writing to the database...";
			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;
        }
    }
}


 

Source Download: (Please use the latest 4.1.2 SDK load to ensure there are no errors)

http://dl.vmall.com/c0na5zn88a

 

Reproduced in: https://my.oschina.net/gal/blog/200185

Posted by Barb54 on Tue, 08 Oct 2019 19:01:31 -0700