现在我从EditText MainActivity中提取一个值,并将其作为字符串放在sharedpref中。在后来的活动中,我收到了这个值,并试图将它添加到一个正在增长的数组的末尾。要将Array存储在稍后的活动中以便稍后接收以继续添加,我使用了JSON数组。因此,基本上我在计数器为0时创建一个字符串数组(用户自清理后首先输入)并从MainActivity EditText接收字符串值并将其作为字符串数组中的第一个值添加,然后将其存储为JSON数组。当我稍后收到它(当计数器大于0时)并将其转换回字符串数组时,我的TextView中对于数组中任何给定位置的outPut是[L.java.lang.String; @ 42a5e438,。下面是我的代码,如果coutner == 0和counter> 0;有谁知道解决方案是什么? int placeCounterExplanation = pref.getInt(" placeCounterExplanation",0); //获得新项目 String explanationItem = pref.getString(" explanationItem",null);
if(placeCounterExplanation > 0){
///////////////////////////////////// RECEIVE//
//pull existing array
JSONArray explanationjArray;
try {
explanationjArray = new JSONArray(settings.getString("jArray", ""));
//converting from json back to workable string array
// String []ExplanationDetailArray = new String[explanationjArray.length()];
String []ExplanationDetailArray = new String[100];
//matching them/converting
for(int i = 0, count = explanationjArray.length(); i<= count; i++)
{
try {
String jsonString = explanationjArray.getString(i);
ExplanationDetailArray[i] = jsonString;
}
catch (JSONException e) {
e.printStackTrace();
}
}
//after matching^ it adds newest value
ExplanationDetailArray[placeCounterExplanation] = explanationItem;
////////////////////////////////////////////////////////////////////////
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);
if (product.equals("Alcohol")){
test.setText(ExplanationDetailArray[0]);
String placeCount = Integer.toString(placeCounterExplanation);
test2.setText(ExplanationDetailArray[1]);
// test3.setText(placeCounterExplanation - 1);
}
//////////////////////////////////////////////////////////////////////////
/////STORE
explanationjArray.put(Arrays.toString(ExplanationDetailArray));
// explanationjArray.put(ExplanationDetailArray);
editor2.putString("jArray", explanationjArray.toString());
editor2.commit();
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (placeCounterExplanation == 0){
ExplanationDetailArray = new String[100];
ExplanationDetailArray[0] = explanationItem;
JSONArray explanationjArray = new JSONArray();
//////////////////////////////
// String sC = Integer.toString(spotCounter);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);
if (product.equals("Alcohol")){
test.setText(ExplanationDetailArray[0]);
// test2.setText(ExplanationDetailArray[1]);
// test3.setText(placeCounterExplanation - 1);
}
///////////////////////////////
//STORING MIGHT NOT NEED THIS INSIDE HERE
// explanationjArray.put(ExplanationDetailArray);
explanationjArray.put(Arrays.toString(ExplanationDetailArray));
editor2.putString("jArray", explanationjArray.toString());
editor2.commit();
// placeCounterExplanation = placeCounterExplanation + 1;
// editor.putInt("placeCounterExplanation",placeCounterExplanation);
//editor.commit();
}
//after either IF Statement catches teh program, it advances the counter.
placeCounterExplanation = placeCounterExplanation + 1;
editor.putInt("placeCounterExplanation",placeCounterExplanation);
editor.commit();
答案 0 :(得分:0)
我相信您的问题来自代码中的以下行: explanationjArray.put(ExplanationDetailArray);
这会将对象的文本表示插入到JSON数组中。相反,你可以这样做: explanationjArray.put(Arrays.toString(ExplanationDetailArray)); 或者您自己的将字符串数组转换为串联字符串集的实现。