Reputation: 11
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) {
}
Upvotes: 1
Views: 218
Reputation: 13
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.
Upvotes: 0
Reputation: 7070
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);
Upvotes: 1