RealmResults大小正在返回零

我有一个小问题,我找不到任何结果。 是的,我也做了一些研究。

我有一个variablesmResults = RealmResults ;

这有数据,你可以看到下面的图片

在这里输入图像说明

但是当我键入>> mResults.size <<返回0.问题是什么? 顺便说一句。 我在Android使用kotlin工作。

pröblem在RecyclerviewgetItemCöunt中

ActivityMain ::::

Realm.init(this) val configuration = RealmConfiguration.Builder().build() Realm.setDefaultConfiguration(configuration) mRealm = Realm.getDefaultInstance() val results:RealmResults = mRealm.where(Drop::class.java).findAll() mToolbar = findViewById(R.id.toolbar) mRecycler = findViewById(R.id.rv_drops) val manager = LinearLayoutManager(this) mRecycler.layoutManager = manager mRecycler.adapter = AdapterDrops(this, results) 

 open class Drop : RealmObject { private var what: String? = null @PrimaryKey private var added: Long? = null private var whenT: Long? = null private var completed: Boolean? = null constructor(){} constructor(what: String, added: Long, whenT: Long, completed: Boolean) : super() { this.what = what this.added = added this.whenT = whenT this.completed = completed } fun getWhat(): String? { return what } fun setWhat(what: String) { this.what = what } fun getAdded(): Long? { return added } fun setAdded(added: Long) { this.added = added } fun getWhenT(): Long? { return whenT } fun setWhenT(whenT: Long) { this.whenT = whenT } fun getCompleted(): Boolean? { return completed } fun setCompleted(completed: Boolean) { this.completed = completed } 

}

 class AdapterDrops: RecyclerView.Adapter { private var mInflater:LayoutInflater private var mResults:RealmResults constructor(context:Context, results: RealmResults){ mInflater = LayoutInflater.from(context) mResults = results } override fun onBindViewHolder(holder: DropHolder?, position: Int) { val drop:Drop = mResults[position]!! holder!!.mTextWhat.setText(drop.getWhat()) } override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): DropHolder { val view = mInflater.inflate(R.layout.row_drop, parent, false) val holder = DropHolder(view) return holder } override fun getItemCount(): Int { return mResults.size <<<<< pröblem } class DropHolder: RecyclerView.ViewHolder { var mTextWhat:TextView constructor(itemView: View):super(itemView){ mTextWhat = itemView.findViewById(R.id.tv_what) } } } 

多谢你们。

One Solution collect form web for “RealmResults大小正在返回零”

尝试这一点,因为你是从数据库中获取所有记录

 var mResults:OrderedRealmCollection = Realm.getDefaultInstance().where(Drop::class.java).findAll() 

请用空的构造函数定义Drop类。

请从活动中删除以下代码,并将其粘贴到您的应用程序类中

  //region initialise Realm for application Realm.init(this) //endregion //region creating realm config val realmConfig:RealmConfiguration = RealmConfiguration.Builder() .name("kotlin_demo.realm") .deleteRealmIfMigrationNeeded() .build() //endregion //region for development purpose getting new realm db each time Realm.deleteRealm(realmConfig) Realm.setDefaultConfiguration(realmConfig) //endregion 

在kotlin中,不需要为Drop类定义getter setter方法,所以删除它

Kotlin language will be the best programming language for Android.