tags:

views:

308

answers:

1

hai , I am new to android . I have proble .

this is my code but it will not work , the problem is in view binder .

please currect it .

// this is my activity

package com.android.Fruits2;

import java.util.ArrayList; import java.util.HashMap;

import android.app.ListActivity; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.SimpleAdapter; import android.widget.SimpleCursorAdapter; import android.widget.SimpleAdapter.ViewBinder;

public class Fruits2 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.createPersonEntry(new PersonData(img, "Harsha", 24,"mca"));


        String[] columns = {mDB.KEY_ID, mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_AGE, mDB.KEY_STUDY};
        String   table   = mDB.PERSON_TABLE;

        Cursor c = mDB.getHandle().query(table, columns, null, null, null, null, null);

        startManagingCursor(c);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.data,
                c,
                new String[] {mDB.KEY_IMG, mDB.KEY_NAME, mDB.KEY_AGE, mDB.KEY_STUDY},
                new int[] {R.id.img, R.id.name, R.id.age,R.id.study});   


        adapter.setViewBinder( new MyViewBinder());

        setListAdapter(adapter);
}

}

//my viewbinder

package com.android.Fruits2;

import android.database.Cursor; import android.graphics.BitmapFactory; import android.view.View; import android.widget.ImageView; import android.widget.SimpleCursorAdapter;

public class MyViewBinder 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;
        }

    return false;
}

}

// data

package com.android.Fruits2;

import android.graphics.Bitmap;

public class PersonData { private Bitmap bmp; private String name; private int age; private String study;

    public PersonData(Bitmap b, String n, int k, String v) {
        bmp = b;
        name = n;
        age = k;
        study = v;
    }

    public Bitmap getBitmap() { return bmp; }
    public String getName() { return name; }
    public int getAge() { return age; }
    public String getStudy() { return study; }

}

//dbhelper

package com.android.Fruits2;

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_AGE = "age"; public static final String KEY_STUDY = "study"; public static final String KEY_IMG = "image";

private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;  

private static final String DATABASE_NAME = "PersonalDB";
private static final int DATABASE_VERSION = 1;

public static final String PERSON_TABLE = "Person";

private static final String CREATE_PERSON_TABLE = "create table "+PERSON_TABLE+" ("
                                     +KEY_ID+" integer primary key autoincrement, "
                                     +KEY_IMG+" blob not null, "
                                     +KEY_NAME+" text not null , "
                                     +KEY_AGE+" integer not null, "
                                     +KEY_STUDY+" text 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_PERSON_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+PERSON_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 createPersonEntry(PersonData about) {
 openDB();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    about.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out);
    ContentValues cv = new ContentValues();
    cv.put(KEY_IMG, out.toByteArray());            
    cv.put(KEY_NAME, about.getName());
    cv.put(KEY_AGE, about.getAge());
    cv.put(KEY_STUDY, about.getStudy());
    mDb.insert(PERSON_TABLE, null, cv);
    closeDB();
}

}

//data.xml

<ImageView
    android:id = "@+id/img"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
   >
</ImageView>

<TextView
    android:id = "@+id/name"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:textSize="15dp"
    android:textColor="#ff0000" 
  >
</TextView>

<TextView

    android:id = "@+id/age"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:textSize="15dp"
    android:textColor="#ff0000" 
    />


<TextView
    android:id = "@+id/study"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:textSize="15dp"
    android:textColor="#ff0000" 

/>

when i run this in android 1.6 and 2.1 , it works

but when i run in android 1.5 , not work .

my application is android 1.5

please currect and send code to me

Thank you.

A: 

If you have a large amount of binary data to add, it's not good to put in the in the row. But you have to insert in the row a link to a file. Android manage that like the following example:

Example from Android Documentation:

import android.provider.MediaStore.Images.Media;
import android.content.ContentValues;
import java.io.OutputStream;

// Save the name and description of an image in a ContentValues map.  
ContentValues values = new ContentValues(3);
values.put(Media.DISPLAY_NAME, "road_trip_1");
values.put(Media.DESCRIPTION, "Day 1, trip to Los Angeles");
values.put(Media.MIME_TYPE, "image/jpeg");

// Add a new record without the bitmap, but with the values just set.
// insert() returns the URI of the new record.
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);

// Now get a handle to the file for that record, and save the data into it.
// Here, sourceBitmap is a Bitmap object representing the file to save to the database.
try {
    OutputStream outStream = getContentResolver().openOutputStream(uri);
    sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);
    outStream.close();
} catch (Exception e) {
    Log.e(TAG, "exception while writing image", e);
}

Hope this help.

Bye.

Guaido79
please send full source code about that example .
hany
this code, like I said was got from Android documentation. Which part need explaination? The code is suppose to be called from an Activity class. The method "getContentResolver()" to work need to implements your own custom ContentProvider, and declare it in the AndroidManifest.xml file. To get more information about ContentProvider go to here: http://developer.android.com/guide/topics/providers/content-providers.htmlRegards
Guaido79