Android 基本适配器BaseAdapter

2023-01-13 11:05:47

ArrayAdapter适用于纯文本的列表数据,SimpleAdapter适用于带图标的列表数据,BaseAdapter适用于更复杂的列表例如同一项中存在多个控件,BaseAdapter允许在别的代码文件中进行逻辑处理。

BaseAdapter与Spinner结合

 具体实现代码有

Planet类:提供行星的图标、名称、简介

PlanetListAdapter类:继承BaseAdapter类,并要实现4个基本方法。

getCount:获取数据项的个数

getItem

getItemId

getView:获取每项的展示视图,并对每项的内部控件进行业务处理。

BaseAdapterActivity类:为Spinner控件设置适配器

activity_base_adapter.xml:Spinner相关的布局文件

item_list.xml:列表数据的布局文件

 Planet类

public Planet(int image, String name, String desc) {
        this.image = image;
        this.name = name;
        this.desc = desc;
    }

    private static int[] iconArray = {R.drawable.shuixing, R.drawable.jinxing, R.drawable.diqiu,
            R.drawable.huoxing, R.drawable.muxing, R.drawable.tuxing};
    private static String[] nameArray = {"水星", "金星", "地球", "火星", "木星", "土星"};
    private static String[] descArray = {
            "水星是太阳系八大行星最内侧也是最小的一颗行星,也是离太阳最近的行星",
            "金星是太阳系八大行星之一,排行第二,距离太阳0.725天文单位",
            "地球是太阳系八大行星之一,排行第三,也是太阳系中直径、质量和密度最大的类地行星,距离太阳1.5亿公里",
            "火星是太阳系八大行星之一,排行第四,属于类地行星,直径约为地球的53%",
            "木星是太阳系八大行星中体积最大、自转最快的行星,排行第五。它的质量为太阳的千分之一,但为太阳系中其它七大行星质量总和的2.5倍",
            "土星为太阳系八大行星之一,排行第六,体积仅次于木星"
    };

    public static ArrayList<Planet> getDefaultList() {
        ArrayList<Planet> planetList = new ArrayList<Planet>();
        for (int i = 0; i < iconArray.length; i++) {
            planetList.add(new Planet(iconArray[i], nameArray[i], descArray[i]));
        }
        return planetList;
    }

PlanetListAdapter类

public class PlanetListAdapter extends BaseAdapter{
    private Context mContext;
    private ArrayList<Planet> mPlanetList;
    public PlanetListAdapter(Context context, ArrayList<Planet> planet_list) {
        mContext = context;
        mPlanetList = planet_list;
    }

    @Override
    public int getCount() {
        return mPlanetList.size();
    }

    @Override
    public Object getItem(int i) {
        return mPlanetList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    //获取指定位置的列表项视图
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);
            holder.iv_icon = convertView.findViewById(R.id.iv_icon);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_desc = convertView.findViewById(R.id.tv_desc);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        Planet planet = mPlanetList.get(position);
        holder.iv_icon.setImageResource(planet.image);
        holder.tv_name.setText(planet.name);
        holder.tv_desc.setText(planet.desc);
        return convertView;
    }


    // 定义一个视图持有者,以便重用列表项的视图资源
    public final class ViewHolder {
        public ImageView iv_icon; // 声明行星图片的图像视图对象
        public TextView tv_name; // 声明行星名称的文本视图对象
        public TextView tv_desc; // 声明行星描述的文本视图对象
    }
}

BaseAdapterActivity类

public class BaseAdapterActivity extends AppCompatActivity {
    private ArrayList<Planet> planetlist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_adapter);
        initPlanetSpinner();
    }

    private void initPlanetSpinner(){
        planetlist = Planet.getDefaultList();
        PlanetListAdapter planetListAdapter = new PlanetListAdapter(this, planetlist);
        Spinner sp =findViewById(R.id.sp_planet);
        sp.setPrompt("请选择行星");
        sp.setAdapter(planetListAdapter);
        sp.setOnItemSelectedListener(new MySelectedListener());
    }
    private class MySelectedListener implements AdapterView.OnItemSelectedListener{
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(BaseAdapterActivity.this, "您选择的是" + planetlist.get(i).name,
                    Toast.LENGTH_LONG).show();
        }
}

activity_base_adapter.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".BaseAdapterActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="行星的列表视图"
        android:textSize="20sp"/>

    <Spinner
        android:id="@+id/sp_planet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:spinnerMode="dialog"/>

</LinearLayout>

item_list.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_weight="1"
        android:scaleType="fitCenter" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <!-- 这是显示行星名称的文本视图 -->
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="left|center"
            android:textSize="20sp" />

        <!-- 这是显示行星描述的文本视图 -->
        <TextView
            android:id="@+id/tv_desc"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:gravity="left|center"
            android:textSize="13sp" />
    </LinearLayout>
</LinearLayout>
  • 作者:知非丶
  • 原文链接:https://blog.csdn.net/zxs490862612/article/details/126027198
    更新时间:2023-01-13 11:05:47