Fabio Mont Ответов: 0

Android studio - listview - неправильная позиция на setonitemclicklistener после getfilter().filter()


I'm with a problem. I have a SearchView who filter my ListViewAdapter when the user put a word. Everything here is Ok. I can get the listViewAdapter and filter this, but, when I try to get it's position, it works fine when my list items aren't filtered, when I search a new word, I can't get the right position of it. I'll try to explain this with the code below:


So, in my search view, everything is ok, the problem is when I search someting and try to get the item position.


The problem is when I filter something I lost the original position. Let's supose that the first item has the position "0" I sum +1 cause my database has the product code 1, so, if I have two items on my listview, I have two positions, 1 and 2. When the App load, everything is ok. It's work perfect, but, after I filter it, I can't get the right position of it. The position of the item two must be two, but in my case it's 1. So I'm getting the wrong data from the database. I filter the listview with a word, and when it's filtered, I get the position from item 1(It's wrong), I want to get the position of the current item filtered.

Thank's for the attention and sorry the bad english. I hope someone help me!


Что я уже пробовал:

<pre>@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.main_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);


    if (searchItem != null) {
        searchView = (SearchView) searchItem.getActionView();
    }
    if (searchView != null) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setQueryHint("Buscar");

        queryTextListener = new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                // Log.i("onQueryTextChange", newText);
                listViewAdapter.getFilter().filter(newText);
                return false;
            }
            @Override
            public boolean onQueryTextSubmit(String query) {
                //Log.i("onQueryTextSubmit", query);

                return true;
            }
        };
        searchView.setOnQueryTextListener(queryTextListener);
    }
    super.onCreateOptionsMenu(menu, inflater);
}


listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3)
        {
            String cd_product = String.valueOf(adapter.getItemIdAtPosition(position)+1);

            productList = db.getDataProducts(cd_product);

            Intent intent = new Intent(getActivity(), ReaderActivity.class);

            intent.putExtra("product_name", productList.get(0));

            startActivity(intent);
        }
    });

0 Ответов