从realm.io读取并添加到listview [英] Read from realm.io and add to listview
问题描述
我想使用 Realm.io 保存数据,然后在列表视图中显示每个项目。
这是我如何创建数据库(我想):
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)));
}
这没有用。而且我不知道 db 是否创建在第一位,无论如何不能在手机上找到它。
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);
推荐答案
根本原因是OP没有创建自定义适配器。 ListView需要创建一个类,该类从适用于每种用途的适配器类型 case 并覆盖至少 getView()
方法
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
这篇关于从realm.io读取并添加到listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!