質問

How can I get the position of an item from a List that I selected from an AutoCompleteTextview?

autoSearch = (AutoCompleteTextView) findViewById(R.id.autoSearch);
    autoSearch.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

        }
    });
    adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, mLIST);
    autoSearch.setAdapter(adapter);

Using the variable arg2 only gets me the position of the AutoComplete list and not the actual position of the item that is on my mLIST.

Is there a way to get the actual position and not the AutoComplete position?

役に立ちましたか?

解決

Here is quick solution, imho very lazy but it works. I was assuming you have unique words, haven't you? Otherwise you would need make customadapter and use setTag() for items.

autoSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Log.d("xxx", "xxx");

                String text = ((TextView) arg1).getText().toString();
                for(int i=0;i<mLIST.size();i++)
                {
                    if(mLIST.get(i).contains(text))
                    {
                        int lookingForId = i;
                        Log.d("xxx", "looking for id is " + i);
                        break;
                    }
                }
            }
        });

他のヒント

I'm not quite sure there's a direct way, but knowing the selected item from the list you can know its position in your mLIST using indexOf() method on your list.

That assuming each item is unique, if not, this will not work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top