Android 实现类似考试座号表效果

 

类似于这种效果

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控件

<ListView
        android:id="@+id/act_base_adapter_student_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </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这种条目布局

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

    <ImageView
        android:id="@+id/act_item_student_iv"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/user_icon" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp">

        <TextView
            android:id="@+id/item_student_name_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/stu_name" />

        <TextView
            android:id="@+id/item_student_age_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="@string/stu_age" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="@string/stu_sex" />

        <TextView
            android:id="@+id/item_student_id_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:text="@string/stu_id" />

    </RelativeLayout>


</LinearLayout>

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

private Context mContext;
    private List<Student> mList
    public StudentInfoAdapter(Context Context,List<Student> 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<Student> 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();
            }
        });

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值