Мой вопрос об ошибке, возникшей, когда я использую свой следующий класс:
public class ClassePai {
private int ID;
private String Nome;
private ArrayList<ClasseFilho> listaParentes;
public ClassePai( int id, String nome){
this.ID = id;
this.Nome = nome;
listaParentes = new ArrayList<ClasseFilho>();
}
public void addParente(ClasseFilho classeFilho) {
listaParentes.add(classeFilho);
}
public ClasseFilho getParente( int index ){
return listaParentes.get(index);
}
public int length(){
return listaParentes.size();
}
public String Nome(){
return this.Nome;
}
public int ID(){
return this.ID;
}
}
и этот класс "сына":
public class ClasseFilho {
private String Nome;
private String Grau;
public ClasseFilho( String nome, String grau ){
this.Nome = nome;
this.Grau = grau;
}
public String Nome(){
return this.Nome;
}
public String Grau(){
return this.Grau;
}
}
Это моя активность:
public class TesteClasseActivity extends Activity {
private ClassePai cp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cp = new ClassePai( 1, "Junior" );
cp.addParente( new ClasseFilho( "Vinicius", "filho" ) );
cp.addParente( new ClasseFilho( "Luciene", "namorada" ) );
//old call returning error
//Toast.makeText(getApplicationContext(), cp.length(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), String.valueOf( cp.length() ), Toast.LENGTH_SHORT).show();
cp.addParente( new ClasseFilho( "Veraldo", "pai" ) );
cp.addParente( new ClasseFilho( "Sônia", "mãe" ) );
Toast.makeText(getApplicationContext(), String.valueOf( cp.length() ), Toast.LENGTH_SHORT).show();
( (TextView) findViewById(R.id.edit) ).setText( cp.Nome() );
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), cp.getParente(1).Nome(), Toast.LENGTH_SHORT).show();
}
});
//my try of doing this, but don't work
JSONObject obj = new JSONObject();
obj.put("teste", cp);
// BUT unfortunatelly
//
// obj.toString() returns this:
//
// {"teste":"my.package.name.ClassePai@40516650"}
//
// BUT...
//wondering on net, I've deceided to try Gson:
//first part: object to string
Gson obj = new Gson();
obj.toJson(cp);
String obj_in_xml = obj.toString();
Toast.makeText( getApplicationContext() , obj_in_xml, Toast.LENGTH_SHORT).show();
//no error, appear works fine, but...
//second part: string to object
Gson obj2 = new Gson();
ClassePai new_cp = obj2.fromJson( obj_in_xml, ClassePai.class);
Toast.makeText( getApplicationContext() , new_cp.Nome(), Toast.LENGTH_SHORT).show();
//give me an error com.google.gson.stream.MalformedJsonException
//and I could't find my properties in obj.toString(), but only codes like:
//
//adapter=com.google.gson.internal.bind.TypeAdapters$7@40529278]
//
// and don't find any of the object's string properties in the obj.toString() string
//
// what I'm doing wrong????
}
}
Как я могу сохранить этот объект с дочерними объектами в текстовом формате и после загрузки из String в объекты снова?
Пожалуйста, помогите мне !!!