我是 android 领域的新手,我需要使用领域实现数据库侦听器。我怎样才能用科特林做到这一点?
I am new to android and Realm I need to implement a database listner with realm. How can i do it with kotlin?
fun createListner(toDoList:SwipeMenuListView,arry:ArrayList<Task>){
toDoList.adapter = TodoListAdapter(cont,arry)
this.passData(toDoList,arry)
swipeList(toDoList,arry)
}
以上是我在片段中更新 ListAdapter 的地方 class“cont”是我在 oncreate 视图中初始化的上下文变量。
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();
}
}
这是 Looper class,我真的不知道 Looper 是如何工作的,它可能错得离谱。
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
}
以上是我的领域数据库监听器,它可能是完全错误的,我不知道如何实现这个
// 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
// }
然后
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())
}
}
和
class TodoListAdapter: RealmRecyclerViewAdapter<Task, TaskViewHolder>() {
...
}
参见 https://github.com/realm/realm-android-adapters RealmRecyclerViewAdapter
如果您确实需要 RealmResults 作为 LiveData<List<T>>
然后检查 https://github.com/Zhuinden/realm-monarchy
fun createListner(toDoList:SwipeMenuListView,arry:ArrayList<Task>){
toDoList.adapter = TodoListAdapter(cont,arry)
this.passData(toDoList,arry)
swipeList(toDoList,arry)
}
以上是我在片段中更新 ListAdapter 的地方 class“cont”是我在 oncreate 视图中初始化的上下文变量。
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();
}
}
这是 Looper class,我真的不知道 Looper 是如何工作的,它可能错得离谱。
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
}
以上是我的领域数据库监听器,它可能是完全错误的,我不知道如何实现这个
// 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
// }
然后
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())
}
}
和
class TodoListAdapter: RealmRecyclerViewAdapter<Task, TaskViewHolder>() {
...
}
参见 https://github.com/realm/realm-android-adapters RealmRecyclerViewAdapter
如果您确实需要 RealmResults 作为 LiveData<List<T>>
然后检查 https://github.com/Zhuinden/realm-monarchy