0

我有 4 个字符串数组,我将它们转换为单个字符串(由 , 分隔)并保存在 sharedpreference 中。然后我在 onCreate 方法中检索变量。然后我想检查该元素是否具有分配给它的值,如果有,则使用静态生成的文本视图显示它:

public class DebtList extends Activity {

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
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];

    SharedPreferences sharedPref= getSharedPreferences("chaosdatasnowball", 0);

    String tempDebtNames = sharedPref.getString("debtNames", "");
    if (!tempDebtNames.equals("")){
    debtName = convertStringToArray(tempDebtNames);
    }
    String tempDebtAmounts = sharedPref.getString("debtAmounts", "");
    if (!tempDebtAmounts.equals("")){
    debtName = convertStringToArray(tempDebtAmounts);
    }
    String tempDebtRates = sharedPref.getString("debtRates", "");
    if (!tempDebtRates.equals("")){
    debtName = convertStringToArray(tempDebtRates);
    }
    String tempDebtPayments = sharedPref.getString("debtPayments", "");
    if (!tempDebtPayments.equals("")){
    debtName = convertStringToArray(tempDebtPayments);
    }

    Bundle extras = getIntent().getExtras();

    int trigger = 0;
    for (int i=0;i<debtName.length;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;
        }
    }

    TableLayout tl = (TableLayout) findViewById(R.id.debtListTableView);
    for (int i=0;i<debtName.length;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);
        }
    }

    int counter = debtName.length;

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

    String debtAmounts = convertArrayToString(debtAmount, counter);
    editor.putString("debtAmounts", debtAmounts);

    String debtRates = convertArrayToString(debtRate, counter);
    editor.putString("debtRates", debtRates);

    String debtPayments = convertArrayToString(debtPayment, counter);
    editor.putString("debtPayments", debtPayments);




    editor.putInt("counter", counter);
    editor.commit();

    TextView disp = (TextView) findViewById(R.id.dispAdditionalAmount);
    disp.setText("" + debtNames);


}


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;
}

目前,setTexts 和 addViews 将每个数组的所有 10 组显示为空。

正如我所提到的,它应该只在元素有值时显示。

任何想法为什么会一遍又一遍地显示空值,而不是保留检索到的输入数据。

4

0 回答 0