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

來源:互聯網
上載者:User

標籤:adapter   notifydatasetchanged   線程   handler   訊息佇列   

有時候我們需要修改列表裡的資料,並即時重新整理,我們除了在在外部調用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 stubthis.context = context;this.alarms = alarms;inflater = LayoutInflater.from(context);}.......@Overridepublic 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) {
@Overridepublic void callBack(String recvHex) {// TODO Auto-generated method stubsuper.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,// 返回結果handlercontext, SettingThread.OperType_EditAlarm, alarm);thread.start();ProgressDialog.dismissProgressDialog();}else if(strs[3].equals("02")||strs[3].equals("FF")){contextHandler.sendEmptyMessage(MODIFY_FAIL);//將一個空訊息送到訊息佇列}}}@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.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,// 返回結果handlercontext, SettingThread.OperType_DelAlarm, alarm);thread.start();

線程機制,將Handler和一個標識符

SettingThread.OperType_DelAlarm
傳到線程裡,在該線程執行耗時操作,並將操作用Msg送到訊息佇列,然後返回給UI線程,這時UIActivity的Handler就可以取出訊息佇列中的訊息(對應的標識符),然後就可以對UI進行操作了,這就是非同步作業


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.