【发布时间】:2020-08-23 17:10:14
【问题描述】:
当我在我的主活动中按下添加按钮时,BottomSheetDialogFragment 被调用,BottomSheet 包含 EditText 和 ImageView(来自 gridview 的图像视图)
用户输入 Edittext 名称并在网格视图中选择一个图像,我想将 Edittext 值和用户选择的图像保存到新表中以创建列表视图
我不知道如何将所选图像保存在 SQLiteDatabase 中
这是我的代码
数据库助手
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "slate.db";
public static final String TABLE_NAME ="listview_name";
public static final String COLUMN_ID="ID";
public static final String COLUMN_TITLE="ITEM_NAME";
private static final String COLUMN_IMAGE = "image_bitmap";;
private Context context;
public DatabaseHelper(Context context)
{
super(context,DATABASE_NAME,null,1);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query = "CREATE TABLE " +TABLE_NAME +"("+COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"+
COLUMN_TITLE + "TEXT," +COLUMN_IMAGE + " BLOB);";
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(sqLiteDatabase);
}
void createlist(String title, byte[] image)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("COLUMN_TITLE", title);
cv.put("COLUMN_IMAGE", image);
Long result = sqLiteDatabase.insert( TABLE_NAME, null, cv );
if (result==-1)
{
Toast.makeText(context, "Faild to create", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Created Sucessfully", Toast.LENGTH_SHORT).show();
}
}
}
底页
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_list_create, container, false);
ImageButton done = view.findViewById(R.id.done);
final EditText listname = (EditText) view.findViewById(R.id.listname);
final GridView gridView = (GridView) view.findViewById(R.id.gridview);
final MyCustomAdpter customAdpter = new MyCustomAdpter(images, getContext());
gridView.setAdapter(customAdpter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
customAdpter.selectedImage = i;
customAdpter.notifyDataSetChanged();
imageRes = images[i];
}
});
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseHelper myDB= new DatabaseHelper(getContext());
myDB.createlist(listname.getText().toString(),);//how to get the selected image and Display here to add the item or How to Convert the blob into String
String itemname = listname.getText().toString();
if (!TextUtils.isEmpty(listname.getText().toString())) {
startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image", imageRes));
dismiss();
} else {
Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
【问题讨论】:
标签: android android-sqlite android-gridview