fun createListner(toDoList:SwipeMenuListView,arry:ArrayList<Task>){
toDoList.adapter = TodoListAdapter(cont,arry)
this.passData(toDoList,arry)
swipeList(toDoList,arry)
}
This above is the where I update my ListAdapter its in a fragment class "cont" is the context variable i initialized in my oncreate view.
public class DbLooper extends Thread {
public Handler mHandler;
TodoFragment tofrg = new TodoFragment();
dict content;
@SuppressLint("HandlerLeak")
public void run() {
Looper.prepare();
mHandler = new Handler() {
public void handleMessage(Message msg) {
content = (dict) msg.obj;
tofrg.createListner(content.getView(),content.getArry());
// I called the method in fragment inside this looper
}
};
Looper.loop();
}
}
This is looper class and i really dont have any idea how looper works it probebly wrong as hell.
public fun getTaskListner(view: SwipeMenuListView):LiveData<ArrayList<Task>>{
val looper = DbLooper()
val msg = Message()
val newdict = dict
val result = realm.where<Task>()
.findAllAsync()
result.addChangeListener { tasksn ->
tasks.addAll(tasksn)
taskList.value = tasks
newdict.arry = tasks
newdict.view = view
msg.obj = newdict
looper.mHandler.handleMessage(msg)
}
tasks.addAll(result)
taskList.value = tasks
return taskList
}
Above is my realm database listner it might be completely wrong i dont have a clue to implement this
// fun createListner(toDoList:SwipeMenuListView,arry:ArrayList<Task>){
// toDoList.adapter = TodoListAdapter(cont,arry)
// this.passData(toDoList,arry)
// swipeList(toDoList,arry)
// }
//
// public class DbLooper extends Thread {
//
// public Handler mHandler;
//
// TodoFragment tofrg = new TodoFragment();
// dict content;
//
// @SuppressLint("HandlerLeak")
// public void run() {
// Looper.prepare();
//
// mHandler = new Handler() {
// public void handleMessage(Message msg) {
//
// content = (dict) msg.obj;
// tofrg.createListner(content.getView(),content.getArry());
// // I called the method in fragment inside this looper
//
//
// }
// };
//
// Looper.loop();
// }
// }
//
// public fun getTaskListner(view: SwipeMenuListView):LiveData<ArrayList<Task>>{
//
// val looper = DbLooper()
// val msg = Message()
// val newdict = dict
// val result = realm.where<Task>()
// .findAllAsync()
// result.addChangeListener { tasksn ->
// tasks.addAll(tasksn)
// taskList.value = tasks
//
// newdict.arry = tasks
// newdict.view = view
// msg.obj = newdict
//
// looper.mHandler.handleMessage(msg)
// }
//
// tasks.addAll(result)
// taskList.value = tasks
// return taskList
// }
Then
class TodoFragment: Fragment(R.layout.fragment_todo) {
private lateinit var realm: Realm
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
realm = Realm.getDefaultInstance()
}
override fun onDestroy() {
super.onDestroy()
realm.close()
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentTodoBinding.bind(view)
binding.toDoList.adapter = TodoListAdapter(realm.where<Task>().findAllAsync())
binding.toDoList.layoutManager = LinearLayoutManager(requireContext())
}
}
And
class TodoListAdapter: RealmRecyclerViewAdapter<Task, TaskViewHolder>() {
...
}
See https://github.com/realm/realm-android-adapters for RealmRecyclerViewAdapter
If you do need RealmResults as a LiveData<List<T>>
then check https://github.com/Zhuinden/realm-monarchy