User在欄位中key入 中/英 文字會從資料中搜尋相似的名單, 但
1. 必須從第一個字開始比對, 有第一個字相同的才算數
2. 至少要key入兩個 中/英 文字, AutoCompleteTextView才會列出搜尋結果
本範例用Contacts(聯絡人)作為資料來源, 必須要在AndroidManifest.xml中加入讀取的權限, 否則會crash喔!
Auto_ContactActivity.java
package com.tsots.AutoCompleteTextView_Contact;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.TextView;
public class Auto_ContactActivity extends Activity
{
List contact_id= new ArrayList();
List contact_name= new ArrayList();
AutoCompleteTextView autoCompleteTextView;
EditText edittext;
TextView textview1;
TextView textview2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
autoCompleteTextView = (AutoCompleteTextView) findViewById (R.id.AutoCompleteTextView1);
/*
* 在layout中並沒有真的加入EditText元件, 而是與autoCompleteTextView共用
* 目的是用EditText.getText().toString()取得user選到的值
* 當然, 在onItemClick()內寫arg0.getItemAtPosition(arg2).toString()也有一樣的效果
*/
edittext = (EditText) findViewById (R.id.AutoCompleteTextView1);
textview1 = (TextView) findViewById (R.id.TextView1);
textview2 = (TextView) findViewById (R.id.TextView2);
/*
* 獲得所有的聯絡人資料
* public final Cursor query (
* Uri uri,
* String[] projection,
* String selection,
* String[] selectionArgs,
* String sortOrder
* )
*/
final Cursor cur_name = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
);
try
{
if (cur_name.moveToFirst())
{
int idColumn = cur_name.getColumnIndex(ContactsContract.Contacts._ID);
int nameColumn = cur_name.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do
{
try
{
//逐一將Contacts的ID和NAME蒐集起來
contact_id.add(String.valueOf(cur_name.getString(idColumn)));
contact_name.add(String.valueOf(cur_name.getString(nameColumn)));
}
catch(Exception e)
{
}
}while(cur_name.moveToNext());
}
cur_name.close();
System.out.println("contact_id.size() = "+contact_id.size());
System.out.println("contact_name.size() = "+contact_name.size());
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, contact_name);
autoCompleteTextView.setAdapter(adapter);
/*
* 為AutoCompleteTextView提供的(下拉式)選單清單添加點擊事件
* 帶出info: 電話
*/
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
{
for(int order=0; order<取得聯絡人的電話號碼>
* 若Phone ID == Contacts ID
*/
Cursor phones = getContentResolver().query
(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null,
null
);
if (phones.moveToFirst())
{
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String phoneType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if(phoneType.equals("1"))
{
textview1.setText("住家電話:");
}
else if(phoneType.equals("2"))
{
textview1.setText("行動電話:");
}
else if(phoneType.equals("3"))
{
textview1.setText("公司電話:");
}
else
{
textview1.setText("其它:");
}
textview2.setText(phoneNumber);
}
}
}
}
});
}
finally
{
cur_name.close();
}
}
}



可以請問一下 97行 點擊事件那邊 是什麼?!
回覆刪除不了解
作者已經移除這則留言。
刪除抱歉這裡切換行有點亂
刪除autoCompleteTextView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
{
for(int order=0; order<contact_name.size(); order++)
{
if(contact_name.get(order).equals(edittext.getText().toString()))
{
Cursor cur_name = getContentResolver().query
(
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
ContactsContract.Contacts.DISPLAY_NAME
);
System.out.println("find the contact in position "+order);
//cursor移到user選取的聯絡人的位置
cur_name.moveToPosition(order);
int idColumn = cur_name.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cur_name.getString(idColumn);
Cursor phones = getContentResolver().query
(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null,
null
);
當user在EditText欄位輸入資料, 有查詢結果列出
刪除點選ListView中一筆結果
會去聯絡簿搜尋該筆聯絡人的電話資料(可能聯絡簿中紀錄的是對方的行動電話號碼, 或公司電話號碼)
謝謝喔^^
刪除不客氣^_^
刪除