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 context;
Private List <App> appList;
Private final String inflater = Context. LAYOUT_INFLATER_SERVICE;
Private LayoutInflater layoutInflater;
Private Handler 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 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
From Berga