Custom data adapter in Android

Source: Internet
Author: User

Application entity class

Public class app {

Private int appid; // Application ID

Private string appname; // Application name

Private string appicon; // application icon

 

Public int getappid () {return this. appid ;}

Public void setappid () {This. appid = value ;}

 

Public int getappname () {return this. appname ;}

Public void setappname () {This. appid = appname ;}

 

Public int getappicon () {return this. appicon ;}

Public void setappicon () {This. appid = appicon ;}

 

}

App_item.xml File

<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">

<Imageview
Android: Id = "@ + ID/imgicon"
Android: layout_width = "50px"
Android: layout_height = "50px"
Android: layout_alignparentleft = "true"
Android: layout_alignparenttop = "true"
Android: layout_marginleft = "5dip"
Android: layout_margintop = "2dip"
Android: src = "@ drawable/portrait"/>

<Textview
Android: Id = "@ + ID/txtname"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginleft = "10dip"
Android: layout_centervertical = "true"
Android: layout_torightof = "@ ID/imgportrait"
Android: textcolor = "@ Android: color/Black"
Android: textsize = "16dip"
Android: gravity = "center"
Android: text = ""/>

<Button
Android: Id = "@ + ID/btndel"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignparentright = "true"
Android: layout_centervertical = "true"
Android: layout_marginright = "10dip"

Android: text = "delete"
Android: textsize = "12dip"
Android: focusable = "false"
Android: focusableintouchmode = "false"/>

// Note: when Android: focusable = "false" and Android: focusableintouchmode = "false" are set, conflicts with the listview item click event can be avoided, resulting in invalid item click events.

</Relativelayout>

Custom data adapter

Public class appadapter extends baseadapter implements view. onclicklistener {

Private context;
Private list <app> applist;
Private final string Inflater = context. layout_inflater_service;
Private layoutinflater;
Private handler;

Private asyncimageloader imageloader; // class for Asynchronously loading Images

// View container class, attribute corresponds to layout file Element

Private class viewholder {
Imageview imgicon;
Textview txtname;
Button btndel;
}

 

// Constructor

Public appadapter (context c, list <app> List ){
If (null! = List ){
Applist = List;
} Else {
Applist = new arraylist <app> ();
}
This. Context = C;
Layoutinflater = (layoutinflater) Context. getsystemservice (Inflater );

Handler = new handler ();

Imageloader = new asyncimageloader ();
}

// Add a single item (custom method)

Public void additem (APP item ){
If (item! = NULL ){
Applist. Add (item );
Notifydatasetchanged (); // notifies the adapter that data has changed
}
}

// Add multiple items (custom methods)

Public void additem (list <app> List ){
If (null! = List & list. Size ()> 0 ){
For (INT I = 0; I <list. Size (); I ++ ){
Applist. Add (list. Get (I ));
}
Notifydatasetchanged (); // notifies the adapter that data has changed

}
}

// Delete an item (custom method)

Public void removeitem (INT position ){
If (applist. Get (position )! = NULL ){
Applist. Remove (position );
Notifydatasetchanged (); // notifies the adapter that data has changed
}
}

// Obtain the total number

Public int getcount (){
Return applist. Size ();
}

// Obtain a single data record

Public app getitem (INT position ){

Return applist. Get (position );
}

// Obtain the ID of the current position (custom method)

Public int getitemid (INT position ){

Return applist. Get (position). getappid ();
}

// Obtain the view

Public View getview (INT position, view convertview, viewgroup parent ){
Viewholder holder;
If (null = convertview ){
// Load the layout file app_item.xml
Convertview = (relativelayout) layoutinflater. Inflate (R. layout. app_item, null );
Holder = new viewholder ();

Holder. imgicon = (imageview) convertview. findviewbyid (R. Id. imgicon );
Holder.txt Nick = (textview) convertview.findviewbyid(r.id.txt Nick );
Holder. btndel = (button) convertview. findviewbyid (R. Id. btndel );
Convertview. settag (holder );
} Else {
Holder = (viewholder) convertview. gettag ();
}
App = applist. Get (position); // get the current item data
If (null! = APP ){
Holder.txt name. settext (App. getappname ());

Holder. btndel. setonclicklistener (this); // Add button click event

Holder. btndel. settag (App. getappid (); // The set button "tag" is the Application ID, which is convenient for deletion.

Imageloader. LoadImage (App. getappicon (), Holder. imgicon); // asynchronously load the image
}
Return convertview;
}

// Implement the view. onclicklistener Interface

Public void onclick (view v ){
Button BTN = (button) V;

// Obtain the ID of the deleted item
Int id = integer. parseint (BTN. gettag (). tostring ());

// Call the deletion method

Removeitem (ID );

}
}

 

The following code is called in the activity class:

List <app> List = new list <app> (); // get data

Appadapter adapter = new appadapter (this, list );

Listview. setadapter (adapter); // listview is a listview object instance

 

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.