안녕하세요 Android 프로그래밍이 처음입니다.QR 코드 스캔을 위해 카메라를 사용한 후 주요 활동의 회 전자 데이터가 손실됩니다.
mainacitivity가로드되면 SQL Server에서 데이터를 가져 와서 스피너에 채울 수 있습니다. 스피너에서 항목을 선택하고 qrcode/바코드를 스캔 할 수 있습니다. 일단 카메라를 멈추고 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();
}
}
다른 방법으로 SQL 연결을 이동하고 OnCreate 및 OnResume에서 호출 할 수 있습니까? 내 생각에 스피너는 OnResume이 다시 채워지지만 코드가 없으므로 데이터가 손실됩니다. 나도 틀릴지도 몰라. – Phantomazi
@Phantomazi 감사합니다. 난 별도의 메서드를 만들고 onCreate 및 onResume 호출하여 아이디어를 시도했다. 처음에는 회 전자에 두 세트의 데이터가있었습니다. 그러나 바코드를 스캔하여 activity_main으로 돌아 가면 스피너가 비어 있습니다. – venutamizh