1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView;
public class ContactListActivity extends Activity { protected static final String tag = "ContactListActivity"; private ListView lv_contact; private List<HashMap<String,String>> contactList = new ArrayList<HashMap<String,String>>(); private MyAdapter mAdapter; private Handler mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { mAdapter = new MyAdapter(); lv_contact.setAdapter(mAdapter); }; };
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact_list); initUI(); initData(); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return contactList.size(); }
@Override public HashMap<String, String> getItem(int position) { return contactList.get(position); }
@Override public long getItemId(int position) { return position; }
@Override public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(getApplicationContext(), R.layout.listview_contact_item, null); TextView tv_name = (TextView) view.findViewById(R.id.tv_name); TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone); tv_name.setText(getItem(position).get("name")); tv_phone.setText(getItem(position).get("phone")); return view; } }
private void initData() { new Thread(){ public void run() { ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query( Uri.parse("content://com.android.contacts/raw_contacts"), new String[]{"contact_id"}, null, null, null); contactList.clear(); while(cursor.moveToNext()){ String id = cursor.getString(0);
Cursor indexCursor = contentResolver.query( Uri.parse("content://com.android.contacts/data"), new String[]{"data1","mimetype"}, "raw_contact_id = ?", new String[]{id}, null); HashMap<String, String> hashMap = new HashMap<String, String>(); while(indexCursor.moveToNext()){ String data = indexCursor.getString(0); String type = indexCursor.getString(1); if(type.equals("vnd.android.cursor.item/phone_v2")){ if(!TextUtils.isEmpty(data)){ hashMap.put("phone", data); } }else if(type.equals("vnd.android.cursor.item/name")){ if(!TextUtils.isEmpty(data)){ hashMap.put("name", data); } } } indexCursor.close(); contactList.add(hashMap); } cursor.close(); mHandler.sendEmptyMessage(0); }; }.start(); }
private void initUI() { lv_contact = (ListView) findViewById(R.id.lv_contact); lv_contact.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { if(mAdapter!=null){ HashMap<String, String> hashMap = mAdapter.getItem(position); String phone = hashMap.get("phone"); Intent intent = new Intent(); intent.putExtra("phone", phone); setResult(0, intent); finish(); } } }); } }
|