Sugar ORM: how to display values
I am using Sugar ORM for Android Development via Android Studio.
But I think I have a pretty similar question. How can I display one or more query results as String or int? My object looks like this:
public class PersonsDatabase extends SugarRecord<PersonsSelection>{
String adultText, childText;
int adultCount, childCount;
public PersonsDatabase()
{
}
public PersonsDatabase(String adultText, String childText, int adultCount, int childCount)
{
this.adultText = adultText;
this.childText = childText;
this.adultCount = adultCount;
this.childCount = childCount;
this.save();
}
}
Saving is correct. But when I want to display like this:
public class PersonsSelection extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_persons_selection);
PersonsDatabase personsDatabase = new PersonsDatabase("1 Adult","No Childs",1,0);
List<PersonsDatabase> personsList = PersonsDatabase.findWithQuery(PersonsDatabase.class,"Select adult_Text from PERSONS_DATABASE");
list = (ListView)findViewById(R.id.listView);
list.setAdapter(new ArrayAdapter<PersonsDatabase>(this, android.R.layout.simple_list_item_1, personsList));
}
}
I get something like: PACKAGENAME.PersonsDatabase@4264c038 But I want the values I wrote in the constructor.
Thanks for the help.
source to share
From the docs onArrayAdapter
:
However the TextView link is referenced, it will be populated by the toString () of each object in the array. You can add lists or arrays of custom objects. Override the toString () method of your objects to determine what text will be displayed for the item in the list.
In short: just override the method toString()
in the class PersonsDatabase
to return the desired textual respresentation.
As an alternative:
To use something other than TextViews to display an array like ImageViews, or to get some data other than that the results of toString () populate the views, override
getView(int, View, ViewGroup)
to return the type of view you want.
(again from the docs). Lots of examples on how to do this.
source to share