简体   繁体   中英

how can i search in android studio database with textwatcher

i want to use search method with textwatcher i do any things bud cursor always return false to me anybody can help me!!!! thank you

its my database:

public static void searchSelect(CharSequence s) {
    database = SQLiteDatabase.openOrCreateDatabase(destPath + "/database.db", null);
    Cursor cursor = database.rawQuery("SELECT * FROM main WHERE title LIKE ' % " + s + " % ' ", null);
    SearchFragment.search.clear();
    Log.i("LOG", "cursor :" + cursor.moveToNext());
    while (cursor.moveToNext()) {
        int id = cursor.getInt(cursor.getColumnIndex("id"));
        String title = cursor.getString(cursor.getColumnIndex("title"));
        String content = cursor.getString(cursor.getColumnIndex("content"));
        String address = cursor.getString(cursor.getColumnIndex("address"));
        String imgAddress = cursor.getString(cursor.getColumnIndex("img_adrs"));
        String city = cursor.getString(cursor.getColumnIndex("city"));
        String category = cursor.getString(cursor.getColumnIndex("category"));
        int cityId = cursor.getInt(cursor.getColumnIndex("city_id"));
        int catId = cursor.getInt(cursor.getColumnIndex("cat_id"));
        int inside = cursor.getInt(cursor.getColumnIndex("inside_id"));

        Structure struct = new Structure(id, title, content, address, imgAddress, city, category, cityId, catId, inside);
        struct.setId(id);
        struct.setTitle(title);
        struct.setContent(content);
        struct.setAddress(address);
        struct.setImgAddress(imgAddress);
        struct.setCity(city);
        struct.setCategory(category);
        struct.setCity_id(cityId);
        struct.setCat_id(catId);
        struct.setInside(inside);
        SearchFragment.search.add(struct);
    }
    cursor.close();
}

and its my textwatcher in my fragment

private void TextWatcher() {
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.i("LOG", "text: " + s);
            searchSelect(s.toString());
            adapterSearch.notifyDataSetChanged();
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

You're doing 2 cursor.moveToNext() with unknown result. After you instantiate the cursor, drop the Log statement and do:

if (cursor != null) {
    if (cursor.moveToFirst()) {
       do {
          // your code here

       } while (cursor.moveToNext())   
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM