ListView Optimization Summary (ii)--android

Source: Internet
Author: User

3. Interact with the adapter using activity and delegate

This is what you see in the book, which helps developers move all of their business logic from the adapter to the activity through the commit mode.

Here is an example of adding a phone number, each row in the list has a RemoveButton, which removes the phone number and implements the Click processor of the "Remove" button in the adapter, but. The method of deleting objects is not implemented in the adapter, and we use a delegate interface to invoke the activity method to delete the object.

The code for the adapter:

public class Numbersadapter extends Arrayadapter<integer> {

public static interface Numbersadapterdelegate {//define entrusted 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);

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 (;
Tv.settext (Value.tostring ());

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

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

return CV;

public void Setdelegate (Numbersadapterdelegate delegate) {//Set the entrusted object for the 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;

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

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

protected void Onresume () {
Super.onresume ();
Madapter.setdelegate (this); Register the entrusted object in the Onresume method

protected void OnPause () {
Super.onpause ();
Madapter.setdelegate (NULL); Cancellation of the consignment object in the OnPause method

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);
. 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 entrusted object is not set in the OnCreate method, but the entrusted object is set in the Onresume method. The goal is to ensure that only when the activity is displayed on the screen will it be used as a trusted object.

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: 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.