24

I have an array (array1 with States) and AutoCompleteTextview in which I'm filling it with array1. When I select the value from AutocompleteTextView I select a state from AutoCompleteTextView Dropdown

What I want is to get the position of the item from array1 which I've selected.
What I've tried is OnClickEvent of AutocompelteTextView.

STATE.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        String selection = (String) parent.getItemAtPosition(position);
        String num = selection;
    }
});

It ss giving me the value which I selected from dropdown but i want to get the position of the value in array1. For Example I have an array of size 4, like array1 = {A,B,C,D}, and if I select B it should return me B position in Array i.e 2.

I hope I made it clear. Thanks in advance.

Emil Sierżęga
  • 1,785
  • 2
  • 31
  • 38
raghav chopra
  • 827
  • 3
  • 9
  • 25
  • You can use int position variable for the position of the value in onItemClick Listener. – Chirag Nov 29 '12 at 08:41
  • Item.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { //// id contains item if from database ItemNoSelected = id; } }); --- here id is id from database – Vijay Jul 07 '15 at 06:57

5 Answers5

31

Use the position variable in the onItemClick.

STATE.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        String selection = (String) parent.getItemAtPosition(position);
        int pos = -1;

        for (int i = 0; i < yourarray.length; i++) {
            if (yourarray[i].equals(selection)) {
                pos = i;
                break;
            }
        }
        System.out.println("Position " + pos); //check it now in Logcat
    }
});
Emil Sierżęga
  • 1,785
  • 2
  • 31
  • 38
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
3

Use List instead of Array. Implement onItemClickListener for AutoCompleteTextView, then use indexOf on your list to find the index of selected item.

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 ;)
     }
});
SANAT
  • 8,489
  • 55
  • 66
0
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            {
                TextView txtvw=(TextView) view;
                String str=txtvw.getText().toString();
                int index = contactNames.indexOf(str);
            } 
Jasmine John
  • 873
  • 8
  • 12
0
Item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    //// id contains item if from database
                    ItemNoSelected = id;
                }
            });

id is from database, we can use that

Vijay
  • 2,021
  • 4
  • 24
  • 33
-3

You can find the parent string in the clickItem listener. OSchools in this case is the custom ArrayList.

Object item = parent.getItemAtPosition(position);
for(OSchools d : data){
Log.e("name",d.getName());
String name = d.getName();
if(d.getName() != null && name.contains(item.toString())){
       ID = d.getID();
}
  • 1
    An answer based only on code it's not considered a proper SO answer. Could you add explanation for your code? (from review) – FrankS101 Nov 22 '18 at 18:53