第一个问题:我在另一个名为DatabaseHelper的活动上创建了数据库,所以这是另一个活动。我搜索了但听不懂,但我认为我没有连接到数据库。如何从另一个活动连接到数据库?
第二个问题:我正在尝试使用Edittext搜索数据库并接收多个数据(例如产品名称和编号)到另一个edittext。我的目标是使用条形码扫描仪设备。它的功能是读取,写入和按Enter键。因此,当设备扫描时,光标跳入另一个edittext。为了有效使用功能,我想在按Enter键时搜索数据库。我已经在C#上做到了,但是在Android上却做不到,这是我的新手。有人可以帮忙吗?
公共类ProductSearch扩展了AppCompatActivity {
public static final String DATABASE_NAME = "Product.db";
public static final String TABLE_NAME = "product_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "ProductCode";
public static final String COL_3 = "ProductName";
public static final String COL_4 = "Number";
EditText editproductcode,editproductname,editnumber;
SQLiteDatabase db;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_search);
editproductcode = (EditText)findViewById(R.id.searchproductcode);
editproductname = (EditText)findViewById(R.id.searchname);
editnumber = (EditText)findViewById(R.id.searchnumber);
db = this.openOrCreateDatabase(DATABASE_NAME,MODE_PRIVATE,null);
editproductcode.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction()==KeyEvent.ACTION_DOWN)&& (keyCode==KeyEvent.KEYCODE_ENTER)) {
try {
cursor = db.rawQuery(" SELECT * FROM '" + TABLE_NAME + "' WHERE '" + editproductcode.toString() + "'",null);
int productnameIx = cursor.getColumnIndex(COL_3);
int productnumberIx = cursor.getColumnIndex(COL_4);
if (cursor != null && cursor.moveToFirst()){
editproductname.setText(cursor.getString(productnameIx));
editnumber.setText(cursor.getString(productnumberIx));
cursor.close();
}
}catch (Exception e){
e.printStackTrace();
}
return false;
}
});
}
首先,您像这样在数据库助手中创建了一个方法。
//Insert Data to SQLite database
public boolean insertData(String first_name, String surname) { //required parameters
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("first_name", first_name);
contentValues.put("surname", surname);
db.insert("User_info", null, contentValues); //User_info is name of the table
return true;
}
以及您如何在MainActivity中调用此方法
new DatabaseHelper(MainActivity.this).insertData(first_name, surname); // supply the needed parameters
(那是问题:1)
关于第二个问题,您需要像这样在databasehelper类中创建搜索和检索方法。
//Get all item in your database public ArrayList<HashMap<String, String>> getAllDatabaseData(String id) { // we use the id to find it corresponding user details SQLiteDatabase db = this.getReadableDatabase(); Cursor res = db.rawQuery( "select * from User_info where id = " + id + "", null ); res.moveToFirst(); while(res.isAfterLast() == false){ HashMap<String, String> array_list = new HashMap<>(); array_list.put("id",res.getString(res.getColumnIndex(COLUMN_ID))); array_list.put("first_name",res.getString(res.getColumnIndex(COLUMN_FIRSTNAME))); array_list.put("surname",res.getString(res.getColumnIndex(COLUMN_SURNAME))); YourList.add(array_list); res.moveToNext(); } return YourList; //it will return all item result of the search query }
以及您如何在MainActivity中访问此方法。 (将其放在您的button.onClickListener上)会触发搜索查询的按钮
ArrayList<HashMap<String, String >>DataList = new ArrayList<>();
DataList = new DbHelper(MainActivity.this).getAllDatabaseData(id); // supply the need id
这就是您提供EditText的方式。(也在上面的代码下方的onclicklistener按钮上)
your_edittext.setText(DataList.get(0).get("information1");) //first row in your arraylist, field name "information1" (change information1 and your_edittext to your fields and edittext)