1

I'm studying Android studio and started with databases this week. I would like a little help with this app. It is simple. The user enters a string and presses a button in the first activity. This string then is sent to the second activity where it is compared with values in a database and display the ones that are similar:

public class DrinkCategoryActivity extends ListActivity {
public static final String EXTRA_MESSAGE = "message";
private SQLiteDatabase db;
private Cursor cursor;
private Cursor newcursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String messageText = intent.getStringExtra(EXTRA_MESSAGE);
    //setContentView(R.layout.activity_drink_category);
    ListView listDrinks = getListView();
    try{
        SQLiteOpenHelper starbucksDatabaseHelper = new StarbucksDatabaseHelper(this);
        db = starbucksDatabaseHelper.getReadableDatabase();
        /*
            SELECT _id, NAME FROM DRINKS;
         */
        cursor = db.query("DRINK",
                    new String[]{"_id", "NAME"},
                    null, null, null, null, null);
        newcursor = cursor.equals(messageText);
        CursorAdapter listAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1,
                cursor,
                new String[]{"NAME"},
                new int[]{android.R.id.text1},
                0);

        listDrinks.setAdapter(listAdapter);

    }catch (SQLiteException ex){
        Toast toast = Toast.makeText(this, "Database unavailable",
                Toast.LENGTH_LONG);
        toast.show();
    }

i have been looking all over the web but i can not find a good explanation. database

this is the database were are the drinks

public class StarbucksDatabaseHelper extends SQLiteOpenHelper{
    add a varaible for the db name
    private static final String DB_NAME = "starbucks";

    version of the database
    private static final int DB_VERSION = 1;

    StarbucksDatabaseHelper (Context context){
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE DRINK ("
                + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "NAME TEXT, "
                + "DESCRIPTION TEXT, "
                + "IMAGE_RESOURCE_ID INTEGER);");
     insert data
        insertDrink(db, "Soy Latte", "Esspresso and steamed soy milk", R.drawable.latte);
        insertDrink(db, "Mocaccino", "Esspresso, hot milk, steamed milk foam, and cocoa", R.drawable.cappuccino);
        insertDrink(db, "American", "Drip coffee", R.drawable.filter);
    }

    private static void insertDrink (SQLiteDatabase db,
                                     String name,
                                     String description,
                                     int resourceId){
        ContentValues drinkValues = new ContentValues();
        drinkValues.put("NAME", name );
        drinkValues.put("DESCRIPTION", description);
        drinkValues.put("IMAGE_RESOURCE_ID", resourceId);


        db.insert("DRINK", null, drinkValues);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
4
  • Why you added newcursor = cursor.equals(messageText);? also, the answer to your question depends upon how do you want to match the string with your database - exactly same or contains that string, starts with it, ends with it ,.....? Commented Apr 19, 2016 at 23:10
  • I forgot to remove that . i will like if it could check world by world because the strings in the databases could have more that two world and the and i will like that it displayed all the string that are similar to the string that the user enters Commented Apr 19, 2016 at 23:20
  • Can you explain by giving an example ? Commented Apr 19, 2016 at 23:21
  • if the user input student and in the databases there it is student id, student services room i will like to display the two that have student Commented Apr 19, 2016 at 23:27

2 Answers 2

1

Try this query instead if you want that any row containing your messageText in the start, middle or the end should be retrieved -

cursor = db.query("DRINK", new String[]{"_id", "NAME"}, "NAME like %?%", new String[]{messageText}, null, null, null);
Sign up to request clarification or add additional context in comments.

10 Comments

Then I guess there is no value matching with your database records.
my input was the same that one of the values in the databases
Can you attach your database screenshot and what is the value of "messagetext" you are passing to the query.
i posted the databases
I meant database records . use the plugin I mentioned and verify.
|
0

What worked for me was using the trim() attached to the strings you are checking. What's happening is most likely, the one in the database is stored with an extra character.

Comments

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.