Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 學生管理系統實作:Adapter、Dialog 與反射的組合應用

編輯:關於 Android 程式設計

這是一個綜合性的小項目:學生與教師的註冊登錄、選課、成績修改。功能本身不複雜,但正好能把 Adapter、Dialog、工廠模式與 Java 反射這幾個知識點串起來練一遍。

功能與界面

系統主要實現三類角色的基礎操作,其中大量使用了 Dialog 做表單輸入與確認提示。

一、自定義 Adapter

Adapter 的作用是為 ListView 的每一項提供數據與視圖。這裡繼承 BaseAdapter,在 getView 中加載 item 布局並填充數據;同時重寫 notifyDataSetChanged,用於在數據變化時通知列表刷新。

public class CourseAdapter extends BaseAdapter {
    private Context context;
    private List<Course> coursetList;

    public CourseAdapter(Context context, List<Course> coursetList) {
        this.context = context;
        this.coursetList = coursetList;
    }

    public int getCount() {
        return coursetList.size();
    }

    public Object getItem(int position) {
        return coursetList.get(position);
    }

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

    /** 通知 adapter 刷新數據 */
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // 加載每一個 item 條目的布局文件
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.student_score_item, null);
        }

        TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
        TextView tv_course = (TextView) convertView.findViewById(R.id.tv_course);
        TextView tv_score = (TextView) convertView.findViewById(R.id.tv_score);

        // 取得當前位置對應的數據對象
        Course course = coursetList.get(position);
        tv_name.setText(course.getStudentName() + "");
        tv_course.setText(course.getCourseName() + "");
        tv_score.setText(course.getCourseSocre() + "");

        return convertView;
    }
}

這裡有兩個可以改進的地方:一是 getView 中沒有使用 ViewHolder 模式,每次滾動都要重新 findViewById,列表長了會明顯卡頓;二是 notifyDataSetChanged 只是原樣調用父類實現,其實並不需要重寫,刪掉即可。

二、工廠模式結合反射

系統裡學生和教師有著相似的操作接口,很適合用工廠來統一創建。PersonFactory 提供了三種實現方式,難度遞增:

public class PersonFactory {

    /**
     * 方式一:直接根據類名反射創建
     */
    public PersonInter getPersonByClass(String className) {
        try {
            return (PersonInter) Class.forName(className).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 方式二:通過 if-else 判斷類型
     */
    public PersonInter getHair(String key) {
        if ("student".equals(key)) {
            return new StudentImpl();
        } else if ("teacher".equals(key)) {
            return new TeacherImpl();
        }
        return null;
    }

    /**
     * 方式三:讀取配置文件建立映射關係,再反射創建
     */
    public PersonInter getPersonByClassKey(String key) {
        try {
            Map<String, String> map = new PropertiesReader().getProperties();
            return (PersonInter) Class.forName(map.get(key)).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

三種方式的取捨很值得琢磨。方式二硬編碼了類型判斷,新增一種角色就要改工廠代碼,違背了開閉原則;方式一雖然擺脫了 if-else,但類名以字符串形式散落在各處調用點,重命名時容易漏改;方式三把「鍵到類名」的映射外置到配置文件中,新增角色只需改配置而不必重新編譯,是三種裡擴展性最好的。這也正是實際項目中 DI 框架的基本思路雛形。

需要注意的一點:Class.newInstance() 要求目標類必須提供無參構造方法,且該方法不能是私有的。若構造中有必需的初始化邏輯,這種方式就不適用,需要改用 Constructor 顯式調用並處理可能的異常。

完整源碼可查看原文提供的下載地址。這個項目規模不大,但把列表渲染、對話框交互、設計模式與反射這幾件事湊在了一起,作為練習素材的性價比頗高。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved