我们如何从listview搜索联系人并在android中的edittext中显示

时间:2013-10-24 08:10:27

标签: java android listview

我想在edittext中搜索联系人。但是edittext没有显示联系人列表,请告诉我如何将电话号码与编辑文本搜索和显示相匹配。

    String products[]={};
   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
   Bundle savedInstanceState) {
mInflater = inflater;
    View view = inflater.inflate(R.layout.contacts_list, container, false);
   contactsList = (ListView) view.findViewById(R.id.contactsList);
     inputSearch=(EditText) view.findViewById(R.id.inputSearch);

    adapter = new ArrayAdapter<String>(getActivity(), R.layout.listitem,           R.id.product_name, products);
    contactsList.setAdapter(adapter);       

   (this,android.R.layout.select_dialog_item,str));

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3)

        {
            ContactsFragment.this.adapter.getFilter().filter(cs);

                }
            }  
        } 
    });

    return view;
}

1 个答案:

答案 0 :(得分:1)

您可以使用AutoCompleteTextView代替EditText作为休闲 -

   AutoCompleteTextView inputSearch=(AutoCompleteTextView) view.findViewById(R.id.inputSearch);
   products = new String[]{"Product1","product2","product3"}; 
   inputSearch.setAdapter(new ArrayAdapter<String>(getActivity(),R.layout.listitem,products));
    inputSearch.setThreshold(1);
    inputSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            inputSearch.showDropDown();
        }
    });

    inputSearch.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        //Do whatever you want with selected object
        }
      }

试试这个。