-1

Yes,I know there is position parametar in AdapterView.OnItemClickListener callback, but this parametar return position of filtered(shown) list. For example I have _countriesEvisitor arraylist and I would like to get selected country object by looking into position like this:

 public AdapterView.OnItemClickListener setOnCitizenshipAutocompleteItemClickListener(){


    return (adapterView, view, position, id) -> {
        CountryEvisitor citizenship=new CountryEvisitor();

        citizenship=_countriesEvisitor.get(position);
        _visit.setCitizenship(citizenship);
    };
}

_countriesEvisitor has 250 items, and when I start typing for example Hrvatska I get only one item, and it has position 0 (because it is first and only one in shown list) although it is 247th in the _countriesEvisitor list from which list autocomplete adapter is made of.

How to get selected country?

Vlado Pandžić
  • 4,879
  • 8
  • 44
  • 75

2 Answers2

-1

Just use like below code.

  public AdapterView.OnItemClickListener setOnCitizenshipAutocompleteItemClickListener(){
  return (adapterView, view, position, id) -> {

    CountryEvisitor citizenship= (CountryEvisitor ) view.getAdapter().getItem(position);
    citizenship=_citizenship.getcitizenship();
    _visit.setCitizenship(citizenship);

  };}
Vasant
  • 3,475
  • 3
  • 19
  • 33
  • Please do not post code only answers, explain to the OP what the difference is. Your code does not work because you do this first `CountryEvisitor citizenship= (CountryEvisitor ) view.getAdapter().getItem(position);` and then you use `.get()` on the same object and... assign it again? – Denny May 17 '17 at 12:29
  • I updated my answer.access data member using citizenship object. – Vasant May 17 '17 at 12:32
-2

Keep a second list with the original items, and search through the list matching your search result and get the index of that item

...
_countriesEvisitor.get(position);
int result = -1;
for(int x = 0; x < originalList.size; x++) {
    if (/* some check here */) {
        result = x;
        break;
    }
}
if (result != -1) {
    // check if result is not -1 and use the index
}
Denny
  • 1,766
  • 3
  • 17
  • 37
  • You dont understand question I think – Vlado Pandžić May 17 '17 at 12:21
  • Yes I do. You filter the results you get for example 10 results from the 100. And when you press the second item, it is not the second index of the original 100 items. So keep a copy of the original items and loop through them to find the index. – Denny May 17 '17 at 12:23
  • Sorry, your don't have to downvote only because I downvoted your answer. If you think that question is unclear you can tell me. – Vlado Pandžić May 17 '17 at 12:46
  • I haven't downvoted your answer as seen [here](http://i.imgur.com/et5j57f.png). And if two people did not understand your question, I think it's wrong at your end and you did not explain it well enough. – Denny May 17 '17 at 12:48
  • @Vlado so explain it better what you're trying to achieve and don't just say that people are downvoting you when they aren't – Denny May 17 '17 at 13:01
  • @Vlado Or don't explain it better. – Denny May 17 '17 at 19:21