BaseAdapter简单使用

在这里插入图片描述

activity_adapter_class_simple_and_base.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/color_simple_base_adapter"
        android:dividerHeight="3dp">
    </ListView>
</LinearLayout>

listview_simple_and_base_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFF"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/item_img"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher_background" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/class_id"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingStart="20dp"
                android:text="789"
                android:textSize="17sp" />

            <TextView
                android:id="@+id/code"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:paddingEnd="20dp"
                android:text="789"
                android:textSize="17sp" />
        </LinearLayout>

        <TextView
            android:id="@+id/classname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="20dp"
            android:text="Android"
            android:textSize="20sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/teacher"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingStart="20dp"
                android:text="789" />

            <TextView
                android:id="@+id/classType"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:paddingEnd="20dp"
                android:text="789" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

AdapterClassBaseActivity.java

package com.jld.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdapterClassBaseActivity extends Activity {
    private ListView listView;
    private List<Map<String, Object>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adapter_class_simple_and_base);
        listView = findViewById(R.id.lv);
        initDataList();
        String[] from = {"img", "class_id", "code", "classname", "teacher", "classType"};
        int[] to = {R.id.item_img, R.id.class_id, R.id.code, R.id.classname, R.id.teacher, R.id.classType};
        final ClassAdapter classAdapter = new ClassAdapter(this, list, R.layout.listview_simple_and_base_item, from, to);
        listView.setAdapter(classAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = view.findViewById(R.id.classname);
                Toast.makeText(getApplicationContext(), textView.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initDataList() {
        list = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("img", R.mipmap.class_1);
        map.put("class_id", "16201");
        map.put("code", "505313");
        map.put("classname", "移动应用开发技术");
        map.put("teacher", "魏老师");
        map.put("classType", "学校课表班课");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.class_1);
        map.put("class_id", "17104");
        map.put("code", "605454");
        map.put("classname", "移动终端程序设计");
        map.put("teacher", "魏老师");
        map.put("classType", "学校课表班课");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.class_1);
        map.put("class_id", "17101");
        map.put("code", "939787");
        map.put("classname", "Java程序设计");
        map.put("teacher", "魏老师");
        map.put("classType", "学校课表班课");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.class_1);
        map.put("class_id", "16101");
        map.put("code", "952532");
        map.put("classname", "移动应用开发技术");
        map.put("teacher", "魏老师");
        map.put("classType", "学校课表班课");
        list.add(map);

        map = new HashMap<String, Object>();
        map.put("img", R.mipmap.class_1);
        map.put("class_id", "16104");
        map.put("code", "449884");
        map.put("classname", "移动终端程序设计");
        map.put("teacher", "魏老师");
        map.put("classType", "学校课表班课");
        list.add(map);
    }
}

ClassAdapter.java

package com.jld.myapplication;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

public class ClassAdapter extends BaseAdapter {
    Context context;
    List<? extends Map<String, ?>> data;
    int resource;
    String[] from;
    int[] to;

    public ClassAdapter(Context context, List<Map<String, Object>> data, int resource, String[] from, int[] to) {
        this.context = context;
        this.data = data;
        this.resource = resource;
        this.from = from;
        this.to = to;
    }

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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewHolder holder = null;
        if (view == null) {
            view = inflater.inflate(R.layout.listview_simple_and_base_item, null);

            holder = new ViewHolder();
            holder.item_img = view.findViewById(R.id.item_img);
            holder.class_id = view.findViewById(R.id.class_id);
            holder.code = view.findViewById(R.id.code);
            holder.classname = view.findViewById(R.id.classname);
            holder.teacher = view.findViewById(R.id.teacher);
            holder.classType = view.findViewById(R.id.classType);

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Map map = data.get(i);
        holder.item_img.setImageResource((int) map.get(from[0]));
        holder.class_id.setText((String) map.get(from[1]));
        holder.code.setText((String) map.get(from[2]));
        holder.classname.setText((String) map.get(from[3]));
        holder.teacher.setText((String) map.get(from[4]));
        holder.classType.setText((String) map.get(from[5]));

        //如何从id映射到组件

        return view;
    }

    static class ViewHolder {
        ImageView item_img;
        TextView class_id;
        TextView code;
        TextView classname;
        TextView teacher;
        TextView classType;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值