我想在每个建议行中的AutoCompleteTextView
旁边创建一个带有图标的自定义textView
。我怎样才能做到这一点?
最佳答案
我有一类从数据库读取联系人数据的类
public class SimpleContactAdapter extends SimpleCursorAdapter implements
Filterable {
/** The context. */
private Context context;
public HashMap<Integer,Long> hm;
private int postionCounter;
/** The layout. */
private int layout;
/** The TAG. */
String TAG = "Logs: ";
private ImageView mProfileImgView;
private Bitmap mBitmap;
/**
* Instantiates a new simple contact adapter.
*
* @param context
* the context
* @param layout
* the layout
* @param c
* the c
* @param from
* the from
* @param to
* the to
*/
public SimpleContactAdapter(Context context, int layout, Cursor c,
String[] from, int[] to,ImageView profileImgView,Bitmap bitmap) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
this.mProfileImgView = profileImgView;
this.mBitmap = bitmap;
this.hm = new HashMap<Integer,Long>();
}
public void bindView(View v, Context context, Cursor c) {
int nameCol = c.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
int photoId = c.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
ImageView profile = (ImageView) v.findViewById(R.id.profile_pic);
if (profile != null) {
try {
mBitmap = loadContactPhoto(this.context
.getContentResolver(),c.getLong(photoId));
profile.setImageBitmap(mBitmap);
hm.put(new Integer(postionCounter),c.getLong(photoId));
postionCounter++;
} catch (Exception ex) {
Logger.printMsg("Image not found for contact" + ex.getMessage());
}
}
}
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(cr, uri);
if (input == null) {
Logger.printMsg("Image not found");
return null;
}
return BitmapFactory.decodeStream(input);
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.Data.DISPLAY_NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
hm.clear();
postionCounter=0;
return context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
buffer == null ? null : buffer.toString(), args,
ContactsContract.Data.DISPLAY_NAME + " ASC");
}
@Override
public CharSequence convertToString(Cursor cursor) {
int nameCol = cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
return cursor.getString(nameCol);
}
}
以下是自动完成文本视图的用法...
//query for the people in your address book
cursor = getContentResolver().query(Uri.parse(""+ ContactsContract.CommonDataKinds.Phone.CONTENT_URI), null, null, null,null);
startManagingCursor(cursor);
//bind the name and the number fields
String[] columns = new String[] { ContactsContract.Data.DISPLAY_NAME,ContactsContract.CommonDataKinds.Photo._ID};
int[] to = new int[] { R.id.name_entry, R.id.profile_pic };
mAdapter = new SimpleContactAdapter(this, R.layout.list_entry, cursor, columns, to,mProfileImgIv,mThumbnail);
mProfileNameAuto.setAdapter(mAdapter);