alexander mogren alexander mogren - 8 months ago 77
Android Question

Read from realm.io and add to listview

I want to use Realm.io to save data and then display each item in a listview.
Here is how I have create the database (I think):

config = new RealmConfiguration.Builder(this)
.name("timeLog")
.schemaVersion(42)
.build();

realm.setDefaultConfiguration(config);


Then after this when I save my data I do the following:

Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
timeData logTime = realm.createObject(timeData.class);
logTime.setWorkedHours(timeTotal);
realm.commitTransaction();
System.out.println(logTime);
System.out.println(logTime.getId());


And now I want to add this to an array maybe and display it in a listview.
So I thought i could make something like this:

RealmResults<timeData> result = realm.where(timeData.class).findAll();
result = result.sort("sgnumber", Sort.DESCENDING);
ArrayList<timeData> arrayList = new ArrayList();
for(int i = 0;i<result.size()-1;i++){
System.out.println(arrayList.add(result.get(i)));
}


This did not work. And also I dont know if the db i created in the first place, cant find it on the phone anyway.

UPDATE

listview = (ListView)findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
listview.setAdapter(adapter);

Answer

The root cause was that the OP was not creating a custom adapter. A ListView requires the creation of a class that extends from a type of adapter purposeful to each use case and override at least the getView() method