简体   繁体   English

如何从AutoCompleteTextView获取适配器项的位置?

[英]How to get adapter item position from AutoCompleteTextView?

String[] idArray = {"1","2","3","4"};

String[] nameArray = {"Abraham","Abhi","John","Joseph"};


final ArrayAdapter<String> adapterTextView = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nameArray);

etItemName.setAdapter(adapterTextView);

etItemName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

        String id = idArray[position];

    }
});

This is my code. 这是我的代码。 In this, when I type "Jo", John and Jospeh are populating on AutoCompleteTextView and when I selected John I get String id as 0 . 在这种情况下,当我键入“ Jo”时, JohnJospeh会在AutoCompleteTextView上填充,并且当我选择John时,我会得到String id0 But actually I need is 2 . 但实际上我需要的是2 So how can I get 2 instead of 0 ? 那么我怎样才能得到2而不是0呢?

This is not the perfect solution but you can take it as the last option: 这不是完美的解决方案,但您可以将其作为最后的选择:

When you pass the selected string just loop through your nameArray to find its position: 当您传递选定的字符串时,只需遍历nameArray即可找到其位置:

 int position=-1;
    for(int i=0;i<nameArray.length;i++){
    if(selectedItem.equalsIgnoreCase(nameArray[i])){
        position=i;
    }
    }

@Shaheer Palollathil n9153's suggestion would work. @Shaheer Palollat​​hil n9153的建议会起作用。 I'm just changing his answer according to your code. 我只是根据您的代码更改他的答案。

AutoCompleteTextView t;
    t.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int pos = -1;
            for(int i = 0; i < nameArray.length; i++){
                if(((AutoCompleteTextView)view).getText().toString().equals(nameArray[position]))
                    pos = position;
                    break;
            }
            String id = idArray[pos]; // should be your desired number
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用自定义适配器从 AutoCompleteTextView 获取所选项目 - How to get the selected item from a AutoCompleteTextView with a custom Adapter Android AutoCompleteTextView:获取项目位置 - Android AutoCompleteTextView: get item position 如何从Adapter类中设置自定义gridview获取项目位置? - How to get the item position from custom gridview set in an Adapter class? 从自定义适配器获取项目位置 - Get Item Position From Custom Adapter 如何获取AutoCompleteTextView适配器的正确ID - How to get correct ID of AutoCompleteTextView adapter 如何在填充了Array的AutoCompletetextview中查找项的位置 - how to find the position of item in a AutoCompletetextview filled with Array 如何通过项目的参数在回收适配器中获取项目 position? - How to get item position in recycler adapter by item's parameter? 如何使用适配器直接从AutoCompleteTextView删除数据 - how to delete data from AutoCompleteTextView with adapter directly 如何从AutoCompleteTextView获取所选项目的数组索引? - How to get array index of selected item from AutoCompleteTextView? 如何从自定义适配器获取可过滤的原始Listview项目位置 - How to Get Original Listview Item Position from Custom Adapter Implements Filterable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM