您的位置:首页 > 移动开发 > Android开发

Android 利用adapter.notifyDataSetChanged()无法更新问题

2015-11-16 10:21 507 查看
1、起始在onCreate(Bundle savedInstanceState)中定义有
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mdata=getData();//是数据获取
listView=(ListView)this.findViewById(R.id.listView);
adapter = new SimpleAdapter(this,mdata,R.layout.messa,
new String[]{"device_id","temperature","humidity","led"},
new int[]{R.id.device_id,R.id.temperature,R.id.humidity,R.id.led});

listView.setAdapter(adapter);
timerTask(); // 定时执行

}

定时器的操作如下

public void timerTask() {
//创建定时线程执行更新任务
mTimer.schedule(new TimerTask() {
@Override
public void run() {
if(count<=50){
System.out.println("TimerTask-->Id is "
+ Thread.currentThread().getId());// TimerTask在它自己的线程中
// mHandler.sendEmptyMessage(1);// 向Handler发送消息
Message msg=new Message();
msg.what =2;
mHandler.sendMessage(msg);
}else{
// mHandler.sendEmptyMessage(2);// 向Handler发送消息停止继续执行
}
count++;
}
}, 3000, 3000);// 定时任务
}

/**
* 销毁定时器的方式
*/
@Override
protected void onStop() {
mTimer.cancel();// 程序退出时cancel timer
super.onStop();
}

为simpleAdapter构建的适配器数据如下
public List<Map<String, Object>> getData() {
// TODO Auto-generated method stub
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

Map<String, Object> map = new HashMap<String, Object>();
map.put("device_id", "DEV01");
map.put("temperature", "25");
map.put("humidity", "50");
map.put("led", R.drawable.close);
list.add(map);

map = new HashMap<String, Object>();
map.put("device_id", "DEV02");
map.put("temperature", "27");
map.put("humidity", "45");
map.put("led", R.drawable.open);
list.add(map);

map = new HashMap<String, Object>();
map.put("device_id",  MainActivity.bufferRcv_string[1]);
System.out.println("字符串是"+ MainActivity.bufferRcv_string[0]);
map.put("temperature",  MainActivity.bufferRcv_string[2]);
map.put("humidity",  MainActivity.bufferRcv_string[3]);
map.put("led", R.drawable.open);
list.add(map);

return list;
}
上面的第三个的数据接收是定义成全局变量,就是当接收到外部数据时能用全局变量来改变数据,这样可以及时的吧数据床底到bufferRcv_string当中
public static String bufferRcv_string[] =new String[4];

下面就是接收到数据进行listview数据的及时更新
public Handler mHandler=new Handler(){
@Override
public void handleMessage(Message msg)
{
switch (msg.what) {
case 1:

//接收到的数据格式如下:ENDADDAC7E:52:92

String strRcv = (msg.obj).toString();
bufferRcv_string[0]=strRcv.substring(0, 6);
bufferRcv_string[1]=strRcv.substring(6, 10);
bufferRcv_string[2]=strRcv.substring(11, 13);
bufferRcv_string[3]=strRcv.substring(14, 16);
mdata=getData();//数据重新调用,不然hashmap中的数据不会发生变化
//对listview的重新刷新
adapter = new SimpleAdapter(MainActivity.this,mdata,R.layout.messa,
new String[]{"device_id","temperature","humidity","led"},
new int[]{R.id.device_id,R.id.temperature,R.id.humidity,R.id.led});
listView.setAdapter(adapter);
break;
case 2:
System.out.println("进入case2");

break;
//定时器关闭
case 3:
mTimer.cancel();//
mTimer=null;
break;

}

super.handleMessage(msg);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android listview