androidautocompleteautocompletetextviewsimpleadapter

Android AutoCompleteTextview click not working


I tried AutoCompleteTextView and getting the values but i can't get the values in the click event of the autocompletetextview this is my layout

 <AutoCompleteTextView
    android:id="@+id/atv_places"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_below="@+id/lin"
    android:layout_gravity="center"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/et_chat"
    android:drawableLeft="@drawable/searchblue"
    android:drawablePadding="4dp"
    android:dropDownVerticalOffset="5dp"
    android:dropDownWidth="match_parent"
    android:hint="Search contacts..."
    android:scrollHorizontally="true"
    android:textColor="@color/Black"
    android:textCursorDrawable="@drawable/color_cursor"
    android:textSize="16dp"
    android:visibility="gone" /> 

and my click event code to get the values i populate through adapter. I cant get the clicked values or can't work with the toast.

  atvPlaces = (AutoCompleteTextView) v.findViewById(R.id.atv_places);

    mTxtPhoneNo = (AutoCompleteTextView) v.findViewById(R.id.mmWhoNo);
    mAdapternew = new SimpleAdapter(getActivity(), mPeopleList,
            R.layout.row, new String[] { "Name", "Phone" }, new int[] {
                    R.id.textView1, R.id.textView2 });
    atvPlaces.setAdapter(mAdapternew);
    atvPlaces.setThreshold(1);

    atvPlaces.setOnItemClickListener(new OnItemClickListener() {

        @SuppressWarnings("unchecked")
        @Override
        public void onItemClick(AdapterView<?> av, View arg1, int index,
                long arg3) {

            Toast.makeText(getActivity(), "auto", Toast.LENGTH_LONG).show();
            Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);

            String name  = map.get("Name");
            String number = map.get("Phone");
            atvPlaces.setText(""+name+"<"+number+">");  

        }



    });

Solution

  • I am not getting what is problem in your code but below code i made from your code and its working fine, check by yourself.

     AutoCompleteTextView atvPlaces = (AutoCompleteTextView) findViewById(R.id.atv_places);
            ArrayList<HashMap<String,String>> mPeopleList=new ArrayList<>();
            HashMap h1 = new HashMap<String, String>();
            h1.put("Name","name1");
            mPeopleList.add(h1);
    
            HashMap h2 = new HashMap<String, String>();
            h2.put("Name","name2");
            mPeopleList.add(h2);
    
            final SimpleAdapter myAdapter = new SimpleAdapter(this, mPeopleList,
                    android.R.layout.simple_spinner_dropdown_item, new String[] { "Name"}, new int[] {
                    android.R.id.text1});
    
            atvPlaces.setAdapter(myAdapter);
            atvPlaces.setThreshold(1);
    
            atvPlaces.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> av, View arg1, int index,
                                        long arg3) {
    
                    Toast.makeText(MainActivity.this, "auto", Toast.LENGTH_LONG).show();
                    HashMap<String, String> map = (HashMap<String, String>) av.getItemAtPosition(index);
    
                    String name  = map.get("Name");
                    String number = map.get("Phone");
    
                }
    
    
    
            });