I'm having difficulties working with SQLite to access information to be used in other activities. Right now, I have an activity where I generate info to fill a drink database, a handler to handle all the SQLite methods and information, and several other classes that, ideally, would use this info.
However, I can't seem to use that generated info in a listview. Here is some relevant code. First, the "MakeDrink":
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.makenewdrink);
dbh = new dbHandler(this, null, null, 1);
initialize();
}
private void initialize() {
ing1 = (EditText)findViewById(R.id.etIng1);
ing1amount = (EditText)findViewById(R.id.etIng1Amo);
ing2 = (EditText)findViewById(R.id.etIng2);
ing2amount = (EditText)findViewById(R.id.etIng2Amo);
... //code reduced//
newDrink = (EditText)findViewById(R.id.etNewDrink);
createNewDrink = (Button)findViewById(R.id.bCreateNewDrink);
createNewDrink.setOnClickListener(this);
}
public void onClick(View v) {
Drinks drink = new Drinks(newDrink.getText().toString());
dbh.addDrink(drink);
Context context = getApplicationContext();
CharSequence toastMessage = newDrink.getText().toString() + " added to drink database.";
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
}
Then the relevant code from the handler:
public void addDrink(Drinks drink) { //edit later
ContentValues values = new ContentValues();
values.put(COLUMN_DRINKNAME, drink.get_drinkname());
values.put(COLUMN_ING_1_AMOUNT, drink.get_ing1amount());
values.put(COLUMN_ING_1, drink.get_ing1());
values.put(COLUMN_ING_2_AMOUNT, drink.get_ing2amount());
values.put(COLUMN_ING_2, drink.get_ing2());
... //code reduced //
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_DRINKINFO, null, values);
db.close();
}
Then finally the relevant info from the actual listview populater:
drinkDB = openOrCreateDatabase("drinkinfo.db", Context.MODE_PRIVATE, null);
Cursor crs = drinkDB.rawQuery("SELECT * FROM drinkinfo ", null);
List<String> drinkNames = new ArrayList<String>();
while(crs.moveToNext()){
String uname = crs.getString(crs.getColumnIndex("COLUMN_DRINKNAME"));
drinkNames.add(uname);
}
ListAdapter lvAdapater = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, drinkNames);
What is happening is that when I run the debugger, the "drinkNames" has a size 0, so the listview can't populate with something that doesn't exist. But, when the debugger runs the "handler" information as the drink is made, it recognizes the ingredients and drinkname in the database.
So, I'm not really sure what I'm doing wrong or what I need to do. I'm going to keep looking for a solution, but if there are any suggestions, or if I'm missing something obvious, or if I need to post more code, I'm all ears.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire