BottomSheetDialogFragment was Called When i press the Add Button in My Main Activity , The BottomSheet Contain EditText and the ImageView ( The Image View from gridview )
The User enter the Edittext name and choose One Image in the Grid View and i want to Save the Edittext value and the User Selected Image into a New Table to create a Listview
I Dont Know How To Save the Selected Image in the SQLiteDatabase
Here is My Code
Database Helper
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();
}
}
}
BottomSheet
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;
}