2013-05-03 40 views
3

我正在創建一個應用程序,在該應用程序中掃描一些條形碼並不斷提取它們的值並在單個位置將它們棍棒化。就像這聽起來那麼簡單,我一直無法創建一個數組,在這個數組中我繼續保存新的值或者一個字符串,我將它們連接在一起。如何通過活動將多個值保存到單個變量中。

請注意以防萬一有人需要更多的代碼或解釋,我明白這個問題可能不是很豐富。

編輯:

public class example 
{ 

    String val; 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      try 
      { 

       String list_id=ToolList3.ID; 
       String list_qty=ToolScanDet3.qty; 

       // val is returned from the barcode scanning app. 

       val=val+issue_id+"~"+issue_qty+";"; 
       Log.d("Tools issued till yet...", val); 

       /* Club all the tool IDs together and fire a single query to issue 
           them all against one name. */ 

       Intent i=new Intent(Issue.this,Issue1.class); 
       startActivity(i); 

       //Issue1 again returns a pair of id and qty which needs to be saved along with the previous set of values. 
      } 

我基本上是有麻煩試圖挽救返回值的集合與前面的,即返回全殲先前值換新一起。我試圖把它們放在一個數組中,但是這需要一個計數器,它再次違背了目的,因爲計數器將被初始化爲零並重新開始。

+0

是的需要一些代碼。 – 2013-05-03 06:46:49

+0

看起來像你想要他們這樣保存在SQLiteDB或SharedPreference - >看看這個:http://stackoverflow.com/questions/15948107/how-to-hold-the-selected-value-in-the-the-無線電按鈕/ 15948303#15948303 - 只是爲了概念,而不是問題的相似性。 – 2013-05-03 06:49:45

+0

@Hardik,請檢查更新。 – 2013-05-03 08:29:39

回答

0

我找不到任何對我的情況可行的解決方案,這就是爲什麼我必須使用SQL Lite創建本地數據庫。每次需要時將值傳遞給數據庫,然後在工作流程結束後檢索值。

註釋:如果任何人需要使用SQL Lite創建本地數據庫的幫助。樂意幫忙:)

1

除非元素的數量已知並且是常量,否則最好使用ArrayList而不是數組。在當你想保持活動時被銷燬的方向變化引起的數據的情況下,可以將它們保存在onSavedInstanceState:

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString("temp", tempString); 
} 

然後檢索它放回的onCreate:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  

    if(savedInstanceState != null) { 
     your_arraylist = savedInstanceState.getString("temp"); 
    } 

編輯: 根據你想要的,掃描活動不應該初始化任何字符串。它應該得到它由主,而不是傳遞給它的字符串值:

public class ScanActivity extends Activity { 

    String tempString; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if(savedInstanceState == null) { 
     tempString = getIntent().getStringExtra("temp"); 
    } else { 
     // orientation change 
     tempString = saveInstanceState.getString("temp"); 
    } 
} 

一旦你完成掃描,做

Intent output = new Intent(); 
output.putExtra("temp", tempString); 
setResult(RESULT_OK, output); 
finish(); 

字符串發送回您的主要活動。

+0

您可以檢查更新並相應地優化您的答案嗎?這絕對是我尋找的一系列解決方案。謝謝! – 2013-05-03 08:31:22

+0

看來你只需要存儲一個字符串而不是一個字符串數組。這就是全部? – Neoh 2013-05-03 08:35:34

+0

就像我之前提到的,每次我開始一個活動時,它都會返回一個值,我需要它們繼續添加到數組或變量中,直到我開始另一個活動並將其傳遞給整個集合。 – 2013-05-03 08:39:24

相關問題