java - How to convert a custom object into String in Android? -
i have object of custom class. need convert string can write file. there way out ?
thanks help.
you can't convert custom class string
automatically. system has no way of knowing how want string
formatted, want in , such.
you have manually implement method in custom class returns text interpretation of object. override method called tostring()
. method gets called automatically if provide instance of class methods. let's if system.out.println(instance)
, instance
's tostring()
method automatically invoked.
i don't know custom class is. let's it's person
class member variables string name
, int age
. tostring()
method defined in class this:
@override public string tostring() { return "name: " + name + ", age: " + age; }
this provide output "name: name, age: 30".
Comments
Post a Comment