Examples of baseadapter usage in Android _android

Source: Internet
Author: User
Tags sqlite database xml file example

This example describes the Baseadapter usage in Android. Share to everyone for your reference, specific as follows:

Overview:

The primary purpose of baseadapter is to upload a set of data to a UI display component such as ListView, Spinner, Gallery, and GridView, which is an inherited from interface class, for the basic data adapters that are often used in Android applications adapter

Baseadapter

Java code:

public class Recentadapter extends Baseadapter {private class Recentviewholder {TextView appName;
    ImageView AppIcon;
  TextView appsize;
  Private list<resolveinfo> mapplist;
  Private Layoutinflater Minflater;
  Private Packagemanager pm;
    Public Recentadapter (Context C, list<resolveinfo> Applist, Packagemanager pm) {mapplist = applist;
    this.pm = PM;
  Minflater = (Layoutinflater) c. Getsystemservice (Context.layout_inflater_service);
    public void Clear () {if (mapplist!=null) {mapplist.clear ();
  } public int GetCount () {return mapplist.size ();
  @Override public Object getitem (int position) {return mapplist.get (position);
  @Override public long getitemid (int position) {//TODO auto-generated method stub return position;
    Public View GetView (int position, View Convertview, ViewGroup parent) {Recentviewholder holder; if (Convertview = = null) {Convertview =Minflater.inflate (R.layout.app_info_item, NULL);
      Holder = new Recentviewholder ();
      Holder.appname = (TextView) Convertview.findviewbyid (r.id.app_name);
      Holder.appicon = (ImageView) convertview. Findviewbyid (R.id.app_icon);
      Holder.appsize = (TextView) Convertview.findviewbyid (r.id.app_size);
    Convertview.settag (holder);
    else {holder = (Recentviewholder) convertview.gettag ();
    } resolveinfo AppInfo = Mapplist.get (position);
      if (appInfo!= null) {String labelname = Appinfo.loadlabel (PM). toString ();
      if (labelname!= null) {Holder.appName.setText (labelname);
      } drawable icon = Appinfo.loadicon (PM);
      if (icon!= null) {holder.appIcon.setImageDrawable (icon);
  } return Convertview;
    public void Remove (int position) {mapplist.remove (position);
  This.notifydatasetchanged ();

 }
}

The two points of attention are:

Settag store data with view settings

Notifydatasetchanged () tells view data changes and refreshes

View Convertview = minflater.inflate (R.layout.app_info_item, NULL) load XML Item view

App_info_item.xml File Example:

XML code:

<?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 "android:layout_gravity=" Center_ Vertical "android:minheight=" "Android:attr/listpreferreditemheight" > <imageview android:id= "@+id/app_icon" Android:layout_width= "@android:d imen/app_icon_size" android:layout_height= "@android:d imen/app_icon_size" Android : layout_alignparentleft= "true" android:paddingleft= "6dip" android:paddingtop= "6dip" android:paddingbottom= "6dip" a Ndroid:scaletype= "Fitcenter"/> <textview android:id= "@+id/app_name" android:layout_width= "Wrap_content" Andr oid:layout_height= "Wrap_content" "android:textappearance="? Android:attr/textappearancelarge "android:textColor="? Android:attr/textcolorprimary "android:layout_torightof=" @id/app_icon "android:paddingleft=" 6dip "android:padding top= "6dip"/> <textview android:id= "@+id/app_dEscription "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:textAppearance="? and Roid:attr/textappearancesmall "android:layout_below=" @+id/app_name "android:layout_torightof=" @id/app_icon "Andro id:paddingleft= "6dip" android:paddingbottom= "6dip"/> <textview android:id= "@+id/app_size" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:textappearance= "? android:attr/ Textappearancesmall "android:layout_alignparentright=" true "android:layout_below=" @+id/app_name "Android:paddingR

 ight= "6dip" android:maxlines= "1"/> </RelativeLayout>

More interested readers of Android-related content can view the site: "Summary of Android Control usage", "Android View Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced course" and " Android Resource Operating Skills summary

I hope this article will help you with the Android program.

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.