// NOTES===========================自动文本完成===========================
/**
* ViewHolder类
* @author hacket
*
*/
private static class ViewHolder {
private TextView tvContactName;
private TextView tvContactAddress;
}
/**
* 自动文本完成数据适配器实现类
* @author hacket
*
*/
private class MessageAddressAdapter extends CursorAdapter {
private final LayoutInflater inflater;
/*
* constraint 关键字
*/
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (TextUtils.isEmpty(constraint)) {
return null;
}
// 查询联系人号码
ContentResolver resolver = getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
// content://com.android.contacts/data/phones/
String[] projection = CONTACT_PROJECTION;
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER+" like '%"+ constraint+"%'";
//data1 like '%...%'
Cursor cursor = resolver.query(uri, projection, selection, null, null);
return cursor;
}
public MessageAddressAdapter(Context context, Cursor c,
boolean autoRequery) {
super(context, c, autoRequery);
inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = inflater.inflate(R.layout.item_new_message, parent, false);
ViewHolder holder = new ViewHolder();
holder.tvContactName = (TextView) view.findViewById(R.id.tv_contact_name);
holder.tvContactAddress = (TextView) view.findViewById(R.id.tv_contact_address);
view.setTag(holder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
// 获得数据
String contactDisplayName = cursor.getString(DISPLAY_NAME_COLUMN_INDEX);
String contactNumber = cursor.getString(NUMBER_COLUMN_INDEX);
// 设置数据
holder.tvContactName.setText(contactDisplayName);
holder.tvContactAddress.setText(contactNumber);
}
}
/**
* 设置自动文本完成
*/
private void setAutoTextComplete() {
addressAdapter = new MessageAddressAdapter(this, null, true);
actAddress.setAdapter(addressAdapter);
// 自动文本完成条目点击事件
actAddress.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// 得到条目对象
Cursor cursor = (Cursor) addressAdapter.getItem(position);
String contactNumber = cursor.getString(NUMBER_COLUMN_INDEX);
String contactDisplayName = cursor.getString(DISPLAY_NAME_COLUMN_INDEX);
if (!TextUtils.isEmpty(contactDisplayName)) {
actAddress.setText(contactNumber+"("+contactDisplayName+")");
} else {
actAddress.setText(contactNumber);
}
}
});
}
①
②