Search code examples
androidkotlinrealm

RealmResults size is returning Zero


I have a little problem, and I couldn't find any results. Yes I did some researches too.

I have a variable mResults = RealmResults < Drop > ;

and this has Data as you can see in the picture below

enter image description here

But when I type >>mResults.size<< it returns 0. What could the problem be? btw. Im working in Android using kotlin.

The pröblem is in the 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)

        }

    }

}

Thanks guys.


Solution

  • Try this as you are fetching all records from db

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

    Please define Drop class with empty constructor.

    Please remove below code from activity and paste them in your application class

      //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
    

    in kotlin no need to define the getter setter methods for Drop class so remove that