Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将由逗号分隔的字符串转换为数组_Android_Arrays_Sharedpreferences - Fatal编程技术网

Android 将由逗号分隔的字符串转换为数组

Android 将由逗号分隔的字符串转换为数组,android,arrays,sharedpreferences,Android,Arrays,Sharedpreferences,我一直在努力将4个字符串数组保存到SharedReference。目前,我正在将数组分解为一个字符串,用逗号分隔各个部分。当我走到弦上时,它确实准确地保存了它应该保存的片段;然而,当我“加载”数据并将其转换回一个数组时,它是空的(不是空的)。我无法确定如何修复此问题以将阵列拉回来。以下是我的活动: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set

我一直在努力将4个字符串数组保存到SharedReference。目前,我正在将数组分解为一个字符串,用逗号分隔各个部分。当我走到弦上时,它确实准确地保存了它应该保存的片段;然而,当我“加载”数据并将其转换回一个数组时,它是空的(不是空的)。我无法确定如何修复此问题以将阵列拉回来。以下是我的活动:

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;
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.debtlist);
字符串[]债务人名称=新字符串[10];
字符串[]债务金额=新字符串[10];
字符串[]债务率=新字符串[10];
字符串[]债务人付款=新字符串[10];
int计数器=0;
SharedReferences SharedReferences=GetSharedReferences(“chaosdatasnowball”,0);
String tempDebtNames=sharedPref.getString(“债务人名称”和“”);
debtName=convertStringToArray(tempDebtName);
Bundle extras=getIntent().getExtras();
int触发器=0;
对于(int i=0;i您可以:

String tempDebtNames = sharedPref.getString("debtNames", "");
debtName = convertStringToArray(tempDebtNames);
可能是
“debtNames”
键不存在。(在本例中,它返回

请考虑第一次运行应用程序时的情况。密钥将不存在。因此,只有当
!tempdebentnames.equals(“”)时,才应
转换StringToArray
。如果密钥不存在,则继续使用空值