我可以保存4个值但我只检索字符串
public class CafeteriaDB extends SQLiteOpenHelper {
private static final String NOMBRE_DB = "cafeteria.db";
private static final int VERSION_DB = 1;
private static final String TABLA_BEBIDAS = "CREATE TABLE BEBIDAS(NOMBRE TEXT, PRECIO TEXT, INFO TEXT, FOTO BLOB)";
public CafeteriaDB(@Nullable Context context) {
super(context, NOMBRE_DB, null, VERSION_DB);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLA_BEBIDAS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLA_BEBIDAS);
db.execSQL(TABLA_BEBIDAS);
}
public void agregarProducto(String nombre, String precio, String info, byte[] foto){
SQLiteDatabase db=getWritableDatabase();
if(db!=null){
if(info.equals("Bebidas")){
db.execSQL("INSERT INTO BEBIDAS VALUES('"+nombre+"','"+precio+"','"+info+"','"+foto+"')");
db.close();
}
}
}
public ArrayList<CarritoVo> mostrarBebidas(){
SQLiteDatabase db=getReadableDatabase();
Cursor cursor= db.rawQuery("SELECT * FROM BEBIDAS",null);
ArrayList<CarritoVo> bebidas=new ArrayList<>();
if(cursor.moveToFirst()){
do{
bebidas.add(new CarritoVo(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getBlob(3)));
}while(cursor.moveToNext());
}
return bebidas;
}
public void eliminarProducto(String nombre, String info){
SQLiteDatabase db=getWritableDatabase();
if(db != null){
if(info.equals("Bebidas")){
db.execSQL("DELETE FROM BEBIDAS WHERE NOMBRE='"+nombre+"'");
db.close();
}
}
}
}
Button btnCarrito = (Button)myDialog.findViewById(R.id.btnCarrito);
btnCarrito.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
cafeteriaDB.agregarProducto(
dialogName.getText().toString().trim(),
dialogCantidad.getText().toString().trim(),
dialogInfo.getText().toString().trim(),
imageViewToByte(dialog_foto));
Toast.makeText(parent.getContext(),"Producto Añadido Correctamente",Toast.LENGTH_SHORT).show();
}
catch (Exception e){
e.printStackTrace();
}
}
});
myDialog.show();
}
});
return myHolder;
}
private static byte[] imageViewToByte(ImageView dialog_foto) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable)dialog_foto.getDrawable()).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG,100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
emphasized text
@Override
public void onBindViewHolder(@NonNull ViewHolderCarrito holder, int position) {
byte[] foto =listaCarrito.get(position).getFoto();
ByteArrayInputStream fotoStream = new ByteArrayInputStream(foto);
Bitmap thefoto = BitmapFactory.decodeByteArray(foto,0,foto.length);
holder.etiFoto.setImageBitmap(thefoto);
holder.etiNombre.setText(listaCarrito.get(position).getNombre());
holder.etiPrecio.setText(listaCarrito.get(position).getPrecio());
holder.etiInfo.setText(listaCarrito.get(position).getInfo());
}
}
最佳答案
插入方法:-
public void agregarProducto(String nombre, String precio, String info, byte[] foto){
SQLiteDatabase db=getWritableDatabase();
if(db!=null){
if(info.equals("Bebidas")){
db.execSQL("INSERT INTO BEBIDAS VALUES('"+nombre+"','"+precio+"','"+info+"','"+foto+"')");
db.close();
}
}
}
不会插入 BLOB,它可能会插入 byte[] 指针的值。
BLOB 必须指定为 x'??????????'
,其中问号是数组中字节的 HEX 表示形式。例如x'00F1F2F3'。
将字节数组转换为合适格式的简单方法是让 SQLiteDatabase 便捷方法 insert代表您进行转换。
因此将上述方法更改为:-
public long agregarProducto(String nombre, String precio, String info, byte[] foto){
SQLiteDatabase db=getWritableDatabase();
ContentValues cv = new ContentValues();
if(db!=null){
if(info.equals("Bebidas")){
cv.put("NOMBRE",nombre);
cv.put("PRECIO",precio);
cv.put("INFO",info);
cv.put("FOTO",foto);
db.insert("BEBIDAS",null,cv);
db.close();
}
}
}
- 请注意,该方法将返回一个 long,如果插入有效,它将返回 > 0,否则返回 -1,表示未插入任何内容。
关于java - 如何在 android studio 中从 SQLite 检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58809825/