作者meya (落寞之心)
看板Android
標題[開發] 複選聯絡人
時間Mon Jul 26 23:04:19 2010
以往的獲取聯絡人方法,是直接叫出系統內建的activity,之後由使用者點選想要的聯絡
人,再傳回去,如下範例:
/*Uri uri = Uri.parse("content://contacts/people");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);*/
但是選完一個人之後,如果還要再選一個人,那就只能再叫出這個activity
下面這個範例實作出多選聯絡人activity,也就是說叫出自己所寫的activity後,可以一
次選擇多人,之後再傳回去:
(註:之所以不用ListActivity這個現成的Activity,是因為使用系統內建的東西限制太
多,無法加入我所想要的Button上去,所以這個程式裡面手動寫了一個類似ListActivity
的東西,將content provider內聯絡人資料對應在list上)
public class MultiContactBook extends Activity {
List<String> contactName = new ArrayList<String>();
List<String> contactPhone = new ArrayList<String>();
//ArrayList<String> contactEmail = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the display name for the specified person
Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,
new String[]{Contacts._ID, Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER}, null, null, null);
// Announce the phone number
Cursor cursorPhone = null;
// Announce the Email
//Cursor cursorEmail = null;
try {
while(cursor.moveToNext())
{
// Load the phone number (if any).
cursorPhone = getContentResolver().query(Phone.CONTENT_URI,
new String[]{Phone.NUMBER},
Phone.CONTACT_ID + "=" + cursor.getString(0), null,
Phone.IS_SUPER_PRIMARY + " DESC");
cursorPhone.moveToFirst();
/*cursorEmail = getContentResolver().query(Email.CONTENT_URI ,
new String[]{"_id","data1","data2","data3"},
Phone.CONTACT_ID + "=" + cursor.getString(0) , null, null);
*/
//預防電話號碼為空
String HasPhone = cursor.getString(
ursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(HasPhone.equals("1")){
contactName.add(cursor.getString(1));
contactPhone.add(cursorPhone.getString(0));
/*while(cursorEmail.moveToNext()) {
if(!cursorEmail.isNull(1)){
int i = cursorEmail.getInt(0);
String str = cursorEmail.getString(1);
str = cursorEmail.getString(2);
str = cursorEmail.getString(3);
contactEmail.add(cursorEmail.getString(1));
}
}*/
}
}
} finally {
cursor.close();
cursorPhone.close();
//cursorEmail.close();
}
setContentView(R.layout.multiple_contact_book);
final ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_multiple_choice, contactName);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setBackgroundDrawable(null);
final int count = arrayAdapter.getCount();
Button buttonOK = (Button) findViewById(R.id.buttonOK);
Button.OnClickListener listenerOK = new Button.OnClickListener() {
public void onClick(View view) {
Intent mIntent = new Intent();
Bundle mBundle = new Bundle();
List<String> alName = new ArrayList<String>();
List<String> alPhone = new ArrayList<String>();
//List<String> alEmail = new ArrayList<String>();
for(int counter = 0; counter < count; counter++) {
//System.out.println("item[" + counter + "]: " +
//listView.isItemChecked(counter) );
if( listView.isItemChecked(counter) ){
alName.add( contactName.get( counter ) );
alPhone.add( contactPhone.get( counter ) );
//alEmail.add( contactEmail.get( counter ) );
}
}
/*for(String s : alName) {
System.out.println("Name: "+s);
}
for(String s : alPhone) {
System.out.println("Phone: "+s);
}
for(String s : alEmail) {
System.out.println("Email: "+s);
}*/
mBundle.putStringArray("getMultiContactName", alName.toArray( new
String[ alName.size() ] ) );
mBundle.putStringArray("getMultiContactPhone",alPhone.toArray( new
String[ alName.size() ] ) );
//mBundle.putStringArray("getMultiContactEmail",alEmail.toArray( new
//String[ alName.size() ] ) );
mIntent.putExtras(mBundle);
setResult(RESULT_OK, mIntent);
finish();
}
};
buttonOK.setOnClickListener(listenerOK);
}
multiple_contact_book.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
android:id="@+id/widgetMultipleContactBook"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/buttonOK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK"
android:drawableLeft="@drawable/ok"
android:layout_alignParentTop="true"
>
</Button>
<ListView
android:id="@+id/listView"
android:layout_below="@+id/buttonOK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</RelativeLayout>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.114.78.145
※ 編輯: meya 來自: 140.114.78.145 (07/26 23:08)
推 Jotarun:快推 免得被發現看不懂(逃 07/26 23:18
推 LIONDODO:太有心了 07/26 23:23