问题描述
我正在制作一个带有菜单列表(名称、描述、价格、图像)的送餐应用。将名称、描述和价格添加到数据库中很容易,但如何添加图像。
更具体地说,我想使用 ImageButton(单击时)打开手机的文件管理器,然后从那里选择图像,让它显示在 ImageButton 上,然后单击 ADD 按钮将其插入到数据库中。我想知道是否还有其他更简单的方法可以做到这一点。
menu_adding.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ListView
android:id="@+id/menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:mode="twoLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</ListView>
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="220dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ImageView
android:id="@+id/add_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:padding="5dp" />
<EditText
android:id="@+id/add_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name"
android:padding="5dp"
android:layout_toRightOf="@id/add_image" />
<EditText
android:id="@+id/add_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Description"
android:padding="5dp"
android:layout_toRightOf="@id/add_image"
android:layout_below="@id/add_name"/>
<EditText
android:id="@+id/add_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Price"
android:padding="5dp"
android:layout_toRightOf="@+id/add_image"
android:layout_below="@+id/add_desc"/>
<Button
android:id="@+id/add_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:padding="5dp"
android:layout_toEndOf="@id/add_price"
android:layout_below="@id/add_desc"
android:layout_marginLeft="15dp"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MenuDatabaseHelper.java
public class MenuDatabaseHelper extends sqliteOpenHelper {
sqliteDatabase db;
List<Menu> menuList = new ArrayList<>();
private static final String MENU_DB = "MenuDB.db";
private static final String MENU_TABLE = "Menu_Table";
//column
private static final String ID = "ID";
//private static final String MENU_IMAGE = "Menu_Image";
private static final String MENU_NAME = "Menu_Name";
private static final String MENU_DESC = "Menu_Desc";
private static final String MENU_PRICE = "Menu_Price";
private static final String CREATE_MENU_TABLE = "CREATE TABLE " + MENU_TABLE +
" ( ID INTEGER PRIMARY KEY AUTOINCREMENT," +
//MENU_IMAGE + " BLOB," +
MENU_NAME + " TEXT," +
MENU_DESC + " TEXT," +
MENU_PRICE + " TEXT ) " ;
public MenuDatabaseHelper(Context context){super(context,MENU_DB,null,1);}
@Override
public void onCreate(sqliteDatabase sqliteDatabase) {
sqliteDatabase.execsql(CREATE_MENU_TABLE);
}
@Override
public void onUpgrade(sqliteDatabase sqliteDatabase,int i,int i1) {
sqliteDatabase.execsql("DROP TABLE IF EXISTS " + MENU_TABLE);
onCreate(sqliteDatabase);
}
//create method to insert data
public boolean insertMenuData(String Menu_Name,String Menu_Desc,String Menu_Price){
sqliteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(MENU_IMAGE,Menu_Image);
contentValues.put(MENU_NAME,Menu_Name);
contentValues.put(MENU_DESC,Menu_Desc);
contentValues.put(MENU_PRICE,Menu_Price);
long result = db.insert(MENU_TABLE,contentValues);
return result != -1; //if result = -1 data doesn't insert
}
}
MenuAdding.java
public class MenuAdding extends AppCompatActivity {
MenuDatabaseHelper db;
Button add_data;
EditText add_name;
EditText add_desc;
EditText add_price;
ImageView add_image;
ListView menu_list;
ArrayList<String> listItem;
ArrayList<String> listItem2;
ArrayAdapter adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_adding);
db = new MenuDatabaseHelper(MenuAdding.this);
add_data = findViewById(R.id.add_data);
add_image = findViewById(R.id.add_image);
add_name = findViewById(R.id.add_name);
add_desc = findViewById(R.id.add_desc);
add_price = findViewById(R.id.add_price);
menu_list = findViewById(R.id.menu_list);
listItem = new ArrayList<>();
listItem2 = new ArrayList<>();
viewData();
menu_list.setonItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,View view,long l) {
String text = menu_list.getItemAtPosition(i).toString();
Toast.makeText(MenuAdding.this,""+text,Toast.LENGTH_SHORT).show();
}
});
add_data.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//String image = add_image.get
String name = add_name.getText().toString();
String desc = add_desc.getText().toString();
String price = add_price.getText().toString();
if (!name.equals("") && db.insertMenuData(name,desc,price)){
Toast.makeText(MenuAdding.this,"Data added.",Toast.LENGTH_SHORT).show();
add_name.setText("");
add_desc.setText("");
add_price.setText("");
listItem.clear();
viewData();
} else {
Toast.makeText(MenuAdding.this,"Data not added.",Toast.LENGTH_SHORT).show();
}
}
});
}
private void viewData() {
Cursor cursor = db.viewData();
if (cursor.getCount() == 0){
Toast.makeText(this,"No data to show",Toast.LENGTH_SHORT).show();
} else{
while (cursor.movetoNext()){
listItem.add(cursor.getString(1)); // index 1 is name,index 0 is ID
listItem2.add(cursor.getString(2));
}
adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,listItem);//create custom adapter
menu_list.setAdapter(adapter);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)