Android autocompletetextview有两个适配器用于建议

时间:2013-02-26 18:55:56

标签: android adapter autocompletetextview

我在AutoCompleteTextView中以这种模式提出建议时遇到问题 [action actionFromContact](这些是2个适配器)ex call michael(其中“call”来自动作适配器,“michael”来自actionFromContact适配器这些以下代码首先将AutoCompleteTextView中的适配器设置为item并且当文本更改为称之为重置为从名称联系db,所以它就像打电话给michael, 问题项目数组太大所以我不能使用替换的东西,第二个替换删除第一个条目,当第二个条目,即从下拉菜单中选择名称,因为它已被空格取代

   private AutoCompleteTextView mEditText;
        private TextWatcher mTextWatcher;
        private ContactPickerAdapter contactPickerAdapter;
        String item[] = { "Call", "Call back"};

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mEditText = (WithSomeOneAutoCompleteTextView) findViewById(R.id.editTextTest);
            contactPickerAdapter = new ContactPickerAdapter(this,
                    android.R.layout.simple_dropdown_item_1line,
                    ContactQuery.getContacts(this, false));
            mEditText.setAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_dropdown_item_1line, item));

    mTextWatcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                    String t = s.toString().replace("call ",");

                    contactPickerAdapter.getFilter().filter(t);
                    if (s.toString().equalsIgnoreCase("call ")) {

                        Toast.makeText(MainActivity.this, "CALL",
                                Toast.LENGTH_SHORT).show();

                        mEditText.setAdapter(contactPickerAdapter);
                    }
                    if (s.toString().equalsIgnoreCase("Call back")) {
    //                  t.replace("call back ", "");
    //                  System.out.println("t is: " + t);
    //                  contactPickerAdapter.getFilter().filter(t);
                        Toast.makeText(MainActivity.this, "Launch",
                                Toast.LENGTH_SHORT).show();
                    }

                }
            };

}

这是ContactPickerAdapter

public class ContactPickerAdapter extends ArrayAdapter<Contact> implements
        Filterable {

    private ArrayList<Contact> contactList, cloneContactList;
    private LayoutInflater layoutInflater;
    private Context mContext;

    public ContactPickerAdapter(Context context, int textViewResourceId,
            ArrayList<Contact> contactList) {
        super(context, textViewResourceId);
        this.contactList = contactList;
        this.cloneContactList = (ArrayList<Contact>) this.contactList.clone();
        layoutInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

        return contactList.size();
    }

    @Override
    public Contact getItem(int position) {

        return contactList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Holder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(
                    R.layout.withsomeone_contact_list_item, null);
            holder = new Holder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.phone = (TextView) convertView.findViewById(R.id.phone);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        Contact contact = getItem(position);
        holder.name.setText(contact.contactName);
        holder.phone.setText(contact.num);
        return convertView;
    }

    @Override
    public Filter getFilter() {
        Filter contactFilter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                    FilterResults results) {
                if (results.values != null) {
                    contactList = (ArrayList<Contact>) results.values;
                    notifyDataSetChanged();
                }

            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // CharSequence t = "";
                System.out.println("contraints is equal: " + constraint);
                // if (constraint.toString().contains("call ")) {
                // // constraint = "";
                // System.out.println("contraints now: "
                // + constraint.toString().replace("call ", ""));
                // t = constraint.toString().replace("call ", "");
                // }
                // } else if
                // (constraint.toString().equalsIgnoreCase("call back ")) {
                // // constraint = "";
                // System.out.println("contraints now: " + constraint);
                // t = constraint.toString().replace("call back ", "");
                //
                // }
                // System.out.println("clone is equal: " + t);
                String sortValue = constraint == null ? "" : constraint
                        .toString().toLowerCase();
                FilterResults filterResults = new FilterResults();
                if (!TextUtils.isEmpty(sortValue.trim())) {
                    ArrayList<Contact> sortedContactList = new ArrayList<Contact>();
                    for (Contact contact : cloneContactList) {
                        if (contact.contactName.toLowerCase().contains(
                                sortValue)
                                || contact.num.toLowerCase()
                                        .contains(sortValue))
                            sortedContactList.add(contact);
                    }

                    filterResults.values = sortedContactList;
                    filterResults.count = sortedContactList.size();

                }
                return filterResults;
            }

            @Override
            public CharSequence convertResultToString(Object resultValue) {
                // need to save this to saved contact
                return ((Contact) resultValue).contactName;
            }
        };

        return contactFilter;
    }

    @SuppressWarnings("unchecked")
    public void setContactList(ArrayList<Contact> contactList) {
        // this isn't the efficient method
        // need to improvise on this
        this.contactList = contactList;
        this.cloneContactList = (ArrayList<Contact>) this.contactList.clone();
        notifyDataSetChanged();
    }

    public static class Holder {
        public TextView phone, name;
    }

1 个答案:

答案 0 :(得分:0)

还有点不清楚你想要实现的目标。

反正。

作为第一步,我将TextWatcher附加到mEditText

mEditText.addTextChangedListener(mTextWatcher);

然后我会删除

contactPickerAdapter.getFilter().filter(t);

看看它是怎么回事。

我建议你也发布contactPickerAdapter的代码。