Android calls the activity method/variable in the adapter.

Source: Internet
Author: User

There is a popupwindow in the activity.

When you want to set a checbox Click Event in getview of the adapter, this popupwindow is displayed and hidden.

 

However, because the adapter and activity are not the same class, the operation is a little troublesome ..

 

-------------------------- Split line --------------------------------------------

 

Class: the adapter inherits the baseadapter

Class: The activity inherits the activity

 

------------------------------ In the adapter --------------------------

 

Weakreference <activity> weak; // defines weak reference variables.

 

Public adapter (context ){

This. Weak = new weakreference <activity> (activity) context );

........

}

In the getview Method

In the checkbox listener

Final atistmactivity = weak. Get ();

Activity. **(); /// // The method in the activity is called here ////////////////////////////

 

 

 

------------------------------------------- The preceding method is always used, but it is too complicated. I have heard from others these days that there is another simpler method ----------------------

 

The red code is the part of the call.

 

Activity:

Public class myactivity extends Activity

{

Public void test (){};

Public void test (INT position ){}

}

Baseadater:

Public class mybaseadapter extends baseadapter {

Myactivity context;

Public mybaseadapter (MyactivityContext ){

This. Context = context;

}

// Call the myactivity method in other functions.

Context. Test ();

Context. Text (1 );

 

 

 

}

Related Article

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.