Reputation: 2318
I have two classes:
I would like to take the returning String from the method "getCurrentCar" and put it inside MyActivity.java
DatabaseHandler.Java:
public class DatabaseHandler extends SQLiteOpenHelper {
public Car getCurrentCar() {
SQLiteDatabase db = getWritableDatabase();
String sql = "SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM " + TABLE_CARS + "ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(sql, new String[] {});
Car car = null;
try {
if (cursor.moveToFirst()) {
car = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3));
}
}
finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
db.close();
}
return car;
}
}
I have attempted already, but the parameters are asking for Context.
//DatabaseHandler handler = new DatabaseHandler(contexthere);
DatabaseHandler handler = new DatabaseHandler();
handler.getCurrentCar();
I would like to know how to take that return and put it inside my MyActivity.java so I can then use a BitmapFactory to recreate the image.
Thanks
Upvotes: 0
Views: 428
Reputation: 3099
Context
means current runtime :
protected void onCreate(Bundle savedInstanceState) {
...........
...........
// you should instantiate 'DatabaseHandler' here
DatabaseHandler db = new DatabaseHandler(this); // "this" refer to the context
..........
..........
// insert the rows
db.createCar(new Car("Sesame street A","23423","anImage1"));
db.createCar(new Car("Sesame street B","43543","anImage2"));
// get the rows which you mean string
for(Car cars : db.getCurrentCar()){
String rows= "id : "+ cars.getID()+ " address : "+getAddress() + "postcode : "+getPostcode()+" image : "+getImage());
}
}
Just adapt .getID(),getAddress(),getPostcode(),getImage()
based on the 'getter' methods name in your Car
class, as I might write them wrongly
** Codes above I write based on the file that you shared by a link. (your file missing Car
class), but I just predict that it should be like above :
Upvotes: 1