Main Activity 中的 Spinner 数据在使用相机扫描 QR 码后丢失
Spinner data in Main Activity is lost after using camera for scanning a QR Code
嗨,我是 Android 编程新手。
加载 mainacitivity 后,我可以从 sql 服务器获取数据并将其填充到微调器中。我可以从微调器中选择一个项目并扫描二维码/条形码。一旦我停止相机并将 return 转到 MainActivity,我就无法在微调器中找到任何数据。如果您需要有关该问题的更多信息,请告诉我。我在 MainActivity.java 文件中提供了完整的代码。
package com.example.vxt.barcodescanner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;//import android.widget.ProgressBar;import android.widget.Spinner;import android.app.AlertDialog;import android.content.DialogInterface;import com.google.zxing.Result;import me.dm7.barcodescanner.zxing.ZXingScannerView;import java.sql.Connection;import java.sql.ResultSet;import java.sql.PreparedStatement;import java.util.ArrayList;import static com.example.vxt.barcodescanner.R.id.spinner;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
ConnectionClass connectionClass;
//ProgressBar pbbar;
Spinner spinnerProducts;
private ZXingScannerView mScannerView;
ArrayList<String> scanned_data = new ArrayList<String>();
ArrayList<String> data = new ArrayList<String>();
String productSelected;
Button scan, pushToDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectionClass = new ConnectionClass();
//pbbar = (ProgressBar) findViewById(R.id.pbbar);
//pbbar.setVisibility(View.VISIBLE);
spinnerProducts = (Spinner) findViewById(spinner);
scan = (Button) findViewById(R.id.button);
pushToDB = (Button) findViewById(R.id.button3);
try {
Connection con = connectionClass.CONN();
if (con != null) {
String query = "select * from Products";
PreparedStatement preparedStatement = con.prepareStatement(query);
ResultSet rs = preparedStatement.executeQuery();
data.add("<--- Select a Product --->");
while(rs.next()){
String product = rs.getString("Product");
String id = Integer.toString(rs.getInt("Id"));
data.add(product + "----" + id);
}
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, data);
spinnerProducts.setAdapter(NoCoreAdapter);
//pbbar.setVisibility(View.GONE);
scan.setEnabled(false);
pushToDB.setEnabled(false);
}
} catch (Exception ex) {
//pbbar.setVisibility(View.GONE);
ex.printStackTrace();
}
spinnerProducts.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinnerProducts.getSelectedItemPosition() != 0){
scan.setEnabled(true);
}
productSelected = spinnerProducts.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) { /* do nothing */ }
public void onClick(View v){
mScannerView = new ZXingScannerView(MainActivity.this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
public void onClickDB(View v){
//pbbar.setVisibility(View.VISIBLE);
pushToDB.setEnabled(false);
try {
Connection con = connectionClass.CONN();
if (con != null) {
String query, query1;
PreparedStatement preparedStatement, preparedStatement1; // = con.prepareStatement(query);
ResultSet rs; // = preparedStatement.executeQuery();
String[] splArray;
//SimpleCursorAdapter adapter = (SimpleCursorAdapter) spinnerProducts.getAdapter();
for (int position = 1; position < scanned_data.size(); position++) {
splArray = scanned_data.get(position).split(",");
query = "select * from Products_Barcode where ProductId ='" + splArray[1] + "'";
preparedStatement = con.prepareStatement(query);
rs = preparedStatement.executeQuery();
if(rs.next()){
query1 = "Update Products_Barcode set Barcode = '" + splArray[2] + "' where ProductId = '" + splArray[1] + "'";
preparedStatement1 = con.prepareStatement(query1);
preparedStatement1.executeQuery();
}
else{
query1 = "insert into Products_Barcode values ('" + splArray[1] + "','" + splArray[0] + "','" + splArray[2] + "');";
preparedStatement1 = con.prepareStatement(query1);
preparedStatement1.executeQuery();
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Data tranferred successfully")
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
} catch (Exception ex) {
//pbbar.setVisibility(View.GONE);
ex.printStackTrace();
}
}
@Override
protected void onPause(){
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
//if(chk == 0){
Log.v("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String source_text = result.getText();
String prod = productSelected.replace("----", ",");
scanned_data.add(prod + "," + source_text);
builder.setTitle("Scan Result");
builder.setMessage("Scan complete")
//chk = 1;
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
/*chk = 1;
mScannerView.resumeCameraPreview(MainActivity.this);*/
}
});
spinnerProducts.setSelection(0);
scan.setEnabled(false);
if(!pushToDB.isEnabled()){
pushToDB.setEnabled(true);
}
mScannerView.stopCamera();
setContentView(R.layout.activity_main);
//pbbar.setVisibility(View.GONE);
ArrayAdapter NoCoreAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, data);
NoCoreAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProducts.setAdapter(NoCoreAdapter);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
您需要将微调器数据保存在 onPause
方法中的某处。
@Override
protected void onPause(){
Data data = spinner.getData(); //Implement your own logic
}
当您回到 MainActivity
时,您的 onResume
方法被调用,您必须在其中将数据反馈给微调器。
@Override
protected void onResume(){
spinner.setData(data); //Implement your own logic
}
[编辑]
问题是,您多次使用 setContentView()
,这是一种不好的做法。根据Android documentation
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.
如果你想达到你目前的愿望,你应该使用片段。
总之,不要多次调用setContentView()
。使用 fragment
显示多个 xml
而无需更改 activity。
嗨,我是 Android 编程新手。
加载 mainacitivity 后,我可以从 sql 服务器获取数据并将其填充到微调器中。我可以从微调器中选择一个项目并扫描二维码/条形码。一旦我停止相机并将 return 转到 MainActivity,我就无法在微调器中找到任何数据。如果您需要有关该问题的更多信息,请告诉我。我在 MainActivity.java 文件中提供了完整的代码。
package com.example.vxt.barcodescanner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;//import android.widget.ProgressBar;import android.widget.Spinner;import android.app.AlertDialog;import android.content.DialogInterface;import com.google.zxing.Result;import me.dm7.barcodescanner.zxing.ZXingScannerView;import java.sql.Connection;import java.sql.ResultSet;import java.sql.PreparedStatement;import java.util.ArrayList;import static com.example.vxt.barcodescanner.R.id.spinner;
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
ConnectionClass connectionClass;
//ProgressBar pbbar;
Spinner spinnerProducts;
private ZXingScannerView mScannerView;
ArrayList<String> scanned_data = new ArrayList<String>();
ArrayList<String> data = new ArrayList<String>();
String productSelected;
Button scan, pushToDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connectionClass = new ConnectionClass();
//pbbar = (ProgressBar) findViewById(R.id.pbbar);
//pbbar.setVisibility(View.VISIBLE);
spinnerProducts = (Spinner) findViewById(spinner);
scan = (Button) findViewById(R.id.button);
pushToDB = (Button) findViewById(R.id.button3);
try {
Connection con = connectionClass.CONN();
if (con != null) {
String query = "select * from Products";
PreparedStatement preparedStatement = con.prepareStatement(query);
ResultSet rs = preparedStatement.executeQuery();
data.add("<--- Select a Product --->");
while(rs.next()){
String product = rs.getString("Product");
String id = Integer.toString(rs.getInt("Id"));
data.add(product + "----" + id);
}
ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, data);
spinnerProducts.setAdapter(NoCoreAdapter);
//pbbar.setVisibility(View.GONE);
scan.setEnabled(false);
pushToDB.setEnabled(false);
}
} catch (Exception ex) {
//pbbar.setVisibility(View.GONE);
ex.printStackTrace();
}
spinnerProducts.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinnerProducts.getSelectedItemPosition() != 0){
scan.setEnabled(true);
}
productSelected = spinnerProducts.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) { /* do nothing */ }
public void onClick(View v){
mScannerView = new ZXingScannerView(MainActivity.this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
public void onClickDB(View v){
//pbbar.setVisibility(View.VISIBLE);
pushToDB.setEnabled(false);
try {
Connection con = connectionClass.CONN();
if (con != null) {
String query, query1;
PreparedStatement preparedStatement, preparedStatement1; // = con.prepareStatement(query);
ResultSet rs; // = preparedStatement.executeQuery();
String[] splArray;
//SimpleCursorAdapter adapter = (SimpleCursorAdapter) spinnerProducts.getAdapter();
for (int position = 1; position < scanned_data.size(); position++) {
splArray = scanned_data.get(position).split(",");
query = "select * from Products_Barcode where ProductId ='" + splArray[1] + "'";
preparedStatement = con.prepareStatement(query);
rs = preparedStatement.executeQuery();
if(rs.next()){
query1 = "Update Products_Barcode set Barcode = '" + splArray[2] + "' where ProductId = '" + splArray[1] + "'";
preparedStatement1 = con.prepareStatement(query1);
preparedStatement1.executeQuery();
}
else{
query1 = "insert into Products_Barcode values ('" + splArray[1] + "','" + splArray[0] + "','" + splArray[2] + "');";
preparedStatement1 = con.prepareStatement(query1);
preparedStatement1.executeQuery();
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Data tranferred successfully")
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
} catch (Exception ex) {
//pbbar.setVisibility(View.GONE);
ex.printStackTrace();
}
}
@Override
protected void onPause(){
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
//if(chk == 0){
Log.v("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String source_text = result.getText();
String prod = productSelected.replace("----", ",");
scanned_data.add(prod + "," + source_text);
builder.setTitle("Scan Result");
builder.setMessage("Scan complete")
//chk = 1;
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
/*chk = 1;
mScannerView.resumeCameraPreview(MainActivity.this);*/
}
});
spinnerProducts.setSelection(0);
scan.setEnabled(false);
if(!pushToDB.isEnabled()){
pushToDB.setEnabled(true);
}
mScannerView.stopCamera();
setContentView(R.layout.activity_main);
//pbbar.setVisibility(View.GONE);
ArrayAdapter NoCoreAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, data);
NoCoreAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProducts.setAdapter(NoCoreAdapter);
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
您需要将微调器数据保存在 onPause
方法中的某处。
@Override
protected void onPause(){
Data data = spinner.getData(); //Implement your own logic
}
当您回到 MainActivity
时,您的 onResume
方法被调用,您必须在其中将数据反馈给微调器。
@Override
protected void onResume(){
spinner.setData(data); //Implement your own logic
}
[编辑]
问题是,您多次使用 setContentView()
,这是一种不好的做法。根据Android documentation
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy.
如果你想达到你目前的愿望,你应该使用片段。
总之,不要多次调用setContentView()
。使用 fragment
显示多个 xml
而无需更改 activity。