Generic Adapter and generic Adapter

Source: Internet
Author: User

Generic Adapter and generic Adapter


Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/lv_main_apps"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" >    </ListView></LinearLayout>

Item_app.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="70dp"    android:minHeight="70dp"    android:orientation="horizontal"     android:gravity="center_vertical">    <ImageView        android:id="@+id/iv_item_icon"        android:layout_width="70dp"        android:layout_height="70dp"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/tv_item_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="TextView" /></LinearLayout>

AppInfo

package com.example.app3_listview;import android.graphics.drawable.Drawable;public class AppInfo {private Drawable icon;private String appName;private String packageName;public AppInfo(Drawable icon, String appName, String packageName) {super();this.icon = icon;this.appName = appName;this.packageName = packageName;}public AppInfo() {super();}public Drawable getIcon() {return icon;}public void setIcon(Drawable icon) {this.icon = icon;}public String getAppName() {return appName;}public void setAppName(String appName) {this.appName = appName;}public String getPackageName() {return packageName;}public void setPackageName(String packageName) {this.packageName = packageName;}@Overridepublic String toString() {return "AppInfo [icon=" + icon + ", appName=" + appName+ ", packageName=" + packageName + "]";}}

MainActivity

Package com. example. app3_listview; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. context; import android. content. intent; import android. content. pm. packageManager; import android. content. pm. resolveInfo; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. li StView; import android. widget. toast; import com. example. adapter. base. commonBaseAdapter; import com. example. adapter. base. viewHolder; public class MainActivity extends Activity {private ListView lv_main_apps; private List <AppInfo> data; private MyAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // load the view-only one listviewsetContentView (R. layout. acti Vity_main); // obtain idlv_main_apps = (ListView) findViewById (R. id. lv_main_apps); // obtain data = getAllAppInfos (); // create adapteradapter = new MyAdapter (this, data, R. layout. item_app); // set the adapter to display lv_main_assist.setadapter (adapter); // set the click listener for each item (item) to lv_main_assist.setonitemclicklistener (new AdapterView. onItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Parent, View view, int position, long id) {// subscript clicked by position // obtain the data AppInfo appInfo = data. get (position); String appName = appInfo. getAppName (); // display Toast. makeText (MainActivity. this, appName, 0 ). show () ;}}) ;}/ ** get the List of all application information in the mobile phone. AppInfo */protected List <AppInfo> getAllAppInfos () {List <AppInfo> list = new ArrayList <AppInfo> (); // obtain the application packgeManagerPackageManager packageManager = getPackageManager (); // create intentIntent intent = new Intent (); intent. setAction (Intent. ACTION_MAIN); intent. addCategory (Intent. CATEGORY_LAUNCHER); // obtain the List containing application information <ResolveInfo> ResolveInfos = packageManager. queryIntentActivities (intent, 0); // traverse for (ResolveInfo ri: ResolveInfos) {// obtain the package name String packageName = ri. activityInfo. packageName; // get the icon Drawable icon = ri. loadIcon (packageManager); // obtain the application name String appName = ri. loadLabel (packageManager ). toString (); // encapsulate the application information object AppInfo appInfo = new AppInfo (icon, appName, packageName); // Add it to listlist. add (appInfo);} return list;}/*** inherits the adapter class from CommonBaseAdapter */class MyAdapter extends CommonBaseAdapter <AppInfo> {public MyAdapter (Context context, final List <AppInfo> data, int layoutId) {super (context, data, layoutId, new Convert () {@ Overridepublic void convert (ViewHolder holder, int position) {AppInfo appInfo = data. get (position); holder. setText (R. id. TV _item_name, appInfo. getAppName ()). setImageDrawable (R. id. iv_item_icon, appInfo. getIcon ());}});}}}

CommonBaseAdapter

Package com. example. adapter. base; import java. util. list; import android. content. context; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter;/*** common baseAdapter */public abstract class CommonBaseAdapter <T> extends BaseAdapter {private Context context; private List <T> data; private int layoutId; public Convert convert; /*** sets the abstract method of View data, which is implemented by the specific adapter subclass */public interface Convert {void convert (ViewHolder holder, int position );} public CommonBaseAdapter (Context context, List <T> data, int layoutId, Convert convert) {this. context = context; this. data = data; this. layoutId = layoutId; this. convert = convert;} @ Overridepublic int getCount () {return data. size () ;}@ Overridepublic T getItem (int position) {return data. get (position) ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// obtain the Viewholder object ViewHolder holder = ViewHolder. getHolder (context, convertView, layoutId); // call the unimplemented abstract method to set data convert. convert (holder, position); // return convertViewreturn holder in holder. getConvertView ();}}

ViewHolder

Package com. example. adapter. base; import android. content. context; import android. graphics. drawable. drawable; import android. util. sparseArray; import android. view. view; import android. widget. imageView; import android. widget. textView;/*** common ViewHolder class */public class ViewHolder {// represents the view object private View itemConvertView of the current row; // used to replace Map <Integer, object> containers are more efficient than map private SparseArray <View> views; /*************************************** * *************************** to get the ViewHolder object context -- layoutId: this is used to construct the layout file * convertview: it is a reusable item Layout */public static ViewHolder getHolder (Context context, View convertView, int layoutId) {/*** if there is no reusable layout, you can create this class object. if there is one, you can get it through getTag **/if (convertView = null) {return new ViewHolder (context, layoutId);} else {ViewHolder holder = (ViewHolder) convertView. getTag (); return holder ;}}/*** constructor loads the layout file and saves the class tag, namely, viewholder, to itemConvertView */private ViewHolder (Context context, int layoutId) {this. itemConvertView = View. inflate (context, layoutId, null); this. itemConvertView. setTag (this); views = new SparseArray <View> () ;}/ *** get the current convertView */public View getConvertView () {return itemConvertView ;} /*************************** % * * ******************************* obtain the corresponding view object according to the view id * /public <T extends View> T getView (int viewId) {View view = views. get (viewId); if (view = null) {view = itemConvertView. findViewById (viewId); views. put (viewId, view);} return (T) view;} // *** set text data */public ViewHolder setText (int viewId, String text) {TextView textView = getView (viewId); textView. setText (text); return this;}/*** set drawable image */public ViewHolder setImageDrawable (int viewId, Drawable drawable) {ImageView imageView = getView (viewId); imageView. setImageDrawable (drawable); return this;}/*** set resource image */public ViewHolder setImageResource (int viewId, int resourceId) {ImageView imageView = getView (viewId); imageView. setImageResource (resourceId); return this ;}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.