0

4 つの文字列配列を 1 つの文字列 (, で区切って) に変換し、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 セットすべてを null として表示しています。

前述したように、要素に値がある場合にのみ表示する必要があります。

null が何度も表示され、取得された入力データが保持されない理由。

4

0 に答える 0