1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| // 创建一个线程来连接数据库并获取数据库中对应表的数据 new Thread(new Runnable() { @Override public void run() { // 调用数据库工具类DBUtils的getInfoByName方法获取数据库表中数据 HashMap<String, Object> map = Databaseutil.getInfoByName("hh");
Message message = handler.obtainMessage(); if(map != null){ String s = ""; for (String key : map.keySet()){ s += key + ":" + map.get(key) + "\n"; } message.what = 0x12; message.obj = s; }else { message.what = 0x11; message.obj = "查询结果为空"; } // 发消息通知主线程更新UI handler.sendMessage(message); } }).start();
//写在成员变量中
@SuppressLint("HandlerLeak") private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) {
switch (msg.what){ case 0x11: String s = (String) msg.obj; et.setText(s); break; case 0x12: String ss = (String) msg.obj; et.setText(ss); break; }
} };
|