Read from realm.io and add to listview

≯℡__Kan透↙ 提交于 2019-12-12 13:25:43

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/38173430/read-from-realm-io-and-add-to-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!