Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

android - Cloud Firestore and RecyclerView. On saving data in the cloud firestore the data is not getting updated in the adapter in realtime

I am using Cloud Firestore and RecyclerView.Adapter for displaying my data in the activity. When I am saving my data the changes are not getting reflected in the adapter in the real-time. It is working fine when I an restarting the app or reloading the activity the newly saved entity is visible then.

private var allEntities: List<EntityModel> = ArrayList()
private val entityAdapter: EntityAdapter = EntityAdapter(this,allEntities)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // set layout manager for adapter
    val layoutManager = LinearLayoutManager(this)
    layoutManager.orientation = LinearLayoutManager.VERTICAL
    entityViewRecycler.layoutManager = layoutManager
    entityViewRecycler.adapter = entityAdapter

    getAllEntities();

    val fab: View = findViewById(R.id.fab)
    fab.setOnClickListener { _ ->

            // ... getting data as input to save the entity

            // save to firebase cloud firestore 
            FirestoreClass().addEntity(this, entity);
        }

        mDialogView.cancel.setOnClickListener {
            mAlertDialog.dismiss()
        }
    }
}

// getting the entities and setting notifyDataSetChanged() to the adapter
private fun getAllEntities() {
    FirestoreClass().getAllEntities().addOnCompleteListener{
        if(it.isSuccessful){
            this.hideProgressDialog()
            allEntities = it.result!!.toObjects(EntityModel::class.java)
            entityAdapter.entities = allEntities

            // applying notifyDataSetChanged() to the adapter
            entityAdapter.notifyDataSetChanged()
        }else{
            this.hideProgressDialog()
            Log.e("fail get entities", "Failed to get all the entities for the user ")
        }
    }
}

// When my entity is getting added successfully I am calling a function to show the toast and also calling notifyDataSetChanged() on the adapter

fun entityAddingSuccessful() {
    hideProgressDialog()
    entityAdapter.notifyDataSetChanged()
    showToast("Entity Added Successfully");
}

// Also setting notifyDataSetChanged() in the onStart method  

override fun onStart() {
    super.onStart()
    entityAdapter.notifyDataSetChanged();
}

Here is my AddEntity method

fun addEntity(activity: MainActivity, entity: EntityModel){
    val entityId : String = mFireStore.collection(Constants.USERS).document(getCurrentUserId()).collection(Constants.ENTITIES).document().id
    entity.entityId = entityId
    mFireStore.collection(Constants.USERS)
        .document(getCurrentUserId())
        .collection(Constants.ENTITIES)
        .document(entityId)
        .set(entity)
        .addOnSuccessListener {

            activity.entityAddingSuccessful()
        }
        .addOnFailureListener{e ->
            activity.hideProgressDialog()
            Log.e("Adding Entity Failed", "addEntity: ", e)
        }
}

Here is my EntityAdapter class

class EntityAdapter(val context: Context?,var entities: List<EntityModel>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    (holder as viewHolder).bind(entities[position], position);
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EntityAdapter.viewHolder {
    val view = LayoutInflater.from(parent.context).inflate(R.layout.entity_row, parent, false)
    return viewHolder(view);
}

override fun getItemCount(): Int {
    return entities.size;
}

inner class viewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
    fun bind(entity: EntityModel?, position: Int){
        itemView.name.text = entity!!.name
        itemView.amount.text = entity.amount.toString()
    }
}
}

Let me know what am I missing here? Thanks in advance for your help.

question from:https://stackoverflow.com/questions/65886682/cloud-firestore-and-recyclerview-on-saving-data-in-the-cloud-firestore-the-data

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...