Notify activity from adapter when item is deleted (emptyView)

116
November 03, 2018, at 03:20 AM

Let's define one variable in the begin, it will be more easier to read:
sqlite3 column KEY_TITLE = A

I have activity in conduction with cursor loader. When there are no rows in column A I want activity to display an emptyView. To do that, I created a method which checks all rows in column A if there is any data in it.

fun hideLayout(){
        val isEmpty = emptyViewObserver(applicationContext).isEmpty()
        if (isEmpty) {
            recyclerView.visibility = View.GONE
            emptyView.visibility = View.VISIBLE
        } else {
            recyclerView.visibility = View.VISIBLE
            emptyView.visibility = View.GONE
        }
    }

class emptyViewObserver with isEmpty method:

 class emptyViewObserver(val context: Context){
        fun isEmpty():Boolean{
            val db = WalletDbHelper(context).readableDatabase
            val projection = arrayOf(KEY_TITLE)
            val cursor = db.query(
                    WalletEntry.TABLE_NAME,
                    projection,
                    null,
                    null,
                    null,
                    null,
                    null
            )
            val names = mutableListOf<String>()
            with(cursor){
                while (moveToNext()){
                    val name = getString(cursor.getColumnIndexOrThrow(WalletEntry.KEY_TITLE))
                    names.add(name)
                }
            }
            db.close()
            return names.isEmpty()
        }
    }

Whenever I perform any CRUD operation in MainActivity class I call hideLayout method to check out if emptyView should be shown or not. It works fine. But I'm facing problem after I perform delete operation from my adapter to call hideLayout() method. For obvious reasons it throws me error when calling it directly from adapter so I though about implementing interface and overriding this in MainActivity. I read some tutorials but I still didn't figure out how to implement this properly.

This is my delete method from adapter:

holder.delete.setOnClickListener { v ->
            val popup = PopupMenu(context, v)
            val inflater = popup.menuInflater
            inflater.inflate(R.menu.delete, popup.menu)
            popup.show()
            popup.setOnMenuItemClickListener { v ->
              when(v.itemId) {
                  R.id.delete -> {
                      delete(uri, walletName)
                      true
              }
                  else -> {
                      false
                  }
              }
            }
        }

and delete():

fun delete(uri: String, walletName: String){
        val newUri: Uri = Uri.parse(uri)
        context.contentResolver.delete(newUri, WalletEntry.KEY_TITLE, arrayOf(walletName))
    }

Do you have any ideas how can I write this code?

Answer 1

You need a callback, When you delete some item, check if is the last one and if it is true call that.

Add this interface on your adapter:

interface OnListIsEmpty{
    fun onListIsEmpty()
}

And create a object of this like global variable in your adapter too:

private var mOnListIsEmpty: OnListIsEmpty? = null

Add a setter method:

fun setOnListIsEmpty(onListIsEmpty: OnListIsEmpty){
    this.mOnListIsEmpty = onListIsEmpty
}

And in your delete method do the validation if is empty if it is true then:

    mOnListIsEmpty?.onListIsEmpty()

And not forget set the callback when you initilize the adapter with:

adapter.setOnListIsEmpty(object : OnListIsEmpty{
        override fun onListIsEmpty() {
            //your action
        }
    })
READ ALSO
How to append icon to the text in the textView in case of the marquee?

How to append icon to the text in the textView in case of the marquee?

I want to show a textView which is acting as a news marquee and I want to make every sentence separated from it's following one by an icon like tv channels marquee so how to append this icon after every sentence ??

98
getGeofenceTransition() returning -1 when passing a ResultReceiver as an intent extra

getGeofenceTransition() returning -1 when passing a ResultReceiver as an intent extra

I'm very new to Android development, and attempting to build a basic app that shows a GoogleMap (with some GeoJSON layers displayed) and will alert the user when entering a geofenceI've got it set up to send a notification when entering/exiting, but unfortunately...

116
This Realm instance has already been closed

This Realm instance has already been closed

I am developing a cv app and use realm for saving retrofit responses, but I am getting the following exception:

183
Deleting menu from ActionBar

Deleting menu from ActionBar

I faced with the following problem:

107