独立完成-假数据学生管理系统

懂得l:

布局方式

泛型类。

实体类的使用 getter and setter 作用

使用MyAdapter extend BaseAdapter的各种细节使用。


mainac

import com.example.a49854.studentxuexi.domain.StudentInfo;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

    private EditText et_id;
    private EditText et_name;
    private EditText et_phone;
    private ListView lv;
    private ArrayList<StudentInfo> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        // 1.找到控件
        // 2.模拟操作,添加假数据进行显示
        // 3.去除假数据,添加真实数据
        // 4.将真实数据写入到数据库中
        // 5.将数据库上传到服务器



    }

    /*
        初始化界面
     */
    private void initView() {
        setContentView(R.layout.activity_main);

        et_id = (EditText) findViewById(R.id.et_id);
        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        lv = (ListView) findViewById(R.id.lv);

        list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {

            StudentInfo info = new StudentInfo();
            info.setId(i);
            info.setName("student" + i);
            info.setPhone("1234567891" + i);
            list.add(info);
        }


        lv.setAdapter(new MyAdapter());

    }

    private class MyAdapter extends BaseAdapter {


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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this, R.layout.item, null);
            TextView tv_item_id = (TextView) view.findViewById(R.id.tv_item_id);
            TextView tv_item_name = (TextView) view.findViewById(R.id.tv_item_name);
            TextView tv_item_phone = (TextView) view.findViewById(R.id.tv_item_phone);
            ImageView iv_delete = (ImageView) view.findViewById(R.id.iv_delete);

            iv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
//                    boolean result = dao.delete(map.get("studentid"));
//                    if (result) {
//                        Toast.makeText(MainActivity.this,"删除成功", Toast.LENGTH_SHORT).show();
//                        lv.setAdapter(new MyAdapter());
//                    }
                    lv.setAdapter(new MyAdapter());//这种方法其实不好
                }
            });


            tv_item_id.setText(String.valueOf(list.get(position).getId()));
            tv_item_name.setText(list.get(position).getName());
            tv_item_phone.setText(list.get(position).getPhone());

            return view;
        }

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

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


    }
}

StudentInfo

package com.example.a49854.studentxuexi.domain;

/**
 * 学生信息的实体类
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class StudentInfo {
    private Integer id;//
    private String name;
    private String phone;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "StudentInfo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}



activity_main.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="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="学生信息管理系统"
        android:textColor="#ff0000"
        android:textSize="20dp"/>

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

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="学生id"
            android:textColor="#0000ff"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="姓名"
            android:textColor="#0000ff"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="联系电话"
            android:textColor="#0000ff"/>
    </LinearLayout>

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

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

        <EditText
            android:id="@+id/et_id"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="输入id"
            android:inputType="textPersonName"/>

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="输入name"
            android:inputType="textPersonName"
            />

        <EditText
            android:id="@+id/et_phone"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="输入电话"
            android:inputType="textPersonName"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加" />

</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_item_id"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="TextView"
        android:textColor="#ff0000"
        tools:text="学生id"/>

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="学生name"
        android:textColor="#00ff00"/>

    <TextView
        android:id="@+id/tv_item_phone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="学生phone"
        android:textColor="#0000ff"
        />

    <ImageView
        android:id="@+id/iv_delete"
        android:layout_width="17dp"
        android:layout_height="17dp"
        app:srcCompat="@drawable/block_msg_delete"/>
</LinearLayout>





















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值