如何使用每个建议行旁边的图标创建自定义 AutoComleteTextView - Android

发布于 2024-11-16 06:39:14 字数 103 浏览 3 评论 0原文

我想创建一个自定义 AutoCompleteTextView,并在每个建议行中的 textView 旁边添加一个图标。我怎样才能做到这一点?

I want to create a custom AutoCompleteTextView with an icon next to the textView in each suggestion line. How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2024-11-23 06:39:14

我有一个从数据库读取联系人数据的类,

 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);

I 've one class that reads contacts data from db

 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);
        }

    }

Below is the usage for Autocomplete textview ...

//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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文