2013-11-05 160 views
0

我一直在努力將4個字符串數組保存爲sharedpreference。目前我正在將數組拆分爲一個用逗號分隔各個部分的字符串。當我走到繩子上時,它確實按照它應該的方式準確地保存了這些碎片;然而,當我「加載」數據並將其轉換回數組時,它會變爲空(非空)。我無法確定我應該如何解決這個問題,將我的陣列拉回來。下面是我的活動:從逗號分隔的字符串轉換爲數組

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.debtlist); 

    String[] debtName = new String[10]; 
    String[] debtAmount = new String[10]; 
    String[] debtRate = new String[10]; 
    String[] debtPayment = new String[10]; 
    int counter = 0; 

    SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0); 
    String tempDebtNames = sharedPref.getString("debtNames", ""); 
    debtName = convertStringToArray(tempDebtNames); 

    Bundle extras = getIntent().getExtras(); 

    int trigger = 0; 
    for (int i=0;i<counter || i==counter;i++) 
    { 
     if (debtName[i] == null && extras != null && trigger == 0) 
     { 
      debtName[i] = extras.getString("debtName"); 
      debtAmount[i] = extras.getString("debtAmount"); 
      debtRate[i] = extras.getString("debtRate"); 
      debtPayment[i] = extras.getString("debtPayment"); 
      trigger = 1; 
      counter++ ; 
     } 
    } 

    TableLayout tl = (TableLayout) findViewById(R.id.debtListTableView); 
    for (int i=0;i<counter || i==counter;i++) 
    { 
     if (debtName[i] != null) 
     { 

      TableRow tr = new TableRow(this); 
      TextView tv0 = new TextView(this); 
      TextView tv1 = new TextView(this); 
      TextView tv2 = new TextView(this); 
      TextView tv3 = new TextView(this); 
      TableRow.LayoutParams trlp = new TableRow.LayoutParams(); 
      tv0.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f)); 
      tv1.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f)); 
      tv2.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f)); 
      tv3.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 0.25f)); 
      trlp.span = 3; 
      tr.setLayoutParams(trlp); 
      tv0.setText("" + debtName[i]); 
      tv1.setText("" + debtAmount[i]); 
      tv2.setText("" + debtPayment[i]); 
      tv3.setText("" + i); 
      tr.addView(tv0); 
      tr.addView(tv1); 
      tr.addView(tv2); 
      tr.addView(tv3); 
      tl.addView(tr); 
     } 
    } 

    SharedPreferences.Editor editor= sharedPref.edit(); 
    String debtNames = convertArrayToString(debtName, counter); 
    editor.putString("debtNames", debtNames).commit(); 

    TextView disp = (TextView) findViewById(R.id.dispAdditionalAmount); 
    disp.setText("" + debtNames); //This is how I confirm that the convertArrayToString is functioning properly 


} 


public static String convertArrayToString(String[] array, int stop){ 
    String str = ""; 
    for (int i = 0;i<stop; i++) { 
     str = str+array[i]; 
     // Do not append comma at the end of last element 
     if(i<stop-1){ 
      str = str+","; 
     } 
    } 
    return str; 
} 
public static String[] convertStringToArray(String str){ 
    String[] arr = str.split(","); 
    return arr; 
} 

回答

1

你這樣做:

String tempDebtNames = sharedPref.getString("debtNames", ""); 
debtName = convertStringToArray(tempDebtNames); 

這可能是"debtNames"鍵不存在。 (在這種情況下它返回"")。

想想應用程序第一次運行。關鍵不存在。因此,只有在!tempDebtNames.equals("")的情況下,您才應該使用convertStringToArray。如果密鑰不存在,則繼續使用空值。

相關問題