The basic adapter BaseAdapter Android view a list of classes of

When it comes to the list view, it is easy to think the previously mentioned array adapter, but the adapter can only build an array of text options, scalability is not strong, Android provides an adaptable base adapter BaseAdapter, the adapter allows developers to other logic in the code, as the carrier Spinner below to demonstrate how to use BaseAdapter, specifically divided into the following three steps:

Layout file (1) to prepare a list of items

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <! - This is a picture of a planet image view -> 
    < the ImageView
         Android: the above mentioned id = "@ + the above mentioned id / iv_icon" 
        Android: layout_width = "0dp" 
        Android: layout_height = "match_parent" 
        Android: layout_weight = "1" / >
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">
        
        <! - This is the text view of the planetary name -> 
        < TextView
             Android: the above mentioned id = "@ + the above mentioned id / tv_name" 
            Android: layout_width = "match_parent" 
            Android: layout_height = "0dp" 
            Android: layout_weight = "1" 
            Android : Gravity = "left | Center" 
            Android: textColor = "# 000" 
            Android: textSize = "20SP" 
            /> 
        ! <- this is how planets described in the text view -> 
        < TextView
             Android: the above mentioned id = "@ + the above mentioned id / tv_desc " 
            Android: layout_width ="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:gravity="left|center"
            android:textColor="#000"
            android:textSize="13sp"/>
        
    </LinearLayout>
</LinearLayout>

(2) to write a new adapter inheritance BaseAdapter, and achieve access operations on list items view

public class PlanetListArrayAdapter extends BaseAdapter {

    Private the Context mContext; // declare a context object 
    Private the ArrayList <Planet> mPlanetList; // declare a planetary queue

    // constructor planetary adapter, the planetary context queue incoming 
    public PlanetListArrayAdapter (the Context context, the ArrayList <Planet> planet_list) {
        mContext=context;
        mPlanetList=planet_list;
    }

    // get the list of the number of items 
    @Override
     public  int getCount () {
         return mPlanetList.size ();
    }

    // get the list item data 
    @Override
     public Object the getItem ( int I) {
         return mPlanetList.get (I);
    }

    // Get a list of item numbers 
    @Override
     public  Long getItemId ( int I) {
         return I;
    }

    // Get the specified location list item view 
    @Override
     public View the getView ( int I, View View, the ViewGroup ViewGroup) {
        View Holder holds;
        IF (View == null ) { // convert view is empty 
            Holder = new new ViewHolder ();
             // a layout view object generates a conversion file item_list.xml 
            view = LayoutInflater.from (mContext) .inflate ( R.layout.item_list, null );
            holder.iv_icon= (ImageView) view.findViewById(R.id.iv_icon);
            holder.tv_name= (TextView) view.findViewById(R.id.tv_name);
            holder.tv_desc = (the TextView) view.findViewById (R.id.tv_desc);
             // save the view holder conversion view 
            view.setTag (holder);
        } The else { // convert non-empty view
             // Get saved before conversion view from the view holder 
            Holder = (ViewHolder) view.getTag ();
        }
        Planet planet=mPlanetList.get(i);
        holder.iv_icon.setImageResource (planet.image); // display the planets picture 
        holder.tv_name.setText (planet.name); // display the name of the planet 
        holder.tv_desc.setText (planet.desc); // display the planet description 
        return View;
    }

    public class ViewHolder{
        public ImageView iv_icon;
        public TextView tv_name;
        public TextView tv_desc;
    }
}

(3) construction of the adapter in the page code, and applied to an object Spinner

Private the ArrayList <Planet> planetList; // declare a planetary queue

    // drop-down list box initialization planet 
    Private  void initPlanetSpinner () {
         // get the default queue planet, namely fire wood, earth, water Golden 
        planetList = Planet.getDefaultList ();
         // Build a list of the planet adapter 
        PlanetListArrayAdapter Adapter = new new PlanetListArrayAdapter ( the this , planetList);
         // get the drop-down box called sp_planet from the layout file 
        Spinner SP = findViewById (R.id.sp_planet);
         // set the drop-down box titled 
        sp.setPrompt ( "Please select planet" );
         // drop-down list box provided adapter 
        sp.setAdapter (adapter);
         // set default display the first drop-down box 
        sp.setSelection (0);
         // to set the drop-down box to select a listener, once the user selects an item, triggered onItemSelected method 
        sp.setOnItemClickListener ( new new MySelectedListener);
    }

    private class MySelectedListener implements AdapterView.OnItemSelectedListener{

        // Select treatment time, where i represents the number of choices 
        @Override
         public  void onItemSelected (The AdapterView <?> AdapterView, View View, int i, Long L) {
            Toast.makeText (. MainActivity the this , "Your choice is:" + planetList.get (i) .name, Toast.LENGTH_LONG) .Show ();
        }

        // event handler method is not selected, usually not an issue 
        @Override
         public  void onNothingSelected (AdapterView <?> AdapterView) {

        }
    }

Guess you like

Origin www.cnblogs.com/zdm-code/p/12222278.html