首页 > 代码库 > android如何在BaseAdapter内使用notifyDataSetChanged()方法以及线程的使用

android如何在BaseAdapter内使用notifyDataSetChanged()方法以及线程的使用

有时候我们需要修改列表里的数据,并实时刷新,我们除了在在外部调用adapter.notifyDataSetChanged()方法来通知activity刷新,  如果在adapter内部有涉及到更新数据,删除或者增加数据,就可以直接在adapter内部调用notifyDataSetChanged()这个方法,前提是该listview绑定的数据有改变。


以下例子结合Handler,线程展示


public class MyAdapter extends BaseAdapter {
	private final static String tag = "MyAdapter";
	private List<Alarm> alarms;
	private static final int MODIFY_FAIL = 13;
	private static final int MODIFY_SUCCESS = 12;
	private static final int DELETE_SUCCESS= 10;
	private static final int STUDY_SUCCESS = 11;
	private Context context;
	private LayoutInflater inflater;
	private TextView tvDeviceName;
	int msgType;
	private Alarm alarm;
	private Dialog confirmDlg;
	// BaseHandler baseHandler;
	public MyAdapter(Context context, List<Alarm> alarms) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.alarms = alarms;
		inflater = LayoutInflater.from(context);
	}
	
	.......

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		alarm = alarms.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.alarm_manage_item, parent,
					false);

		}
		tvDeviceName = (TextView) convertView.findViewById(R.id.tv_device_name);
		tvDeviceName.setText(alarm.getName());	
		.......
		
		return convertView;
	}

由于代码太多,中间部分省略了


以下是Handler部分,通过Handler,Thread,Message可进行异步操作 ,记住,在Handler的CallBack()方法内不能对UI进行操作,但是可以发送一个空消息到消息队列

contextHandler.sendEmptyMessage(MODIFY_FAIL);
,这样

handleMessage()方法就能够处理消息队列中的消息了,在这个方法里面可对UI进行操作,

</pre><p></p><h5>要在BaseAdapter内进行通知刷新,关键在于一句 .this.notifyDataSetChanged();</h5><pre name="code" class="java">
</pre><p></p><p></p><pre name="code" class="java">private BaseHandler contextHandler = new BaseHandler(context) {
		@Override
		public void callBack(String recvHex) {
			// TODO Auto-generated method stub
			super.callBack(recvHex);
			recvHex = recvHex.toUpperCase().trim();
			String[] strs = recvHex.split(" ");
			msgType = OutPutProtocol.analysisOutPutOperInfo(recvHex);
			// 修改名称应答
			if (DeviseSettingProtocol.modifyDeviceNameMsg(recvHex) == Resp.Rst_Success) {
				if(strs[3].equals("00"))
				{
				SettingThread thread = new SettingThread(
						contextHandler,// 返回结果handler
						context, SettingThread.OperType_EditAlarm, alarm);
				thread.start();
				ProgressDialog.dismissProgressDialog();
				}
				else if(strs[3].equals("02")||strs[3].equals("FF"))
				{
					contextHandler.sendEmptyMessage(MODIFY_FAIL);//将一个空消息送到消息队列
				}
			}
	}
		
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);

			switch (msg.what) {
			//修改名称失败
			case MODIFY_FAIL:
			{
				Toast.makeText(context, "修改失败", Toast.LENGTH_SHORT).show();
			}
			case Resp.Rst_Fail:
			{
				Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
			}
			case SettingThread.OperType_DelAlarm:
			{
				MyAdapter.this.notifyDataSetChanged();
				Toast.makeText(context, "删除成功!",
						Toast.LENGTH_SHORT).show();
			}
			case SettingThread.OperType_EditAlarm:
			{
				MyAdapter.this.notifyDataSetChanged();
				Toast.makeText(context, "修改成功!",
						Toast.LENGTH_SHORT).show();
			}
			default:
				break;
			}
		}

	};


// 通过线程删除数据库中的数据
				SettingThread thread = new SettingThread(
						contextHandler,// 返回结果handler
						context, SettingThread.OperType_DelAlarm, alarm);
				thread.start();

线程机制,将Handler和一个标识符

SettingThread.OperType_DelAlarm
传到线程里,在该线程执行耗时操作,并将操作用Msg送到消息队列,然后返回给UI线程,这时UIActivity的Handler就可以取出消息队列中的消息(对应的标识符),然后就可以对UI进行操作了,这就是异步操作