首页 > 其他分享 >基本适配器 BaseAdapter

基本适配器 BaseAdapter

时间:2023-03-03 15:47:27浏览次数:31  
标签:基本 name BaseAdapter int 适配器 convertView holder public desc

新建一个activity,命名为 BaseAdapterActivity,这里以Spinner为载体,演示 BaseAdapter 的用法

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 adapter = new PlanetListAdapter(this, planetList);
        Spinner sp = findViewById(R.id.sp_planet);
        sp.setPrompt("请选择行星");
        sp.setAdapter(adapter);
        sp.setOnItemSelectedListener(new MySelectedListener());
    }

    private class MySelectedListener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ToastUtil.showToast(BaseAdapterActivity.this, "您选择的是" + planetList.get(position).name);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    }
}

BaseAdapterActivity 对应的xml文件为:activity_base_adapter 如下:

<?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="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="行星的列表视图"
        android:textColor="@color/black"
        android:textSize="20sp" />

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

</LinearLayout>

编写新的适配器 PlanetListAdapter 继承于 BaseAdapter,实现对列表项的获取与操作,如下:

public class PlanetListAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Planet> mPlanetList;

    public PlanetListAdapter(Context context, ArrayList<Planet> arrayList) {
        mContext = context;
        mPlanetList = arrayList;
    }

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

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

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

    @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 info = mPlanetList.get(position);
        holder.iv_icon.setImageResource(info.image);
        holder.tv_name.setText(info.name);
        holder.tv_desc.setText(info.desc);
        return convertView;
    }

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

PlanetListAdapter 中对应的布局文件为 item_list ,如下:

<?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:background="@color/white"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:layout_height="100dp">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_marginLeft="5dp"
        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_marginLeft="5dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/tv_desc"
            android:layout_width="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:textColor="@color/gray"
            android:textSize="13sp" />

    </LinearLayout>

</LinearLayout>

PlanetListAdapter 中对应的模型为Planet 如下:

public class Planet {
    public int image;
    public String name;
    public String desc;

    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 < nameArray.length; i++) {
            planetList.add(new Planet(iconArray[i], nameArray[i], descArray[i]));
        }
        return planetList;
    }
}

效果图如下

  • 左边的图是BaseAdapterActivity的显示效果
  • 右边的图是PlanetListAdapter的显示效果

标签:基本,name,BaseAdapter,int,适配器,convertView,holder,public,desc
From: https://www.cnblogs.com/styCy/p/17175804.html

相关文章

  • C/C++ 数据结构使用数组实现队列的基本操作
    //使用数组实现队列#include<iostream>#include<Windows.h>usingnamespacestd;#defineMAXSIZE5//队列的最大容量typedefintDataType;//队列中的元素类型......
  • symbol的基本使用
    Symbol是什么Symbol是ES6中新增的一个基本数据类型,翻译为符号。在ES6之前,对象的属性名都是字符串形式,那么很容易造成属性名冲突;比如原来有一个对象,我们希望在其中添加......
  • 数据库和表的基本操作
    1.进入mysql:“mysql-uroot-p123456”2.创建数据库:“createdatabase数据库名称;”3.查看数据库名称:“showdatabase;”4.查看已经创建的数据库信息:“showcreatedat......
  • 软件体系架构方面英文文章翻译----软件架构的基本原理
    FundamentalsofSoftwareArchitecture02Dec,2022Intheworldoftechnology,startingfromsmallchildrentoyoungpeopleandstartingfromyoungtooldpeop......
  • 【编程基础之Python】7、Python基本数据类型
    (【编程基础之Python】7、Python基本数据类型)Python基本数据类型Python是一种动态类型语言,它支持多种基本数据类型和复合数据类型,让开发人员能够更加方便地处理不同类型......
  • Python 入门的基本语法规则
    官方对Python的介绍如下:Python是一款易于学习且功能强大的编程语言。它具有高效率的数据结构,能够简单又有效地实现面向对象编程。Python有着非广泛的应用,几乎所有大......
  • 【排序】基本排序的介绍
    冒泡和插入排序和快排这三种排序感觉比较常见面试也容易问到1、冒泡排序简单的来说就是将大的数和前面的数比较,交换,一直冒泡到最上方,然后下一次冒泡忽略这个上次的......
  • 软件开发基本要求
    模块化将程序中的所有功能划分为一个一个相互独立不可分割的功能部件。其中模块内的功能特定又集中,具有着内聚的特性。而模块间则基本独立无关,但也可能存在着少量的依赖......
  • kotlin基本数据类型
    通过idea创建kotlin项目:创建kotlin文件packagecom.czhappy.chapter01varaBoolean:Boolean=truevaranInt:Int=9varanotherInt:Int=0xFFvarmaxInt:Int=Int.MAX......
  • JavaScript的Dom基本操作
    获取元素的方式:根据id名称获取   document.getElementById("id名称")根据元素类名获取    document.getElementsClassName("元素类名")根据元素标......