0



I am having a listview with one profile imageview, two text views and a favorite imageview.
In search box which I have provided above list view I want to query sqlite DB and get updated results populated in my listview using SimpleCursorAdapter.

The following is my create table statement which shows all columns involved.

public static String CREATE_TABLE_STMT="create table "
            +CONTACT_TABLE+" ("
            +ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
            +NAME+" varchar(100) NOT NULL UNIQUE ,"
            +PHONE+" varchar(100) ,"
            +EMAIL+" varchar(100) ,"
            +JOBTITLE+" varchar(100) ,"
            +WEBPAGE+" varchar(100) ,"
            +PICTUREURL+" varchar(100) ,"
            +THUMBNAILURL+" varchar(100) ,"
            +PICTUREPATH+" varchar(100) ,"
            +THUMBNAILPATH+" varchar(100))";

I am trying to run the following query to search rows which has "NAME" matching with my search text. To be specific I am trying to retrieve rows who's name column value starts with search query .

String searchQuery = "SELECT ID as _id," + AppConstants.NAME+","+AppConstants.JOBTITLE+","+AppConstants.THUMBNAILURL +  " from " + AppConstants.CONTACT_TABLE + " where " + AppConstants.NAME + " LIKE '" + query + "';";


  • Searching for "al" should return back full row having name as "Alan"
  • Searching for "tho" should return back full row having name as "Thomas"

After running the query I am checking the cursor values and trying to update my list view

if (cursor != null) {
            Log.i(AppConstants.APPUILOG,"cursor not null");
            cursor.moveToFirst();
            String[] from = new String[] new String[] {NAME,JOBTITLE,THUMBNAILURL};
            int[] to = new int[] {R.id.textViewMain,R.id.textViewSub,R.id.contact_icon};
            SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.contact_list_item, cursor, from, to);
            mainListView.setAdapter(cursorAdapter);
        }else {
            Log.i(AppConstants.APPUILOG,"cursor is null");
        }

But this code is always giving back blank listview.

  • Do I have to customize SimpleCursorAdapter to implement the same?
  • What modification I need to do in my query?
  • How to create a custom SimpleCursorAdapter and display the updated results?
1
  • see FilterQueryPrivider documentation Commented Oct 18, 2015 at 8:30

1 Answer 1

1

As I am getting ,You want to search in ListView . Am I right ? If yes then you need change your approach , You don't need to interact with DB while searching in ListView . You need to implement Filters in ListView . You can see this link for complete solution .

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Android Dev . I was in the wrong direction. Thanks for the link and guidance.
@Harry of course you have to interact with DB while searching for data, your data are stored in DB aren't they? so how to search them if not by making a query? just read about FilterQueryPrivider and in your search box use adapter.getFilter().filter(constraint) that's all, see here for a sample code

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.