Android 如何将数据库中的值添加到按钮
我想做一个pos,我想在按钮中插入来自Sqlite数据库的数据。因此,当我点击按钮时,数据名称和价格显示出来Android 如何将数据库中的值添加到按钮,android,sqlite,button,point-of-sale,Android,Sqlite,Button,Point Of Sale,我想做一个pos,我想在按钮中插入来自Sqlite数据库的数据。因此,当我点击按钮时,数据名称和价格显示出来 首先,您应该在DatabaseHelper上创建一个接受插入数据的方法,如下所示 public boolean insertData(String name, String price, String type) { /required parameters SQLiteDatabase db = this.getWritableDatabase();
首先,您应该在DatabaseHelper上创建一个接受插入数据的方法,如下所示
public boolean insertData(String name, String price, String type) { /required parameters
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, name);
contentValues.put(COL_3, price);
contentValues.put(COL_4, type);
db.insert(Table_Product, null, contentValues);
return true;
}
然后,无论何时您想要添加数据,都可以在活动中这样调用它
new DatabaseHelper(MainActivity.this).insertData(name, price, type); //supply the parameters.
new DatabaseHelper(MainActivity.this).insertData(name, price, type); //supply the parameters.