I have custom listview with data from SQLite database. I can add and edit items. Also I have SearchVIew in toolbar to serch items by name. 

The problem is that when I want to edit item, and use SearchView to find it, onClick method returns wrong id (item of the first in the list). 
If I do not use the search, then everything is fine. For example:[![no search](https://i.stack.imgur.com/Wmjgu.png)](https://i.stack.imgur.com/Wmjgu.png)

[![search](https://i.stack.imgur.com/6a28R.png)](https://i.stack.imgur.com/6a28R.png)

My class code:

```
public class ProductCategoryActivity extends AppCompatActivity {

private ListView myProductsList;
private DataBaseHelper dataBaseHelper;
private SQLiteDatabase db;
private Cursor productCursor;
private ArrayList<Product> productList;
private int tableId;
private String dbName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_products);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Intent idOfGroup = new Intent(getIntent());
    tableId = idOfGroup.getIntExtra("table_id", 0);

    myProductsList = (ListView) findViewById(R.id.lvMyProducts);

    switch (tableId) {
        case 1:
            dbName = DataBaseHelper.TABLE_MY_PRODUCTS;
            break;
    }

    //allow adding and editing products if tableId == 1
    if (tableId == 1) {
        myProductsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //get id by click on list view item
                String sql = "SELECT " + DataBaseHelper.COLUMN_ID + "  FROM " + dbName;
                productCursor = db.rawQuery(sql, null);
                ArrayList<Long> arrayList = new ArrayList<>();
                if (productCursor.moveToFirst()) {
                    do {
                        arrayList.add(Long.parseLong(productCursor.getString(0)));
                    } while (productCursor.moveToNext());
                } else {
                    throw new NullPointerException();
                }
                //sending id to another activity
                Intent intent = new Intent(ProductCategoryActivity.this, ProductAddEditActivity.class);
                intent.putExtra("id", arrayList.get(position));
                startActivity(intent);
            }
        });
    }
    dataBaseHelper = new DataBaseHelper(getApplicationContext());
    //creating db
    dataBaseHelper.create_db();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.newProduct:
            Intent intent = new Intent(ProductCategoryActivity.this, ProductAddEditActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public void onResume() {
    super.onResume();
    productList = new ArrayList<>();
    productList.clear();
    db = dataBaseHelper.open();
    //get data from db
    productCursor = db.rawQuery("select * from " + dbName, null);
    setProduct();
}

//product search by name
public Cursor getProductListByKeyword(String search) {
    productList = new ArrayList<>();
    productList.clear();
    db = dataBaseHelper.open();
    //get data from db by word
    productCursor = db.rawQuery("SELECT * FROM " + dbName + " WHERE "
            + DataBaseHelper.COLUMN_NAME + "  LIKE  '%" + search + "%' ", null);
    setProduct();
    return productCursor;
}

private void setProduct() {
    //select  required columns
    if (productCursor != null && productCursor.getCount() != 0) {
        if (productCursor.moveToFirst()) {
            do {
                Product product = new Product();
                product.setProductName(productCursor.getString(productCursor.getColumnIndex(DataBaseHelper.COLUMN_NAME)));

                productList.add(product);
            } while (productCursor.moveToNext());
        }
    }
    productCursor.close();
    ProductListAdapter productListAdapter = new ProductListAdapter(this, productList);
    myProductsList.setAdapter(productListAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    //the add button is hidden if tableId != 1
    if (tableId != 1) {
        MenuItem menuItem = menu.findItem(R.id.newProduct);
        menuItem.setVisible(false);
    }
    SearchView searchView = (SearchView) menu.findItem(R.id.searchForSee).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            productCursor = getProductListByKeyword(s);
            Toast.makeText(ProductCategoryActivity.this, productCursor.getCount() + " " +
                            getString(R.string.txt_products_found),
                    Toast.LENGTH_LONG)
                    .show();
            return false;
        }
        @Override
        public boolean onQueryTextChange(String s) {
            productCursor = getProductListByKeyword(s);
            return false;
        }
    });
    return true;

}
@Override
public void onDestroy() {
    super.onDestroy();
    db.close();
    productCursor.close();
}

```