1

如何自定义自动完成文本视图的建议列表?我需要在其中显示图像和文本,我正在使用无法自定义它的 baseadapter 类下面是我的代码。请看一下并给我解决这个问题的建议谢谢....

public class AutocompleteTextActivity extends Activity {
        /** Called when the activity is first created. */

        static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania",
                "Algeria", "American Samoa", "Andorra", "India", "Indonesia" };
        static final Integer[] Images = { R.drawable.one, R.drawable.two,
                R.drawable.three, R.drawable.four, R.drawable.five, R.drawable.six,
                R.drawable.seven };

        private AutoCompleteTextView searchText;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            searchText = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
            searchText.setAdapter(new EfficientAdapter(this));

        }

        private static class EfficientAdapter extends BaseAdapter {
            private LayoutInflater li;

            public EfficientAdapter(Context context) {
                li = LayoutInflater.from(context);
            }

            public int getCount() {
                return COUNTRIES.length;
            }

            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    convertView = li.inflate(R.layout.listitem, null);
                    holder = new ViewHolder();
                    holder.name = (TextView) convertView.findViewById(R.id.text);
                    holder.image = (ImageView) convertView.findViewById(R.id.image);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.name.setText(COUNTRIES[position]);
                holder.image.setBackgroundResource(Images[position]);
                return convertView;
            }

            static class ViewHolder {
                TextView name;
                ImageView image;
            }
        }
    }
4

1 回答 1

0

试试这个作为参考。希望这会对某人有所帮助!

于 2012-08-23T18:58:20.647 回答