BaseAdapter class
(1) Overview
AdapterView fills data to the subclass view of AdapterView through Adapter (adapter class), among which, BaseAdapter is the base class of all adapter classes. This class is an abstract class
(2) Common methods
1、public View getView(int position,View convertView,ViewGroup parent) Role: Create and return list items, including the display style and data of list items, Android draws list items based on the object returned by getView. illustrate: The first parameter -position: the index value of the current list item in the list The second parameter -convertView: the View object of the current list item The third parameter -parent: the parent container object of the list item - a list object Hint: This method is an abstract method. Example: The following code creates a label object and returns the object as a list item @Override public View getView(int position,View convertView,ViewGroup parent){ //Instantiate the label object TextView tvCityName = (TextView) findViewById (R.layout.listview_item); / / Set the title of the label to the index value of the array in the position (that is, the current list item's index value) element tvCityName.setText(mCityName[position]); return tvCityName; //return the label object } 2、public long getItemId(int position) Role: Returns the index value of the current list item Parameter -position: the index value of the current list item Hint: This method is an abstract method Sample code: @Override public long getItemId(int position){ return postion; } 3、public Object getItem(int position) Role: Returns the current list item object Parameter -position: the index value of the current list item Hint: This method is an abstract method Sample code: @Override public String getItem(int position){ return mCityName[position]; } illustrate: 1. mCityName is an array of strings. The above code returns one of the array element. 2. The default getItem returns the Object type, which should be modified in actual programming is the specific type to be returned. For example: In this example the returned type is String. If it is not modified, when calling this method, you need to do Type conversion. 4、public int getCount() Function: Returns the total number of objects in the list, the Android system returns according to getCount() The returned value draws the specified number of list items. Hint: This method is an abstract method.
Specific examples:
package com.jxust.day_05_01_listviewdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { ListView mlvGeneral; List<GeneralBean> mGenerals; // Represents a collection of ten military strategists GeneralAdapter mAdapter; int[] resid = { R.drawable.baiqi, R.drawable.caocao, R.drawable.chengjisihan, R.drawable.hanxin, R.drawable.lishimin, R.drawable.nuerhachi, R.drawable.sunbin, R.drawable.sunwu, R.drawable.yuefei, R.drawable.zhuyuanzhang }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); initData(); // initialize data initView(); } private void initView() { mlvGeneral = (ListView) findViewById(R.id.lvGeneral); mAdapter = new GeneralAdapter(); //Create an adapter // Associate with ListView mlvGeneral.setAdapter(mAdapter); } private void initData() { // Convert the string array in the resource to a Java array String[] names = getResources().getStringArray(R.array.generals); mGenerals = new ArrayList<GeneralBean>(); for (int i = 0; i < names.length; i++) { GeneralBean bean = new GeneralBean(resid[i], names[i]); mGenerals.add(bean); } } // Define the adapter class that represents the military strategist class GeneralAdapter extends BaseAdapter { @Override public int getCount() { return mGenerals.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // Get the layout of the ListView and convert it to an object of type View View layout = View.inflate(MainActivity.this, R.layout.item_generals, null); // root represents the parent container // Find the ImageView that displays the fighter's avatar // The role of layout is to convert the target to layout.item_generals ImageView ivThumb = (ImageView) layout.findViewById(R.id.ivThumb); // Find the TextView that displays the military strategist's name TextView tvName = (TextView) layout.findViewById(R.id.tvName); // Get the strategist object whose subscript is position in the strategist GeneralBean bean = mGenerals.get(position); // Display the fighter's avatar ivThumb.setImageResource(bean.getResid()); // Display the military strategist's name tvName.setText(bean.getName()); return layout; } } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/lvGeneral" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#ccc" android:dividerHeight="2dp" android:entries="@array/generals" /> </RelativeLayout>
<?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="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/ivThumb" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/baiqi" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="80dp" android:layout_marginLeft="10dp" android:text="白起" android:textSize="20sp" android:gravity="center_vertical" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Day_05_01_ListViewDemo</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string-array name="generals"> <item >白起</item> <item >曹操</item> <item >Genghis Khan</item> <item>Han Xin</item> <item >Li Shimin</item> <item >Nurhaci</item> <item >孙膑</item> <item >Sun Wu</item> <item >Yue Fei</item> <item >Zhu Yuanzhang</item> </string-array> </resources>