I am new to android development and making a simple To-do list app to get my bearings.
I have a fragment from my MainActivity that contains a list, called FragToDoList. I also have a class DialogAddToDo that extends DialogFragment to bring up a dialog to input your to do list item infomation. I am having trouble passing the data entered into DialogAddTodo back to the FragToDoList class to then display it in the list.
It is saying my interface is not initialized, and cant find a way to resolve this issue. Does anyone have some pointers?
class FragToDoList : Fragment(), DialogAddTodo.ToDoAddedListener {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
private val toDoList: MutableList<ToDoItem>? = null
lateinit var adapter: ToDoAdapter
private lateinit var listViewItem: ListView
lateinit var mContext: Context
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.fragment_frag__to_do_list, container, false)
listViewItem = view.findViewById(R.id.listView)
//for showing items in list view
// adapter = ToDoAdapter(mContext, toDoList!!)
// listViewItem.adapter = adapter
val fab = view.findViewById<FloatingActionButton>(R.id.fabAddTask)
fab?.setOnClickListener {
showAddTaskDialog(childFragmentManager);
}
// Inflate the layout for this fragment
return view
}
private fun showAddTaskDialog(childFragmentManager:FragmentManager) {
var dialog = DialogAddTodo()
dialog.show(childFragmentManager,DialogAddTodo.TAG)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragTodoList.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
FragToDoList().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
override fun onToDoAdded(item: ToDoItem) {
toast("added",mContext)
}
}
class DialogAddTodo : DialogFragment() {
interface ToDoAddedListener {
fun onToDoAdded(item: ToDoItem)
}
private lateinit var onTodoAdded: ToDoAddedListener
private val todoItemModel: TodoItemModel? =null
companion object {
const val TAG = "Dialog Add Task"
}
lateinit var mContext: Context
override fun onAttach(context: Context) {
super.onAttach(context)
mContext = context
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.dialog_add_task, container, false)
var addBut = view.findViewById<Button>(R.id.butAddTaskDialog)
addBut.setOnClickListener()
{
val todoItemData = ToDoItem.createToDoItem()
var taskNameText =
view.findViewById<TextView>(R.id.editTextTaskName)
if (taskNameText != null)
todoItemData.itemDataText = taskNameText?.text.toString()
todoItemData.done = false
todoItemData.UID = getRandomString(Int.SIZE_BITS - 1)
// Frag_ToDoList().adapter.notifyDataSetChanged()
//b.setItem(todoItemData)
//todoItemModel.setItem(todoItemData)
onTodoAdded.onToDoAdded(todoItemData)
this.dismiss()
}
return view
}
}
CodePudding user response:
You declared lateinit variable
private lateinit var onTodoAdded: ToDoAddedListener
however it remains uninitialized.
When uninitialized var accessed it throws an exception.
You need to assign variable for example using accessor:
var dialog = DialogAddTodo()
// Initialize listener
dialog.setListener(this)
dialog.show(childFragmentManager,DialogAddTodo.TAG)
In DialogAddTodo add following method:
fun setListener(onTodoAdded: ToDoAddedListener) {
this.onTodoAdded = onTodoAdded
}