Android : ListView 學生管理


Android 使用 ListView 控件實現簡易學生管理功能。

使用工具

  • android studio (ver. 3.5.1)
  • android(sdk 29)
  • java(ver.1.8.0)
  • gradle(ver. 5.4.1)

功能實現

1.實現多個學生信息的添加和顯示

創建一個 Basic Activity 項目實現學生增加顯示

pic0

2.用 EditText 實現姓名、年齡的輸入,並有輸入校驗(空校驗,數字校驗)

學生實體類中定義函數實現輸入合法校驗。

public String mandatory(){
        if (name.equals(null) || name == "" || name.length() < 2) {
            return "姓名";
        }
        if (major.equals("請選擇專業")) {
            return "專業";
        }
        if (!isNumeric(age))
            return "年齡";
        int ageInt = Integer.parseInt(age);
        if (ageInt < 10 || ageInt > 100) {
            return "年齡";
        }
        return "null";
    }

3.用 AutoCompleteTextView 的實現專業的輸入,並有提示功能。

 autoCompleteTextViewAddMajor = findViewById(R.id.add_major);
        String[] major_array = {"物聯網工程",
                "計算機科學與技術",
                "材料科學與工程",
                "環境科學與工程",
                "化學化工與生物工程"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_dropdown_item_1line,
                major_array);
        //初始化autoCompleteTextView
        autoCompleteTextViewAddMajor.setAdapter(adapter);
        //設置輸入多少字符后提示,默認值為2,在此設為1
        autoCompleteTextViewAddMajor.setThreshold(1);

4.通過 CheckBox 實現多門課程選擇

private LinearLayout checkbox;

private List<String> getCheckBoxInfo(LinearLayout checkbox) {
        List<String> courses = new ArrayList<>();
        int num = checkbox.getChildCount();
        for (int i = 0; i < num; i++) {
            CheckBox cb = (CheckBox) checkbox.getChildAt(i);
            if (cb.isChecked())
                courses.add(cb.getText().toString());
        }
        return courses;
    }

5.通過 Radio 實現性別

設置 RadioGroup 的監聽。

radioGroupSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup rg, int checkedId) {
                if (checkedId == radioButtonMan.getId()) {
                    sex = "男";
                } else if (checkedId == radioButtonWoman.getId()) {
                    sex = "女";
                } else {
                    sex = "男";
                }
            }
        });

6.用下拉框選擇學院

Layout 中 Spinner 引用 res/values 中的 college_array.xml 的中值。

android:entries="@array/college"

7.用日期控件實現入學時間的輸入

private Calendar calendar; // 通過Calendar獲取系統時間

點擊"日期"按鈕布局 設置日期。

			editTextAddAdmissionDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(AddActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int month, int day) {
                                mYear = year;
                                mMonth = month;
                                mDay = day;
                                // 更新EditText控件日期 小於10加0
                                editTextAddAdmissionDate.setText(new StringBuilder()
                                        .append(mYear)
                                        .append("-")
                                        .append((mMonth + 1) < 10 ? "0"
                                                + (mMonth + 1) : (mMonth + 1))
                                        .append("-")
                                        .append((mDay < 10) ? "0" + mDay : mDay));
                            }
                        }, calendar.get(Calendar.YEAR), calendar
                        .get(Calendar.MONTH), calendar
                        .get(Calendar.DAY_OF_MONTH)).show();
            }
        });

8.實現列表中學生信息的編輯和刪除。

在學生 Adapater 中重寫方法。

		@Override
    public View getView(int i, View view, ViewGroup viewGroup) {
//創建一個 view 對象(item),對象的樣式采用自定義的 layout 樣式
        View stuView = View.inflate(stuContext, R.layout.list_view, null);
///獲得 item 的每個子元素
        TextView tv_name = stuView.findViewById(R.id.name);
        TextView tv_major = stuView.findViewById(R.id.major);
        ImageView iv_edit = stuView.findViewById(R.id.edit);
        ImageView iv_delete = stuView.findViewById(R.id.delete);
//將數據源的一項數據和 item 的子元素綁定
        Student student = (Student) stuDates.get(i);
        tv_name.setText(student.getName());
        tv_major.setText(student.getMajor());
        iv_edit.setImageResource(R.drawable.edit);
        iv_delete.setImageResource(R.drawable.delete);
//給 iv_edit 增加一個監聽
        iv_delete.setOnClickListener(this);
        iv_edit.setOnClickListener(this);
//給刪除和編輯的 imageView 設置一個標志,用 item 的位置 i 來定位,類似一個 id
        iv_delete.setTag(i);
        iv_edit.setTag(i);
        return stuView;
    }

演示

pic1pic2

代碼下載地址

ListViewApp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM