Reputation: 665
How is it possible to get List object from String, add new element to list and turn it back to string. I use Json and jackson, created class for list's element:
public class WeightDataJson {
private String weightValue;
private String dateValue;
public WeightDataJson(String weightValue,String dateValue){
this.weightValue=weightValue;
this.dateValue=dateValue;
}
public String getWeightValue() {
return weightValue;
}
public void setWeightValue(String weightValue) {
this.weightValue = weightValue;
}
public String getDateValue() {
return dateValue;
}
public void setDateValue(String dateValue) {
this.dateValue = dateValue;
}
}
and my code for change List and String:
String myWeightData = preferencesWeight.get();
List<WeightDataJson> myList = new Vector<WeightDataJson>();
ObjectMapper jsonMapper = new ObjectMapper();
try {
myList = jsonMapper.readValue(myWeightData, new TypeReference<List<WeightDataJson>>(){});
} catch (IOException e) {
e.printStackTrace();
}
WeightDataJson newWeightData = new WeightDataJson(editText.getText().toString(),"213123");
myList.add(newWeightData);
JSONObject myJson=new JSONObject();
for(int i=0;i<myList.size();i++){
try {
myJson.put("weightValue",myList.get(i).getWeightValue());
myJson.put("dateValue",myList.get(i).getDateValue());
} catch (JSONException e) {
e.printStackTrace();
}
}
String toSave ="["+myJson.toString()+"]";
Toast.makeText(getActivity(),"zapisuje: "+toSave,Toast.LENGTH_LONG).show();
preferencesWeight.save(toSave);
WeightDataProvider weightDataProvider = new WeightDataProvider(addIcon,deleteIcon,editText.getText().toString(),"213123");
weightAdapter.add(weightDataProvider);
myWeightData
is 'basic' String (this is possible that it is empty), I am not sure but probably there is problem because String for jakcson has to be like [{element1},{element2}]
and Json object gives {element1},{element2}
so I put there String toSave ="["+myJson.toString()+"]"
.
If basic String is [{weightValue":"50","dateValue":"34234"}]
I have an error in line myList=jsonmapper.readValue~~
:
07-30 03:59:04.407 11837-11837/com.plan.aplikacjamobilna W/System.err: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.plan.aplikacjamobilna.WeightDataJson]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
07-30 03:59:04.407 11837-11837/com.plan.aplikacjamobilna W/System.err: at [Source: [{"weightValue":"14","dateValue":"213123"}]; line: 1, column: 3] (through reference chain: java.util.ArrayList[0])
Is there another way to solve my problem?
Upvotes: 0
Views: 119
Reputation: 7474
If you want store data as String Why are you complicating things and involving other unnecessary classes and object of those ?
override method toString() in your class
a) chose data(variable) delimiter1 ":"
b) concatenate data for Class records (variables) using delimiter1
make constructor with String parameter to:
a) split concatenated string with previous used delimiter1
b) assign result [] of split to declared variables
make static methods to:
a) List<> as parameter to concatenate each object toString() method produced strings with delimiter2 "%" to achieve something like this:
"var1o1:var2o1%var1o2:var2o2%var1o3:var2o3" etc
b) to split above string with:
b.a/ delimieter2 - > then result each String pass to object to split with
b.b/ delimieter1 -> then result [] of split assign to variables
Upvotes: 1
Reputation: 3654
You can use Gson to switch between your type and String. Usage:
build.gradle
compile 'com.google.code.gson:gson:2.7'
From POJO to String:
Gson gson = new Gson();
String json = gson.toJson(weightData);
From String to POJO:
WeightDataJson newWeightData = gson.fromJson(json, WeightDataJson.class);
EDIT :
With ArrayList
List<WeightDataJson> list = new ArrayList<WeightDataJson>();
list.add(obj1);
list.add(obj2);
list.add(obj3);
..
Type type = new TypeToken<List<WeightDataJson>>() {}.getType();
String json = gson.toJson(list, type);
List<WeightDataJson> newWeightData = gson.fromJson(json, type);
Upvotes: 0