padde wrote:I found some minutes thanks to a canceled meeting
I changed the sample to a ListActivity and show a sample of presenting
the data from the database in a list.
If i understand it right thats what you want.
First the class representing a fruit:
Fruit.javaUsing java Syntax Highlighting
package de.padde.fruitsdbsample; import android.graphics.Bitmap; public class Fruit { private Bitmap bmp; private String name; private int kcal; private int vitaminc; public Fruit(Bitmap b, String n, int k, int v) { bmp = b; name = n; kcal = k; vitaminc = v; } public Bitmap getBitmap() { return bmp; } public String getName() { return name; } public int getKcal() { return kcal; } public int getVitaminC() { return vitaminc; } } Parsed in 0.011 seconds, using GeSHi 1.0.8.4
Nothing changed here...
Next is the class helping us handling the DB:
DBhelper.javaUsing java Syntax Highlighting
package de.padde.fruitsdbsample; import java.io.ByteArrayOutputStream; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.graphics.Bitmap; import android.provider.BaseColumns; public class DBhelper { public static final String KEY_ID = BaseColumns._ID; public static final String KEY_NAME = "name"; public static final String KEY_KCAL = "kcal"; public static final String KEY_VC = "vitaminc"; public static final String KEY_IMG = "image"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; private static final String DATABASE_NAME = "FruitDB"; private static final int DATABASE_VERSION = 1; public static final String FRUITS_TABLE = "fruits"; private static final String CREATE_FRUITS_TABLE = "create table "+FRUITS_TABLE+" (" +KEY_ID+" integer primary key autoincrement, " +KEY_IMG+" blob not null, " +KEY_NAME+" text not null unique, " +KEY_KCAL+" integer not null, " +KEY_VC+" integer not null);"; private final Context mCtx; private boolean opened = false; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_FRUITS_TABLE); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS "+FRUITS_TABLE); onCreate(db); } } public void Reset() { openDB(); mDbHelper.onUpgrade(this.mDb, 1, 1); closeDB(); } public DBhelper(Context ctx) { mCtx = ctx; mDbHelper = new DatabaseHelper(mCtx); } private SQLiteDatabase openDB() { if(!opened) mDb = mDbHelper.getWritableDatabase(); opened = true; return mDb; } public SQLiteDatabase getHandle() { return openDB(); } private void closeDB() { if(opened) mDbHelper.close(); opened = false; } public void createFruitEntry(Fruit fruit) { openDB(); ByteArrayOutputStream out = new ByteArrayOutputStream(); fruit.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out); ContentValues cv = new ContentValues(); cv.put(KEY_IMG, out.toByteArray()); cv.put(KEY_NAME, fruit.getName()); cv.put(KEY_KCAL, fruit.getKcal()); cv.put(KEY_VC, fruit.getVitaminC()); mDb.insert(FRUITS_TABLE, null, cv); closeDB(); } } Parsed in 0.015 seconds, using GeSHi 1.0.8.4
Nothing essential changed here...
Following class is new and defines how to bind the data to the Views.
Out of the box SimpleCursorAdapter supports TextView and ImageView but
images have to be in form of a image ressource or a URI. Therefore we need
this class to override default binding and/or define how to bind to other view types.
FruitViewBinder.javaUsing java Syntax Highlighting
package de.padde.fruitsdbsample; import android.database.Cursor; import android.graphics.BitmapFactory; import android.view.View; import android.widget.ImageView; import android.widget.RatingBar; import android.widget.SimpleCursorAdapter; public class FruitViewBinder implements SimpleCursorAdapter.ViewBinder { public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if(view instanceof ImageView) { ImageView iv = (ImageView) view; byte[] img = cursor.getBlob(columnIndex); iv.setImageBitmap(BitmapFactory.decodeByteArray(img, 0, img.length)); return true; } if(view instanceof RatingBar) { RatingBar rb = (RatingBar) view; int kcal = cursor.getInt(cursor.getColumnIndex(DBhelper.KEY_KCAL)); int vitc = cursor.getInt(cursor.getColumnIndex(DBhelper.KEY_VC)); rb.setRating((float) (kcal * ((float) vitc/1000.0))); return true; } return false; } } Parsed in 0.012 seconds, using GeSHi 1.0.8.4
Last class is the ListActivity that implements the example
FruitDBSampleUsing java Syntax Highlighting
package de.padde.fruitsdbsample; import android.app.ListActivity; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.SimpleCursorAdapter; public class FruitsDBSample extends ListActivity { private DBhelper mDB; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDB = new DBhelper(this); mDB.Reset(); Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.icon); mDB.createFruitEntry(new Fruit(img, "Banane", 92, 10)); mDB.createFruitEntry(new Fruit(img, "Kiwi", 56, 71)); mDB.createFruitEntry(new Fruit(img, "Pfirsich", 41, 10)); mDB.createFruitEntry(new Fruit(img, "Zitrone", 40, 51)); String[] columns = {mDB.KEY_ID, mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_KCAL, mDB.KEY_VC}; String table = mDB.FRUITS_TABLE; Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null); startManagingCursor(c); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.fruitlist, c, new String[] {mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_KCAL, mDB.KEY_VC}, new int[] {R.id.img, R.id.txt, R.id.rating}); adapter.setViewBinder(new FruitViewBinder()); setListAdapter(adapter); } } Parsed in 0.012 seconds, using GeSHi 1.0.8.4
And last but not least the xml that defines the layout of a row in the final list
fruitlist.xmlUsing xml Syntax Highlighting
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id = "@+id/img" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentLeft = "true"> </ImageView> <TextView android:id = "@+id/txt" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerVertical = "true" android:layout_toRightOf = "@id/img"> </TextView> <RatingBar style="?android:attr/ratingBarStyleSmall" android:id = "@+id/rating" android:numStars = "5" android:stepSize = "0.5" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_alignParentRight = "true" android:layout_centerVertical = "true" android:layout_marginRight = "5px"> </RatingBar> <TextView android:text = "Healthindex: " android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerVertical = "true" android:layout_toLeftOf = "@id/rating"> </TextView> </RelativeLayout> Parsed in 0.002 seconds, using GeSHi 1.0.8.4
This is all pretty straight forward so no further explanation at this point.. but should something
remain unclear feel free to ask.
Greets Padde
--- Edit ---
And this is how it looks like.
padde wrote:What is missing? And what version of android do you target with your app?
byte[] avatarBytes = vCard.getAvatar();
icon = BitmapFactory.decodeByteArray(avatarBytes, 0, avatarBytes.length);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
ContentValues cv = new ContentValues();
cv.put("avatar", outputStream.toByteArray());
mDbHelper.open();
database.addAvatar(uid, cv, hash);
mDbHelper.close();
public class database {
private DatabaseHelper mDbHelper;
private static SQLiteDatabase mDb;
private static final String DATABASE_NAME = "avatardatabase";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
ns
db.execSQL("create table avatars (_id integer primary key autoincrement, userid text not null, avatar blob, avatar_hash text not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS avatars");
onCreate(db);
}
}
public databasecheck(Context ctx) {
this.mCtx = ctx;
}
public databasecheck open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
}
public static void addAvatar(String uid, ContentValues avatar, String hash) {
String sqlstring = "insert into avatars (userid, avatar, avatar_hash) values ('" + uid + "', '" + avatar + "', '" + hash + "');";
mDb.execSQL(sqlstring);
}
}
Return to Networking & Database Problems
Users browsing this forum: No registered users and 2 guests