Android Studio基础ListView之BaseAdapter

Android Studio基础ListView之BaseAdapter

完成的样式的如下:

第一步:布局XML设置

<?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:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/holo_red_dark"
            android:text="姓名:"/>
        <TextView
            android:id="@+id/tv_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/holo_red_dark"
            android:text="手机:"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拨号"/>
</LinearLayout>

第二步:设置数据源使用map方法

第三步:使用适配器baseAdapter,新建新的java文件PhoneAdapter.java

继承BaseAdapter的所有的方法,并重新所有的方法

第一个是数据源:在重新的方法中告诉PhoneAdapter.java的数据源 List<Map<String,String>> names

在PhoneAdapter.java添加数据源

第二个是上下文:自己定义的

第三个设置构造方法,需要把数据源、上下文传入,并赋值

第四个:把数据源的数据展示在单元格布局上

第五个:在单元格上添加数据,因此必须获取该单元格布局中的控件,即从单元格布局中绑定所有控件

第六个:获取当前需要显示的记录数据,并保存下来

第七个:将数据显示在指定的控件上,按钮无法显示的数据。

PhoneAdapter.java的所有源码信息:

package com.xwb.zdydhk;

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

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

public class PhoneAdapter extends BaseAdapter {
    private List<Map<String,String>> names;//数据源
    private Context context;//上下文

    public PhoneAdapter(Context context,List<Map<String,String>> names){//把数据源、上下文传入,并赋值
        this.context = context;
        this.names = names;
    }

    @Override
    //getCount()告述其,数据源的数量大小(总数)
    public int getCount() {
        return names.size();
    }

    @Override
    //暂时不用处理
    public Object getItem(int position) {
        return null;
    }

    @Override
    //暂时不用处理
    public long getItemId(int position) {
        return 0;
    }

    @Override
    //getView()是非常关键的方法,是通过getCount()数量大小来执行多少次数。作用把数据源的数据展示在单元格布局上
    //position当前的数据的索引;convertView指定的单元格的布局;parent暂时忽略
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){//单元格布局为空时直接指定item_list_view布局
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_view,null);
        }

        //在单元格上添加数据,因此必须获取该单元格布局中的控件,即从单元格布局中绑定所有控件
        TextView tv_name = convertView.findViewById(R.id.tv_name);
        TextView tv_phone = convertView.findViewById(R.id.tv_phone);
        Button btn_call = convertView.findViewById(R.id.btn_call);

        //获取当前需要显示的记录数据,并保存下来
        Map<String,String> item = names.get(position);

        //将数据显示在指定的控件上
        tv_name.setText(item.get("names"));
        tv_phone.setText(item.get("phone"));

        return convertView;
    }
}

第四步:调用自定义的Adapter,并将适配器指定给ListView的对象。

package com.xwb.zdydhk;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn_single = findViewById(R.id.btn_single);
        btn_single.setOnClickListener(this);
        Button btn_alert_customer = findViewById(R.id.btn_alert_customer);
        btn_alert_customer.setOnClickListener(this);
        //获取ListView控件
        ListView lv_demo = findViewById(R.id.lv_demo);

        //定义数据源为数组
        //String[] names = {"强哥","信春哥","大禹"};
        //使用数据集合List
        List<Map<String,String>> names = new ArrayList<>();
        Map<String,String> map = new HashMap<String,String>();
        map.put("name","强哥");
        map.put("phone","135123456789");
        names.add(map);
        map = new HashMap<String,String>();
        map.put("name","信春哥");
        map.put("phone","15812131315");
        names.add(map);
        map = new HashMap<String,String>();
        map.put("name","大禹");
        map.put("phone","18878978766");
        names.add(map);

        //单元格布局可以自定义,也可以使用安卓自带的
        //安卓自带的,使用SimpleAdapter。
        // new SimpleAdapter第一个参数为类名.this。第二个参数为数据源必须是数据集合List。第三个参数为单元格布局,第四个参数from显示Key。
        // 第五个参数把该KEY放在单元格哪个控件上,这里是放在安卓自带布局的ID上。
//        SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,names, android.R.layout.simple_list_item_1,new String[]{"name"},new int[]{android.R.id.text1});
//
//        //将适配器指定给ListView的对象
//        lv_demo.setAdapter(simpleAdapter);

        //----------------------------
        //创建数据源对象(数据适配器)ArrayAdapter<String>(参数1(上下文),参数2(安卓默认自带布局),参数3(安卓默认自带布局的控件ID),参数4(数据源为数组))
        //android.R.xx官方提供资源
        //ArrayAdapter不支持多属性数据,SimpleAdapter支持多种复杂属性的数据
        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,names);

        //将适配器指定给ListView的对象
        //lv_demo.setAdapter(adapter);
        //------------------------------

        //创建自定义的Adapter对象,数据源对象(数据适配器)BaseAdapter(参数1(上下文),参数2(数据源数据))
        PhoneAdapter adapter = new PhoneAdapter(MainActivity.this,names);
        //将适配器指定给ListView的对象
        lv_demo.setAdapter(adapter);
    }
    @Override
    public void onClick(View v){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        switch (v.getId()){
            case R.id.btn_single:
                builder.setTitle("单选对话框").setIcon(R.mipmap.ic_launcher).setSingleChoiceItems(new String[]{"中国", "德国", "日本"}, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"选中的"+which,Toast.LENGTH_SHORT).show();
                    }
                });
                break;
            case R.id.btn_alert_customer:
                //setView(R.layout.item_alert_dialog)为自定义的对话框
                builder.setTitle("自定义对话框").setIcon(R.mipmap.ic_launcher).setView(R.layout.item_alert_dialog);
                break;
            }
        AlertDialog ad = builder.create();
        ad.show();
    }
}

第五步:APP运行效果

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐为波

看着给就好了,学习写作有点累!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值