i trying pass object intent , read different post on here use gson. doesn't show errors , runs. when click button freezes phone. there no exception thrown.
oncreate { ... listviewobjects.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { person item = (person)parent.getitematposition(position); toast.maketext(getapplicationcontext(), item.name + " selected\n" + item.status, toast.length_short).show(); gson gson = new gson(); string json = gson.tojson(item); persondescriptionpage.putextra("person", json); startactivity(persondescriptionpage); } }); }
i had 1 line anonymous gson object
persondescriptionpage.putextra("person", new gson().tojson(item));
and if debug it freezes on line. split instantiated object see in first block of code above , freezes on gson.tojson(item)
and reference, class trying serialize json is:
public class person{ string name; boolean ownit; drawable image; string status; int backgroundcolor; public person(){} public person(string n, drawable d, string s, int b) { this.name= n; this.ownit = false; this.image = d; this.status = s; this.backgroundcolor = b; } @override public string tostring() { // todo auto-generated method stub return this.name.tostring(); } }
it better use singleton this. think freezing @ image (getresources().getdrawable(r.drawable.fred207)).
suggest this
public class person{ //singleton private static arraylist<person> persons=null; string name; boolean ownit; drawable image; string status; int backgroundcolor; //only getperson creates objects private person(){} private person(string n, drawable d, string s, int b) { this.name= n; this.ownit = false; this.image = d; this.status = s; this.backgroundcolor = b; } @override public string tostring() { // todo auto-generated method stub return this.name.tostring(); } public static person addperson() { if(persons==null) { persons = new arraylist<person>(); } person pers = new person(); persons.add(pers); return pers; } public static person addperson(string n, drawable d, string s, int b) { if(persons==null) { persons = new arraylist<person>(); } person pers = new person(n,d,s,b); persons.add(pers); return pers; } public static person getperson(int index) { if(persons==null) return null; if(index>(persons.size()-1)) return null; return persons.get(index); } public static arraylist<person> getpersons() { return persons; } }
add way
person.addperson("fred", getresources().getdrawable(r.drawable.fred207),"sick", color.rgb(89,168,65)) ;
pass position instead of person
persondescriptionpage.putextra("position", position);
retrieve item
person.getperson(position);