package com.zymic.home;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ContactList extends ListActivity {
private ListView contactList;
//
private Cursor contact_cursor;
private Cursor iphonenumber_cursor;
//
private List<String> phonenumbers=new ArrayList<String>();
private int contact_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//实例化ListView;
contactList=this.getListView();
//异步的方法将联系人的信息放入contactList;
new MyAsyncTask(this).execute(contactList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//参数v是
TextView tvid=(TextView) v.findViewById(R.id.idtv);
contact_id=Integer.valueOf(tvid.getText().toString());
//Toast.makeText(this, contact_id+"", Toast.LENGTH_SHORT).show();
iphonenumber_cursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{String.valueOf(contact_id)}, null);
while(iphonenumber_cursor.moveToNext()){
phonenumbers.add(iphonenumber_cursor.getString(iphonenumber_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
//R.layout.dialogtextview应该是不带有布局的只有一个TextView;否则会报错的;
//final ArrayAdapter arrayadapter=new ArrayAdapter(this, android.R.layout.test_list_item, phonenumbers);
final ArrayAdapter arrayadapter=new ArrayAdapter(this, R.layout.dialogtextview, phonenumbers);
//
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("电话号码");
builder.setAdapter(arrayadapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//得到选择的电话号码;
//此处选择实际上是在对话框的数据适配器中选择;
String phone_number=(String) arrayadapter.getItem(which);
//
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel://"+phone_number));
ContactList.this.startActivity(intent);
}
});
builder.show();
}
}
//
package com.zymic.home;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.widget.BaseAdapter;
import android.widget.ListView;
public class MyAsyncTask extends AsyncTask<ListView, Void, BaseAdapter> {
private Context context;
private Cursor cursor;
private ListView lv;
//构造方法;
public MyAsyncTask(Context context){
this.context=context;
}
@Override
protected BaseAdapter doInBackground(ListView... params) {
//根据ContactsContract.Contacts.CONTENT_URI这个Uri查询出所有联系人对象的cursor;
cursor=context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
//
MyBaseAdapter myAdapter=new MyBaseAdapter(context,cursor);
lv=params[0];
if(myAdapter!=null){
return myAdapter;
}else{
return null;
}
}
@Override
protected void onPostExecute(BaseAdapter result) {
super.onPostExecute(result);
if(result!=null){
lv.setAdapter(result);
}
}
}
//
package com.zymic.home;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private Context context;
private Cursor cursor;
//构造方法;
public MyBaseAdapter(Context context,Cursor cursor){
this.context=context;
this.cursor=cursor;
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
//得到LayoutInflater对象;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
//将布局文件转化为视图;
View myview=inflater.inflate(R.layout.textview, null);
//找到视图myview中的TextView控件tv;
TextView tv=(TextView)myview.findViewById(R.id.tv);
//找到视图myview中的TextView控件tv_id;
TextView tvid=(TextView)myview.findViewById(R.id.idtv);
if(cursor.moveToPosition(arg0)){
tv.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
tvid.setText(cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID))+"");
return myview;
}
return null;
}
}