In AddClaims
, I save ListView
(with data and image) into SQLite database.
AddClaims
ArrayList<ImageAndText> images=new ArrayList<ImageAndText>();
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { // button is clicked
SB.insertStaffBenefit(images, a); // SB is the object of StaffAPI
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
StaffAPI
public long insertStaffBenefit(ArrayList<ImageAndText> imageText,long id)
{
id1=id;
database=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
for(ImageAndText i:imageText) {
String description=i.getDescription();
Bitmap image=i.getImage();
byte[]data=getBitmapAsByteArray(image);
values.put(MydatabaseHelper.Description,description);
values.put(MyDatabaseHelper.Image, data);
database.insert(MyDatabaseHelper.TABLE_STAFF_BENEFIT, null, values);
}
database.close();
return 0 ;
}
public static byte[] getBitmapAsByteArray(Bitmap bitmap)
{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
Log.e("TAG", "Blob size: " + outputStream.size() / 1024 + " KB");
return outputStream.toByteArray();
}
I not sure whether this is the correct way to save the image in listView
, but it can display the Blob
size. So I assume the image can be saved.
Next, I wanted to retrieve all the image and loaded to ListView
in EditStaff
EditStaff
public void BuildEditStaffList(long id)
{
Log.e("S", id+"");
sqlcon.open();
Cursor cursor1=sqlcon.readName(id);
String[] columns=new String[]{
MydatabaseHelper.image,MyDatabaseHelper.Description};
int[] to=new int[]
{
R.id.image,R.id.description};
dataAdapter = new SimpleCursorAdapter(getActivity(), R.layout.retrieve_staff,
cursor1,
columns,
to,
0);
listViewEdit.setAdapter(dataAdapter);
}
StaffAPI
public Cursor readName(long id)
{
Log.e("ID",id+"");
Cursor c=database.rawQuery(" SELECT _id, Image, Description FROM " + MyDatabaseHelper.TABLE_STAFF_BENEFIT + " WHERE Ts_id = ?", new String[]{String.valueOf(id)}, null);
if (c != null) {
c.moveToFirst();
}
Log.e("ID",id+"");
return c;
}
The app crashed when I try to retrieve the image and text to ListView EditStaff. I check the logCat, but it only throw me
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41e46c80)
After I remove the MydatabaseHelper.image
and R.id.image
in EditStaff, the text can be displayed on listView
. So I guess the app crashed is because of the image. Is there any way to check whether the image is inserted correctly in SQLite
? Thanks
Aucun commentaire:
Enregistrer un commentaire