2016-03-23 66 views
0

我已經創建了一個應用程序,它利用斑馬線條形碼掃描器掃描條形碼與在mainActivity.java下面的代碼輸入窗體的一部分:Android - 如何將掃描的條形碼轉換爲字符串?

 @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     scanBtn = (Button)findViewById(R.id.scan_button); 
     formatTxt = (TextView)findViewById(R.id.scan_format); 
     contentTxt = (TextView)findViewById(R.id.scan_content); 
     scanBtn.setOnClickListener(this); 

      public void onClick(View v){ 
      if(v.getId()==R.id.scan_button){ 
      IntentIntegrator scanIntegrator = new IntentIntegrator(this); 
      scanIntegrator.initiateScan(); 
      } 

     } 

     public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
      IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); 
      if (scanningResult != null) { 
      String scanContent = scanningResult.getContents(); 
      String scanFormat = scanningResult.getFormatName(); 
      formatTxt.setText("FORMAT: " + scanFormat); 
      contentTxt.setText("CONTENT: " + scanContent); 
     } 
     else{ 
      Toast toast = Toast.makeText(getApplicationContext(), 
       "No scan data received!", Toast.LENGTH_SHORT); 
      toast.show(); 
      } 
     } 

然後我就指派我的文字輸入值這裏:

String postCode = etPostCode.getText().toString(); 
String address1 = etAddress1.getText().toString(); 

,並用下面的代碼,試圖做同樣的與掃描的內容值:

String scanVal = ((s)findViewById(R.id.scan_content)).toString(); 

,然後添加它編到我的代碼

Toast.makeText(this, "Adding Record...", Toast.LENGTH_SHORT).show(); 
    new SignupActivity(this).execute(postCode, address1, scanVal); 

}

我再發的值,以我的web服務器的最後一部分被點擊提交按鈕時要保存到數據庫,並唉,這是行不通的。

我得到'郵編'和'地址1'的文本值,但沒有掃描字段,它只是我的數據庫中的一個空字段。

有人能看到我在這裏出了什麼地方嗎?

回答

0

您需要通過執行來得到它:

String contents = intent.getStringExtra("SCAN_RESULT"); 

                 String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); 
+0

打招呼。你是說我可能會使用'contentTxt'來傳遞其他值嗎?如:new SignupActivity(this).execute(postCode,address1,contentTxt); – fairydragon

相關問題