Android Studio主线程不允许耗时操作,最后通过多线程实现了Android Studio远程连接数据库
但是主线程可以分线程传值,分线程却不可以直接向主线程传值
Android Studio通过handler和massage解决了这个问题
话不多说,看代码——
private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
//这一步是为了在有多个分线程传值时判断是哪一条分线程
if(msg.what == 0){
goodlist = (List<GoodBean>)msg.obj; //获取分线程传递是数值
adapter.notifyDataSetChanged();
}
}
};
private void loadDataToGV(){
new Thread(new Runnable(){
@Override
public void run(){
List<GoodBean> list=new ArrayList<>();
adapter=new GoodsBaseAdapter(getContext(),list);
goods_gv.setAdapter(adapter);
//获取数据库中的数据
List<GoodBean> gl=Dao.getGoodsList();
list.addAll(gl);
//调用massage
Message message=new Message();
message.what=0;
message.obj=list;
handler.sendMessage(message); //将信息封装在message中向主线程发送
}
}).start();
}
good_gv 是 GrigView 的按钮实例化,adapter是相应的adapter对象
这样就可以实现啦
参考——
https://www.bilibili.com/video/BV1j54y1H7Kj/?spm_id_from=333.880.my_history.page.click&vd_source=c2fca9b4cf27a6496643dde809415b05
https://blog.csdn.net/ZQL2288/article/details/72782975
标签:adapter,handler,线程,new,Android,多线程,message From: https://www.cnblogs.com/yansans/p/17346285.html