SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MainActivity : AppCompatActivity() {
  2.     internal var doItList: MutableList<String> = ArrayList()
  3.     internal var adapter: ArrayAdapter<String>? = null
  4.     internal var listView: ListView? = null
  5.     internal var emptyView: TextView? = null
  6.     override fun onCreate(savedInstanceState: Bundle?) {
  7.         super.onCreate(savedInstanceState)
  8.         setContentView(layout.activity_main)
  9.         val toolbar: Toolbar = findViewById(id.toolbar)
  10.         setSupportActionBar(toolbar)
  11.         listView = findViewById<ListView?>(id.listview)
  12.         emptyView = findViewById<TextView?>(id.empty_view)
  13.         adapter = ArrayAdapter(this, R.layout.simple_list_item_1,
  14.                 doItList)
  15.         listView!!.adapter = adapter
  16.         val fab: FloatingActionButton = findViewById(id.fab)
  17.         fab.setOnClickListener { showAddDoItItem() }
  18.     }
  19.  
  20.     override fun onResume() {
  21.         super.onResume()
  22.         refreshUI()
  23.     }
  24.  
  25.     protected fun refreshUI() {
  26.         if (doItList.size > 0) {
  27.             listView!!.visibility = View.VISIBLE
  28.             emptyView!!.visibility = View.GONE
  29.             adapter!!.notifyDataSetChanged()
  30.         } else {
  31.             listView!!.visibility = View.GONE
  32.             emptyView!!.visibility = View.VISIBLE
  33.         }
  34.     }
  35.  
  36.     protected fun showAddDoItItem() {
  37.         val inputView = EditText(this)
  38.         inputView.hint = "Add Do Item Name"
  39.         val lp = LayoutParams(
  40.                 LayoutParams.MATCH_PARENT,
  41.                 LayoutParams.WRAP_CONTENT
  42.         )
  43.         lp.setMargins(10, 10, 10, 10)
  44.         inputView.layoutParams = lp
  45.         val alertDialog: AlertDialog = Builder(this)
  46.                 .setTitle("Do It")
  47.                 .setCancelable(true)
  48.                 .setView(inputView)
  49.                 .setNegativeButton("Cancel") { dialogInterface, i -> dialogInterface.dismiss() }
  50.                 .setPositiveButton("Add") { dialogInterface, i ->
  51.                     doItList.add(inputView.text.toString())
  52.                     refreshUI()
  53.                 }
  54.                 .create()
  55.         alertDialog.show()
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top