Simply use Rxjava to say goodbye to cumbersome interface callbacks (1)

Source: Internet
Author: User

Interface callback Although cumbersome, but also must master a skill, interface callback use a lot of, first figure out when to use interface callback, how to use interface callback.

 Public classInterquerydataactivityextendsappcompatactivity {@Overrideprotected voidonCreate (@Nullable Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.layout_interquery);    Initview (); } Handler Handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Name_tv.settext (Msg.getdata (). GetString ("Name"));    }    }; PrivateTextView Name_tv; Private voidInitview () {Button query_btn=(Button) Findviewbyid (R.ID.QUERY_BTN); NAME_TV=(TextView) Findviewbyid (R.ID.NAME_TV); Query_btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {internetquerydata Internetquerydata=NewInternetquerydata (NewInternetquerydata.querysuccesslistener () {@Override Public voidonquerysuccess (String msg) {//Name_tv.settext (msg);Message message =NewMessage (); Bundle Bundle=NewBundle (); Bundle.putstring ("Name", MSG);                        Message.setdata (bundle);                    Handler.sendmessage (message);                }                });            Internetquerydata.querydata ();    }        }); }}
 Public classInternetquerydata { Public Interfacequerysuccesslistener{voidonquerysuccess (String msg); }    PrivateQuerysuccesslistener Querysuccesslistener;  PublicInternetquerydata (Querysuccesslistener querysuccesslistener) { This. Querysuccesslistener =Querysuccesslistener; }     Public voidQuerydata () {//thread sleep mimics network requests        NewThread (NewRunnable () {@Override Public voidrun () {Try{Thread.Sleep (4000); String msg= "Programmer Tycoon";                Querysuccesslistener.onquerysuccess (msg); } Catch(interruptedexception e) {e.printstacktrace ();    }}). Start (); }}
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical" android:layout_width= "match_parent"Android:layout_height= "Match_parent" > <button android:id= "@+id/query_btn"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Request Data"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Name:"/> <textview android:id= "@+id/name_tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= ""/> </LinearLayout></LinearLayout>

In the example above, when the button is pressed in interquerydataactivity to make a request (mimicking a network request), the Internetquerydata network requests the management class to perform the operation. We create a new thread to simulate the network connection request data process.

When we sleep 4s, we get the data, how to return to the activity to display the UI, this time we need to use the interface callback.

We create an interface Querysuccesslistener, define an abstract method in this interface onquerysuccess, the parameter msg, is the data you need to use, of course, here can be any parameter, you define it.

Then we need to call our network request management class in the constructor, we must pass in an instance object of this interface, rewrite the Onquerysuccess method, get the data we need in this method and do the operation we need.

In this example, we pass the name to the TextView in four seconds and show it on top of the table.

Simply use Rxjava to say goodbye to cumbersome interface callbacks (1)

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.