main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
fruitlist.xml
<?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>
<TextView
android:text = "Healthindex: "
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_centerVertical = "true"
>
</TextView>
</RelativeLayout>
customimagelistview.java
package com.customimagelistview;
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 customimagelistview extends ListActivity {
private Dbhelper mDB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDB=new Dbhelper(this);
mDB.Reset();
Bitmap img=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
mDB.createFruitEntry(new Fruit(img, "Girish", "A'bad", 22));
mDB.createFruitEntry(new Fruit(img, "Poojan", "A'bad", 22));
mDB.createFruitEntry(new Fruit(img, "Rakesh Bhai", "A'bad", 22));
mDB.createFruitEntry(new Fruit(img, "Jano", "Patan", 22));
mDB.createFruitEntry(new Fruit(img, "Shivo", "Patan", 22));
String[] columns={mDB.KEY_ID,mDB.KEY_IMAGE,mDB.KEY_NAME,mDB.KEY_ADD,mDB.KEY_AGE};
//String table=mDB.FRUITS_TABLE;
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_ID,mDB.KEY_IMAGE,mDB.KEY_NAME,mDB.KEY_ADD,mDB.KEY_AGE},
new int[] {R.id.img,R.id.txt});
adapter.setViewBinder(new FruitViewBinder());
setListAdapter(adapter);
}
}
FruitsDBSample.java
package com.customimagelistview;
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, "Girish", "A'bad", 22));
mDB.createFruitEntry(new Fruit(img, "Poojan", "A'bad", 22));
mDB.createFruitEntry(new Fruit(img, "Rakesh Bhai", "A'bad", 22));
mDB.createFruitEntry(new Fruit(img, "Jano", "Patan", 22));
mDB.createFruitEntry(new Fruit(img, "Shivo", "Patan", 22));
String[] columns={mDB.KEY_ID,mDB.KEY_IMAGE,mDB.KEY_NAME,mDB.KEY_ADD,mDB.KEY_AGE};
//String table=mDB.FRUITS_TABLE;
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_ID,mDB.KEY_IMAGE,mDB.KEY_NAME,mDB.KEY_ADD,mDB.KEY_AGE},
new int[] {R.id.img,R.id.txt});
adapter.setViewBinder(new FruitViewBinder());
setListAdapter(adapter);
}
}
Dbhelper.java
package com.customimagelistview;
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_IMAGE="image";
public static final String KEY_ADD="address";
public static final String KEY_AGE="age";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME="FruitDB";
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_IMAGE+" blob not null,"+KEY_NAME+" text not null,"+KEY_ADD+" text not null,"+KEY_AGE+" integer not null)";
private boolean opened=false;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
DatabaseHelper(Context ctx){
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_FRUITS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
// TODO Auto-generated method stub
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)
mDbHelper.close();
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_IMAGE, out.toByteArray());
cv.put(KEY_IMAGE, fruit.getName());
cv.put(KEY_ADD, fruit.getAdd());
cv.put(KEY_AGE, fruit.getAge());
mDb.insert(FRUITS_TABLE, null, cv);
closeDB();
}
}
Fruit.java
package com.customimagelistview;
import android.graphics.Bitmap;
public class Fruit {
private Bitmap bmp;
private String name;
private String address;
private int age;
public Fruit(Bitmap b,String n,String ad,int ag){
bmp=b;
name=n;
address=ad;
age=ag;
}
public Bitmap getBitmap(){return bmp;}
public String getName(){return name;}
public String getAdd(){return address;}
public int getAge(){return age;}
}
FruitViewBinder.java
package com.customimagelistview;
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{
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
// TODO Auto-generated method stub
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;
String name=cursor.getString(cursor.getColumnIndex(Dbhelper.KEY_NAME));
String address=cursor.getString(cursor.getColumnIndex(Dbhelper.KEY_ADD));
int age=cursor.getInt(cursor.getColumnIndex(Dbhelper.KEY_AGE));
return true;
}
return false;
}
}
No comments:
Post a Comment