Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!


What I have tried:

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


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900