Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity : AppCompatActivity() {
- internal var doItList: MutableList<String> = ArrayList()
- internal var adapter: ArrayAdapter<String>? = null
- internal var listView: ListView? = null
- internal var emptyView: TextView? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(layout.activity_main)
- val toolbar: Toolbar = findViewById(id.toolbar)
- setSupportActionBar(toolbar)
- listView = findViewById<ListView?>(id.listview)
- emptyView = findViewById<TextView?>(id.empty_view)
- adapter = ArrayAdapter(this, R.layout.simple_list_item_1,
- doItList)
- listView!!.adapter = adapter
- val fab: FloatingActionButton = findViewById(id.fab)
- fab.setOnClickListener { showAddDoItItem() }
- }
- override fun onResume() {
- super.onResume()
- refreshUI()
- }
- protected fun refreshUI() {
- if (doItList.size > 0) {
- listView!!.visibility = View.VISIBLE
- emptyView!!.visibility = View.GONE
- adapter!!.notifyDataSetChanged()
- } else {
- listView!!.visibility = View.GONE
- emptyView!!.visibility = View.VISIBLE
- }
- }
- protected fun showAddDoItItem() {
- val inputView = EditText(this)
- inputView.hint = "Add Do Item Name"
- val lp = LayoutParams(
- LayoutParams.MATCH_PARENT,
- LayoutParams.WRAP_CONTENT
- )
- lp.setMargins(10, 10, 10, 10)
- inputView.layoutParams = lp
- val alertDialog: AlertDialog = Builder(this)
- .setTitle("Do It")
- .setCancelable(true)
- .setView(inputView)
- .setNegativeButton("Cancel") { dialogInterface, i -> dialogInterface.dismiss() }
- .setPositiveButton("Add") { dialogInterface, i ->
- doItList.add(inputView.text.toString())
- refreshUI()
- }
- .create()
- alertDialog.show()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement