How to get the correct AutoCompleteTextView adapter ID

I am new to Android development, and I ran into a problem that is hard for me to solve. I am trying to figure out how to use the widget correctly AutoCompleteTextView. I want to create AutoCompleteTextViewusing XML data from a web service. I managed to get it to work, but I'm confidently not happy with the exit.

I would like to put a HashMapwith id => name pairs in an AutoCompleteTextView and get the id of the clicked item. When I click on the autofill output of the filtered set, I want to fill out the list under the autofill field, which I also managed to get.

Done:

  • autocomplete works well for simple ArrayList, all data is filtered correctly
  • onItemClick event fires correctly after clicking
  • parent.getItemAtPosition(position)returns the correct String representation of the clicked item

The event onItemClick(AdapterView parent, View v, int position, long id)does not behave as we would like. How can I determine the position of an unfiltered array of a clicked element? The filtered position is one that doesn't interest me.

Additional questions:

  • How to deal with HashMapsor CollectionsinAutoCompleteTextView
  • How to get the right one itemIdin an eventonItemClick

I conducted very extensive research on this issue, but did not find any valuable information that would answer my questions.

+5
source share
4 answers

The onItemClick event (parent element of AdapterView, View v, int position, long id) does not behave as we would like.

. , , ( , ). . sdk ( ) onItemClick() position , . getItem() position ( , , ).

String data = getItem(position);
int realPosition = list.indexOf(data); // if you want to know the unfiltered position

Maps ( , SimpleAdapter). Maps .

AutoCompleteTextView, , onItemClick() id (, ).

public class SpecialAutoComplete extends AutoCompleteTextView {

    public SpecialAutoComplete(Context context) {
        super(context);
    }

    @Override
    public void onFilterComplete(int count) {
        // this will be called when the adapter finished the filter
        // operation and it notifies the AutoCompleteTextView
        long[] realIds = new long[count]; // this will hold the real ids from our maps
        for (int i = 0; i < count; i++) {
            final HashMap<String, String> item = (HashMap<String, String>) getAdapter()
                    .getItem(i);
            realIds[i] = Long.valueOf(item.get("id")); // get the ids from the filtered items
        }
        // update the adapter with the real ids so it has the proper data
        ((SimpleAdapterExtension) getAdapter()).setRealIds(realIds);
        super.onFilterComplete(count);
    }


}

:

public class SimpleAdapterExtension extends SimpleAdapter {

    private List<? extends Map<String, String>> mData;
    private long[] mCurrentIds;

    public SimpleAdapterExtension(Context context,
            List<? extends Map<String, String>> data, int resource,
            String[] from, int[] to) {
        super(context, data, resource, from, to);
        mData = data;
    }

    @Override
    public long getItemId(int position) {       
        // this will be used to get the id provided to the onItemClick callback
        return mCurrentIds[position];
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public void setRealIds(long[] realIds) {
        mCurrentIds = realIds;
    }

}

Filter , AutoCompleTextView.

+13

onItemClickListener AutoCompleteTextView, indexOf , .

actvCity.setOnItemClickListener(new OnItemClickListener() {

     @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
          int index = cityNames.indexOf(actvCity.getText().toString());
          // Do Whatever you want to do ;)
     }
});
+2

Luksprog, ArrayAdapter.

    public class SimpleAutoCompleteAdapter extends ArrayAdapter<String>{
        private String[] mData;
        private int[] mCurrentIds;

        public SimpleAutoCompleteAdapter(Context context, int textViewResourceId,
             String[] objects) {
             super(context, textViewResourceId, objects);
             mData=objects;
        }

        @Override
        public long getItemId(int position) {
            String data = getItem(position);
            int index = Arrays.asList(mData).indexOf(data);
            /*
             * Atention , if your list has more that one same String , you have to improve here  
             */
            // this will be used to get the id provided to the onItemClick callback
            if (index>0)
                return (long)mCurrentIds[index];
            else return 0;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        public void setRealIds(int[] realIds) {
            mCurrentIds = realIds;
        }

    }
+1
source

Add your data to custom arraylist first

    // mList used for adding custom data into your model
        private List<OutletListSRModel> mList = new ArrayList<>();
      // listdata used for adding string data for auto completing.
        ArrayList<String> listdata = new ArrayList<String>();
        for (int i = 0; i < JArray.length(); i++) {
           JSONObject responseJson = JArray.getJSONObject(i);
           OutletListSRModel mModel = new OutletListSRModel();
           mModel.setId(responseJson.getString("id"));
           mModel.name(responseJson.getString("outlet_name"));
           listdata.add(responseJson.getString("outlet_name"));
        }

    ArrayAdapter adapter = new
                            ArrayAdapter(getActivity(),
 android.R.layout.simple_list_item_1, listdata);
    searchOutletKey.setAdapter(adapter);

Now to get any value from the model that we added above. we can do it.

searchOutletKey.setOnItemClickListener ( new AdapterView.OnItemClickListener ( ) {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String txtOutletId = mOutletListSRModel.get(position).getId();
            }
        });
0
source

All Articles