Android編程實現輸入框動態(tài)自動提示功能
本文實例講述了Android編程實現輸入框動態(tài)自動提示功能。分享給大家供大家參考,具體如下:
關于AutoCompleteTextView的使用,我想大家并不陌生,對其設定上Adapter后系統(tǒng)便能自己識別與匹配了。近期 一個項目中,需要做到匹配通迅錄中的電話號碼和聯系人,由于通迅錄中數據量大,所以把所有的數據在自己提示之前就查詢出來并加入到 AutoCompleteTextView中是不現實的,所以我們可以使用cursor來動態(tài)加載AutoCompleteTextView的數據,從而 實現時時搜索提示,要實現動態(tài)加載,只用重寫一個類繼承于CursorAdapter,然后設定在AutoCompleteTextView上就行了。

AutoCompleteTextView editNumber = (AutoCompleteTextView)findViewById(R.id.edit_number); Cursor cursor = getContentResolver()(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); ContactListAdapter listAdapter = new ContactListAdapter(this, cursor); editNumber.setAdapter(listAdapter);
ContactListAdapter.java中的核心代碼如下:
重寫newView方法
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
final View view = (View)inflater.inflate( R.layout.auto_complete, parent, false);
TextView txtName = (TextView)view.findViewById(R.id.txt_name);
txtName.setText(cursor.getString(0));
TextView txtNumber = (TextView)view.findViewById(R.id.txt_number);
txtNumber.setText(cursor.getString(1));
TextView txtType = (TextView)view.findViewById(R.id.txt_type);
String[] arrType = SmsConstant.ARR_CONTACTS_TYPE;
if(cursor.getint(2) > 3)
{
txtType.setText(arrType[0]);
} else
{
txtType.setText(arrType[cursor.getint(2)]);
}
return view;
}
重寫bindView方法,
public void bindView(View view, Context context, Cursor cursor) {
TextView txtName = (TextView)view.findViewById(R.id.txt_name);
txtName.setText(cursor.getString(0));
TextView txtNumber = (TextView)view.findViewById(R.id.txt_number);
txtNumber.setText(cursor.getString(1));
TextView txtType = (TextView)view.findViewById(R.id.txt_type);
String[] arrType = SmsConstant.ARR_CONTACTS_TYPE;
if(cursor.getint(2) > 3)
{
txtType.setText(arrType[0]);
} else {
txtType.setText(arrType[cursor.getint(2)]);
}
}
點擊彈出的Listview列表后的返回值:
public String convertToString(Cursor cursor) {}
執(zhí)行搜索的sql語句,返回一個Cursor加載到彈出的Listview上
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {}
在此所返回的Cursor結果,會全部顯示在彈出提示上,無需再次過慮。
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android視圖View技巧總結》、《Android布局layout技巧總結》、《Android圖形與圖像處理技巧總結》、《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android開發(fā)之ViewSwitcher用法實例
這篇文章主要介紹了Android開發(fā)之ViewSwitcher用法,結合實例形式分析了ViewSwitcher的功能、使用方法與相關注意事項,需要的朋友可以參考下2016-02-02
Kotlin開發(fā)實戰(zhàn)之hello world
這篇文章主要為大家詳細介紹了Kotlin開發(fā)實戰(zhàn)之hello world的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

