0

I'm trying to filter a listview on buttonclick but my database query returns an empty cursor. I can't find where the problem is. Does someone know why the cursor returns empty?

Here is my code:

private void initSearch() {
    searchText = (EditText) findViewById(R.id.search_edit_text);

    ImageButton searchEntry = (ImageButton) findViewById(R.id.button_search_entry);
    searchEntry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.startAnimation(buttonAnimation);
            String catchword = searchText.toString();
            if (catchword.equals("")) {
                Toast.makeText(SearchActivity.this, noCatchword, Toast.LENGTH_LONG).show();
            } else {
                Cursor match = myDb.getWordMatches(catchword); //cursor is empty

                cursorAdapter.swapCursor(match);
                cursorAdapter.notifyDataSetChanged();
            }
        }
    });
}

Here is the database method:

public Cursor getWordMatches(String query) {
    //Cursor c = database.rawQuery("SELECT * FROM "+TABLE_NAME+ " WHERE "+COL_HEAD+ " = '%" +query+"%' OR " +COL_TEXT+ " = '%"+query+"%'", null);
    Cursor c = database.rawQuery("SELECT * FROM " + TABLE_NAME+ " WHERE "+COL_HEAD+ " LIKE ? OR "+COL_TEXT+ " LIKE ?", new String[]{query});
    if(c != null){
        c.moveToFirst();
    }
    return c;
}

I've tried both queries as you can see but both return an empty cursor. The rows are correctly inserted in my database as everything else works fine, so this can not be the reason for my problem.

Thanks in advance!

17
  • try "%"+query+"%" in your selectionArgs Commented Sep 28, 2016 at 9:18
  • have you tried manually executing your SQL statement directly into your DB's command interface? Commented Sep 28, 2016 at 9:24
  • @pskink: still returns empty cursor Commented Sep 28, 2016 at 9:27
  • Try this Cursor c = database.rawQuery("SELECT * FROM " + TABLE_NAME+ " WHERE "+COL_HEAD+ " LIKE ? OR "+COL_TEXT+ " LIKE ?", new String[]{query,query}); Commented Sep 28, 2016 at 9:29
  • @user1506104 no I don't know how that works Commented Sep 28, 2016 at 9:29

1 Answer 1

1

You might want to try using getText().toString();. As in this line:

String catchword = searchText.getText().toString();
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you soo much! That was the problem. Now the cursor is returned with the right content. :)
Glad I could help :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.