android如何在BaseAdapter內使用notifyDataSetChanged()方法以及執行緒的使用
阿新 • • 發佈:2019-02-16
有時候我們需要修改列表裡的資料,並實時重新整理,我們除了在在外部呼叫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進行操作了,這就是非同步操作