how to find the position of item in a AutoCompletetextview filled with Array

社会主义新天地 提交于 2019-11-28 21:22:00
Ram kiran

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
    }
});

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 ;)
     }
});
            @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);
            } 
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

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!