我正在android studio中开发一个应用程序,该应用程序是条形码扫描。通常,您会选择复选框活动中的过敏反应,并且当您扫描产品时,应用程序会告诉您是否可以食用。但是我不知道怎么说:如果扫描结果在过敏数据库中,您可以吃也可以不吃。基本上,我不知道如何使用数据库和复选框活动来连接de Scanner。
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class Database extends SQLiteOpenHelper {
private static final String SQL_CREATE_CONFIG =
"CREATE TABLE " + Configuracio.TABLE_NAME + " (" +
Configuracio._ID + " INTEGER PRIMARY KEY," +
Configuracio.LACTOSA + " INTEGER," +
Configuracio.GLUTEN + " INTEGER," +
Configuracio.NOUS + " INTEGER," +
Configuracio.OU + " INTEGER," +
Configuracio.CACAUETS + " INTEGER)";
private static final String SQL_DELETE_CONFIG =
"DROP TABLE IF EXISTS " + Configuracio.TABLE_NAME;
private static final String SQL_INSERT_CONFIG =
"INSERT INTO " + Configuracio.TABLE_NAME + " (" +
Configuracio.LACTOSA + ", " +
Configuracio.GLUTEN + ", " +
Configuracio.NOUS + ", " +
Configuracio.OU + ", " +
Configuracio.CACAUETS + " ) VALUES (" +
0 + ", " +
0 + ", " +
0 + ", " +
0 + ", " +
0 + ")";
private static final String SQL_CREATE_CODI =
"CREATE TABLE " + CodisDeBarra.TABLE_NAME + " (" +
CodisDeBarra._ID + " INTEGER PRIMARY KEY," +
CodisDeBarra.COLUMN_PRODUCT + " INTEGER," +
CodisDeBarra.COLUMN_CODI_DE_BARRES + " INTEGER," +
CodisDeBarra.COLUMN_LACTOSA + " INTEGER," +
CodisDeBarra.COLUMN_GLUTEN + " INTEGER," +
CodisDeBarra.COLUMN_NOUS + " INTEGER," +
CodisDeBarra.COLUMN_OU + " INTEGER," +
CodisDeBarra.COLUMN_CACAUETS + " INTEGER)";
private static final String SQL_DELETE_CODI =
"DROP TABLE IF EXISTS " + CodisDeBarra.TABLE_NAME;
private static final String SQL_INSERT_CODI_LLET =
"INSERT INTO " + CodisDeBarra.TABLE_NAME + " (" +
CodisDeBarra.COLUMN_PRODUCT + ", " +
CodisDeBarra.COLUMN_CODI_DE_BARRES + ", " +
CodisDeBarra.COLUMN_LACTOSA + ", " +
CodisDeBarra.COLUMN_GLUTEN + ", " +
CodisDeBarra.COLUMN_NOUS + ", " +
CodisDeBarra.COLUMN_OU + ", " +
CodisDeBarra.COLUMN_CACAUETS + " ) VALUES (" +
"0" + ", " +
"8431876100785" + ", " +
0 + ", " +
0 + ", " +
0 + ", " +
0 + ", " +
0 + ")";
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_CONFIG);
db.execSQL(SQL_CREATE_CODI);
db.execSQL(SQL_INSERT_CONFIG);
db.execSQL(SQL_INSERT_CODI_LLET);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_CONFIG);
db.execSQL(SQL_DELETE_CODI);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
public void updateConfig (int lactosa, int gluten, int nous, int ou, int cacauets) {
}
public long selectLactosaConfig() {
SQLiteDatabase db = getReadableDatabase();
String[] projection = {
Configuracio.LACTOSA
};
Cursor cursor = db.query(
Configuracio.TABLE_NAME, // The table to query
projection, // The array of columns to return (pass null to get all)
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
List itemIds = new ArrayList<>();
while(cursor.moveToNext()) {
long itemId = cursor.getInt(
cursor.getColumnIndexOrThrow(Configuracio.LACTOSA));
itemIds.add(itemId);
}
cursor.close();
return (long) itemIds.get(0);
}
public long selectGlutenConfig() {
SQLiteDatabase db = getReadableDatabase();
String[] projection = {
Configuracio.GLUTEN
};
Cursor cursor = db.query(
Configuracio.TABLE_NAME, // The table to query
projection, // The array of columns to return (pass null to get all)
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
List itemIds = new ArrayList<>();
while(cursor.moveToNext()) {
long itemId = cursor.getInt(
cursor.getColumnIndexOrThrow(Configuracio.GLUTEN));
itemIds.add(itemId);
}
cursor.close();
return (long) itemIds.get(0);
}
public long selectNousConfig() {
SQLiteDatabase db = getReadableDatabase();
String[] projection = {
Configuracio.NOUS
};
Cursor cursor = db.query(
Configuracio.TABLE_NAME, // The table to query
projection, // The array of columns to return (pass null to get all)
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
List itemIds = new ArrayList<>();
while(cursor.moveToNext()) {
long itemId = cursor.getInt(
cursor.getColumnIndexOrThrow(Configuracio.NOUS));
itemIds.add(itemId);
}
cursor.close();
return (long) itemIds.get(0);
}
public long selectOuConfig() {
SQLiteDatabase db = getReadableDatabase();
String[] projection = {
Configuracio.OU
};
Cursor cursor = db.query(
Configuracio.TABLE_NAME, // The table to query
projection, // The array of columns to return (pass null to get all)
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
List itemIds = new ArrayList<>();
while(cursor.moveToNext()) {
long itemId = cursor.getInt(
cursor.getColumnIndexOrThrow(Configuracio.OU));
itemIds.add(itemId);
}
cursor.close();
return (long) itemIds.get(0);
}
public long selectCacauetsConfig() {
SQLiteDatabase db = getReadableDatabase();
String[] projection = {
Configuracio.CACAUETS
};
Cursor cursor = db.query(
Configuracio.TABLE_NAME, // The table to query
projection, // The array of columns to return (pass null to get all)
null, // The columns for the WHERE clause
null, // The values for the WHERE clause
null, // don't group the rows
null, // don't filter by row groups
null // The sort order
);
List itemIds = new ArrayList<>();
while(cursor.moveToNext()) {
long itemId = cursor.getInt(
cursor.getColumnIndexOrThrow(Configuracio.CACAUETS));
itemIds.add(itemId);
}
cursor.close();
return (long) itemIds.get(0);
}
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
public class CheckboxActivity extends AppCompatActivity {
Database database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
database = new Database(getApplicationContext());
CheckBox gluten = findViewById(R.id.checkboxGluten);
long glutenVal = database.selectGlutenConfig();
if (glutenVal == 0) {
gluten.setChecked(false);
} else {
gluten.setChecked(true);
}
database = new Database(getApplicationContext());
CheckBox lactosa = findViewById(R.id.checkboxLactosa);
long lactosaVal = database.selectLactosaConfig();
if (lactosaVal == 0) {
lactosa.setChecked(false);
} else {
lactosa.setChecked(true);
}
database = new Database(getApplicationContext());
CheckBox nous = findViewById(R.id.checkboxNous);
long nousVal = database.selectNousConfig();
if (nousVal == 0) {
nous.setChecked(false);
} else {
nous.setChecked(true);
}
database = new Database(getApplicationContext());
CheckBox ou = findViewById(R.id.checkboxOu);
long ouVal = database.selectOuConfig();
if (ouVal == 0) {
ou.setChecked(false);
} else {
ou.setChecked(true);
}
database = new Database(getApplicationContext());
CheckBox cacauets = findViewById(R.id.checkboxCacauets);
long cacauetsVal = database.selectCacauetsConfig();
if (cacauetsVal == 0) {
cacauets.setChecked(false);
} else {
cacauets.setChecked(true);
}
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
import static android.Manifest.permission.CAMERA;
public class Scanner extends Activity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView scannerView;
private boolean grantResults;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (checkPermission ())
{
Toast.makeText(Scanner.this, "Permission is granted!", Toast.LENGTH_LONG).show();
}
else
{
requestPermission();
}
}
}
private boolean checkPermission ()
{
return (ContextCompat.checkSelfPermission(Scanner. this, CAMERA) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission()
{
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
}
public void onRequestPermissionResult(int requestCode, String permission[] , int grantResults[])
{
switch (requestCode)
{
case REQUEST_CAMERA :
if (grantResults.length > 0)
{
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted)
{
Toast.makeText(Scanner.this, "Permission Granted", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(Scanner.this, "Permission Denied", Toast.LENGTH_LONG).show();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(shouldShowRequestPermissionRationale(CAMERA))
{
displayAlertMessage("Yoou need to allow access for both permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{CAMERA} , REQUEST_CAMERA);
}
}
});
return;
}
}
}
}
break;
}
}
@Override
public void onResume()
{
super.onResume();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if(checkPermission())
{
if(scannerView == null)
{
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
}
scannerView.setResultHandler(this);
scannerView.startCamera();
}
else
{
requestPermission();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
scannerView.stopCamera();
}
public void displayAlertMessage(String message, DialogInterface.OnClickListener listener)
{
new AlertDialog.Builder(Scanner.this)
.setMessage(message)
.setPositiveButton("OK", listener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void handleResult(final Result result) {
final String scanResult = result.getText();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("SUITABLE");
builder.setPositiveButton("SCAN", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
scannerView.resumeCameraPreview(Scanner.this);
}
});
builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse(scanResult));
startActivity(intent);
}
});
builder.setMessage(scanResult);
AlertDialog alert = builder.create();
alert.show();
}
}