gpt4 book ai didi

android - RealmResults 大小返回零

转载 作者:行者123 更新时间:2023-11-29 19:04:51 29 4
gpt4 key购买 nike

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

我有一个变量 mResults = RealmResults ;

这有数据,如下图所示

enter image description here

但是当我输入 >>mResults.size<< 它返回 0。可能是什么问题?顺便提一句。我正在使用 kotlin 在 Android 中工作。

问题出在Recyclerview getItemCöunt

ActivityMain::::

Realm.init(this)
val configuration = RealmConfiguration.Builder().build()
Realm.setDefaultConfiguration(configuration)
mRealm = Realm.getDefaultInstance()
val results:RealmResults<Drop> = mRealm.where(Drop::class.java).findAll()

mToolbar = findViewById<Toolbar>(R.id.toolbar)
mRecycler = findViewById<RecyclerView>(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<AdapterDrops.DropHolder> {
private var mInflater:LayoutInflater
private var mResults:RealmResults<Drop>
constructor(context:Context, results: RealmResults<Drop>){
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<TextView>(R.id.tv_what)

}

}

}

谢谢大家。

最佳答案

尝试这个,因为你正在从数据库中获取所有记录

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

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

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

  //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 方法,所以删除它

关于android - RealmResults 大小返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47675137/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com