博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 实现类似考试座号表效果
阅读量:6267 次
发布时间:2019-06-22

本文共 4455 字,大约阅读时间需要 14 分钟。

hot3.png

类似于这种效果

da8f568b7062b412d84fee44d1267e13883.jpg

1,新建一个Student类,用户添加学生信息

private int icon;    private String name;    private int age;    private String sex ;    private int id;    public Student(int icon, String name, int age, String sex, int id) {        this.icon = icon;        this.name = name;        this.age = age;        this.sex = sex;        this.id = id;    }  //下面添加getter and setter方法

2,主布局文件中添加ListView控件

3.新建一个StudentInfoAdapter类,继承BaseAdapter并且实现其中的抽象方法

public class StudentInfoAdapter extends BaseAdapter {    @Override    public int getCount() {        return 0;    }    @Override    public Object getItem(int position) {        return null;    }    @Override    public long getItemId(int position) {        return 0;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        return null;    }}

4,创建一个item_student.xml布局文件,用于设置列表中单个条目的布局

de1c8387db8430f9524f2af179f11eaf94f.jpg这种条目布局

5,定义有参构造函数,在StudentInfoAdapter类中重写的getCount()方法中return  传入的list 的长度

private Context mContext;    private List
mList public StudentInfoAdapter(Context Context,List
list) { this.mContext = Context; this.mList = list; } @Override public int getCount() { return mList.size(); }

6,StudentInfoAdapter类中重写的getView()方法中设置控件和属性,具体看下面代码注释

@Override    public View getView(int position, View convertView, ViewGroup parent) {        if (convertView == null) {            //将自定义的布局文件设置给convertView            convertView = View.inflate(mContext, R.layout.item_student, null);        }        //获取convertView中要使用的控件        ImageView iconIv = convertView.findViewById(R.id.act_item_student_iv);        TextView idTv = convertView.findViewById(R.id.item_student_id_tv);        TextView nameTv = convertView.findViewById(R.id.item_student_name_tv);        TextView ageTv = convertView.findViewById(R.id.item_student_age_tv);        TextView sexTv = convertView.findViewById(R.id.item_student_sex_tv);        //根据每次调用getView方法时的position值获取当前需要填充的Student对象        Student student = mList.get(position);        //将此Student对象的各个属性设置给item中的相关控件        iconIv.setImageResource(student.getIcon());        nameTv.setText("姓名:"+student.getName());        idTv.setText("座号:"+ student.getId());        ageTv.setText("年龄:"+student.getAge());        sexTv.setText("性别:"+student.getSex());        return convertView;    }

7,在主类中初始化

private ListView studentLv;    private ArrayList
stuAl; private StudentInfoAdapter adapter; //学生ListView studentLv = findViewById(R.id.act_base_adapter_student_lv); stuAl = new ArrayList<>(); //初始化自定义的StudentInfoAdapter if (adapter == null) { adapter = new StudentInfoAdapter(this, stuAl); }else{ adapter.notifyDataSetChanged(); }

8,添加学生信息

stuAl.add(new Student(R.drawable.stu1,"露娜",11,"女",1));        stuAl.add(new Student(R.drawable.stu2,"王昭君",13,"女",2));        stuAl.add(new Student(R.drawable.stu3,"貂蝉",17,"女",3));        stuAl.add(new Student(R.drawable.stu4,"兰陵王",18,"男",4));        stuAl.add(new Student(R.drawable.stu7,"李白",16,"男",5));        stuAl.add(new Student(R.drawable.stu6,"鲁班",19,"男",6));        stuAl.add(new Student(R.drawable.stu8,"狄仁杰",15,"男",7));        stuAl.add(new Student(R.drawable.stu9,"武则天",10,"女",8));        stuAl.add(new Student(R.drawable.stu10,"妲己",14,"女",9));

9,给设置适配器

studentLv.setAdapter(adapter);

10,添加功能:点击学生条目,谈提示框提示学生信息,如下效果

24a6324e252b746bac49d3a86428e142377.jpg

添加条目点击监听器,然后添加对话框

studentLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView
parent, View view, int position, long id) { Student student = stuAl.get(position); String nameStr = student.getName(); int age = student.getAge(); int idInt = student.getId(); String sexStr = student.getSex(); AlertDialog.Builder builder = new AlertDialog.Builder(BaseAdapterActivity.this); builder.setTitle("学生信息"); builder.setMessage("姓名:"+nameStr+ "\r\n年龄:"+age+ "\r\n性别:"+sexStr+ "\r\n座 号:"+idInt); builder.setPositiveButton("知道了", null); builder.create().show(); } });

 

转载于:https://my.oschina.net/lanyu96/blog/2248541

你可能感兴趣的文章
lakala反欺诈建模实际应用代码GBDT监督学习
查看>>
java 解析excel工具类
查看>>
Google FireBase - fcm 推送 (Cloud Messaging)
查看>>
BBS论坛(二十七)
查看>>
html DOM 的继承关系
查看>>
装饰器的邪门歪道
查看>>
Dubbo常用配置解析
查看>>
【转】C#解析Json Newtonsoft.Json
查看>>
macports的安装及常用命令
查看>>
(转)使用C#开发ActiveX控件
查看>>
spring mvc 基于注解 配置默认 handlermapping
查看>>
半小时学会上传本地项目到github
查看>>
Android学Jni/Ndk 开发记录(一)
查看>>
Linux Tcl和Expect的安装
查看>>
WPF中的依赖项属性(转)
查看>>
linux防火墙相关 iptables
查看>>
最简单的单例模式
查看>>
JPopupMenu的使用以及JPopupMenu中子组件的事件处理
查看>>
从反汇编的角度看引用和指针的区别
查看>>
拓马长枪定乾坤
查看>>