ListView Optimization Summary (ii)--android

Source: Internet
Author: User

3. Interact with the adapter using activity and delegate

This content is seen from the book and helps developers move all the business logic from the adapter to the activity through the delegate mode. Here is an example of adding a phone number, each row in the list has a remove button that removes the phone number, implements the Click Processor for the "Remove" button in the adapter, but does not implement the method of deleting the object in the adapter. We use a delegate interface to invoke the activity's method to delete the object.

The code for the adapter:

public class Numbersadapter extends Arrayadapter<integer> {


public static interface Numbersadapterdelegate {//define Delegate interface
void RemoveItem (Integer value);
}


Private Layoutinflater Minflator;
Private Numbersadapterdelegate mdelegate;


Public Numbersadapter (context context, list<integer> objects) {
Super (context, 0, objects);
Minflator = Layoutinflater.from (context);
}


@Override
Public View GetView (int position, View CV, viewgroup parent) {


if (null = = CV) {
CV = Minflator.inflate (R.layout.number_row, parent, false);
}


Final Integer value = GetItem (position);
TextView TV = (TextView) Cv.findviewbyid (R.id.numbers_row_text);
Tv.settext (Value.tostring ());


View button = Cv.findviewbyid (R.id.numbers_row_button);
Button.setonclicklistener (New Onclicklistener () {


@Override
public void OnClick (View v) {
if (null! = Mdelegate) {
Mdelegate.removeitem (value); Delete Object
}
}
});


return CV;
}


public void Setdelegate (Numbersadapterdelegate delegate) {//Set delegate object for adapter
Mdelegate = delegate;
}


}


Activity's Code:

public class Mainactivity extends Activity implements
Numbersadapter.numbersadapterdelegate {//Implement Numbersadapterdelegate interface
private static final String TAG = Mainactivity.class
. Getcanonicalname ();


Private ListView Mlistview;
Private arraylist<integer> mnumbers;
Private Numbersadapter Madapter;
Private EditText Medittext;


@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);


Medittext = (EditText) Findviewbyid (R.id.main_edittext);
Mlistview = (ListView) Findviewbyid (R.id.main_listview);
Mnumbers = new arraylist<integer> ();
Madapter = new Numbersadapter (this, mnumbers);
Mlistview.setadapter (Madapter);
}


@Override
protected void Onresume () {
Super.onresume ();
Madapter.setdelegate (this); Registering a delegate object in the Onresume method
}


@Override
protected void OnPause () {
Super.onpause ();
Madapter.setdelegate (NULL); Unregister a delegate object in the OnPause method
}


@Override
public void RemoveItem (Integer value) {//Removes the specified item from the list, and then notifies the adapter that the data of the binding has changed
Mnumbers.remove (value);
Toast
. Maketext (This, "removed object:" + value, Toast.length_short)
. Show ();
Madapter.notifydatasetchanged ();
}


public void Addnumber (View v) {
String value = Medittext.gettext (). toString (). Trim ();
try {
Mnumbers.add (integer.valueof (value));
Medittext.settext ("");
Madapter.notifydatasetchanged ();
} catch (NumberFormatException e) {
LOG.E (TAG, "couldn ' t convert to integer the string:" + value);
}
}
}


The delegate object is not set in the OnCreate method, and the delegate object is set in the Onresume method to ensure that it is used only as a delegate object when the activity is displayed on the screen.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ListView Optimization Summary (ii)--android

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.