Search code examples
androidkotlinandroid-recyclerviewempty-list

setvisibility for button is not working in android


Basically the idea is adding the address in activity address when clicks on plus it will redirect to next page where the user gets the address form onsubmit it will fetch address and add to previous activity recycleview.

scenerios:--

1-->when the recycleview is empty in address actvity it shows the empty text and also the button is not visible ==>no problem in here

2-->when the recycleview is not empty in address actvity empty text is gone but button is not visible ==>im unable to see visibility of button ==> problem is here

here is my code -->

class AddressActivity : AppCompatActivity(){
private lateinit var data: LiveData<MutableList<Address>>
private  var data1:Int = 0
var adapter: AddressAdapter? =null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.address)
    var mActionBarToolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbartable);
    setSupportActionBar(mActionBarToolbar);
    // add back arrow to toolbar
    if (getSupportActionBar() != null){
        getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);
        getSupportActionBar()?.setDisplayShowHomeEnabled(true);
        getSupportActionBar()?.setHomeAsUpIndicator(R.drawable.ic_keyboard_arrow_left_black_24dp);

        //supportActionBar?.setTitle("Tables")
           getSupportActionBar()?.setTitle((Html.fromHtml("<font color=\"#FFFFFF\">" + getString(
               R.string.address) + "</font>")));

    }

    addbutton.findViewById<View>(R.id.addbutton).setOnClickListener {
        val intent = Intent(this, AddAddressActivity::class.java)
        startActivity(intent)
    }
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
        IntentFilter("custom-message1")
    )
    val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
    recyclerView.setHasFixedSize(true)
    recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
    val adapter = AddressAdapter(applicationContext)

    recyclerView.adapter = adapter
    recyclerView.addItemDecoration(
        DividerItemDecoration(
            recyclerView.context,
            DividerItemDecoration.VERTICAL
        )
    )
    recyclerView.addOnScrollListener(object :
        RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)
            Log.e("RecyclerView", "onScrollStateChanged")
        }

        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
        }
    })
    val application = application as CustomApplication
    data = application.database.AddressDao().getAddressesWithChanges()
    data.observe(this, Observer { words1 ->
        // Update the cached copy of the words in the adapter.
        words1?.let { adapter.updateData(it) }
    })
    if (!data.hasActiveObservers()) {
        ordernow.setVisibility(View.GONE)

        emptytext.setVisibility(View.VISIBLE)

    } else {
        ordernow.setVisibility(View.VISIBLE)

        emptytext.setVisibility(View.GONE)

    }


}

need help thanks


Solution

  • replace this

    data.observe(this, Observer { words1 ->
        // Update the cached copy of the words in the adapter.
        words1?.let { adapter.updateData(it) }
    })
    if (!data.hasActiveObservers()) {
        ordernow.setVisibility(View.GONE)
    
        emptytext.setVisibility(View.VISIBLE)
    
    } else {
        ordernow.setVisibility(View.VISIBLE)
    
        emptytext.setVisibility(View.GONE)
    
    }
    

    to

     data.observe(this, Observer { words1 ->
                
                if (words1.size > 0) {
                ordernow.setVisibility(View.VISIBLE) emptytext . setVisibility (View.GONE)
            } else {
                ordernow.setVisibility(View.GONE) emptytext . setVisibility (View.VISIBLE)
            }
                
                words1?.let { adapter.updateData(it) }
            })