How Android uses the Notifydatasetchanged () method and thread usage within Baseadapter

Source: Internet
Author: User

Sometimes we need to modify the data in the list and refresh it in real time, except that we call the Adapter.notifydatasetchanged () method on the outside to notify the activity refresh, if it involves updating data, deleting or adding data inside adapter, The Notifydatasetchanged () method can be called directly inside the adapter, provided that the data of the ListView binding is changed.


The following example combines handler, thread display


public class Myadapter extends Baseadapter {Private final static String tag = "Myadapter";p rivate list<alarm> Alarms ;p rivate static final int modify_fail = 13;private static final int modify_success = 12;private static final int delete_su ccess= 10;private static final int study_success = 11;private Context context;private layoutinflater inflater;private Text View 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 (c Onvertview = = 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;} 

Because there is too much code, the middle part omits the


The following is the handler section, which allows asynchronous operation through Handler,thread,message, remembering that the UI cannot be manipulated within the handler callback () method, but can send an empty message to the message queue

Contexthandler.sendemptymessage (Modify_fail);
, so

The Handlemessage () method is capable of handling messages in Message Queuing, where the UI can be manipulated,

</pre><p></p>
</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);//Modify name Answer if ( Devisesettingprotocol.modifydevicenamemsg (recvhex) = = resp.rst_success) {if (Strs[3].equals ("xx")) {SettingThread thread = new Settingthread (contexthandler,//returns results Handlercontext, settingthread.opertype_editalarm, alarm); Thread.Start (); Progressdialog.dismissprogressdialog ();} else if (strs[3].equals ("02") | | Strs[3].equals ("FF")) {contexthandler.sendemptymessage (modify_fail);//Send an empty message to the message Queue}}} @Overridepublic void Handlemessage (Message msg) {//TODO auto-generated Method Stubsuper.handlemessage (msg); switch (msg.what) {//Modify name failure case Modify_fail:{toast.maketext (Context, "modification failed", Toast.length_short). Show (); Case Resp.rst_fail:{toast.maketext (context, "Delete failed", Toast.length_short). Show (); Case Settingthread.opertype_delalarm:{myadapter.this.notifydatasetchangEd (); Toast.maketext (context, "Delete succeeded!", Toast.length_short). Show (); Case settingthread.opertype_editalarm:{myadapter.this.notifydatasetchanged (); Toast.maketext (Context, "modified successfully!", Toast.length_short). Show (); Default:break;}};


Remove data from the database by thread settingthread thread = new Settingthread (contexthandler,//returns the result Handlercontext, Settingthread.opertype _delalarm, alarm); Thread.Start ();

Threading mechanism that will handler and an identifier

Settingthread.opertype_delalarm
To the thread, the thread executes the time-consuming operation, and the action is sent to the message queue with MSG and then returned to the UI thread, then Uiactivity's handler can take out the message in the message queue (the corresponding identifier), and then the UI can be manipulated, which is the asynchronous operation


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.