編輯:關於 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 顯式調用並處理可能的異常。
完整源碼可查看原文提供的下載地址。這個項目規模不大,但把列表渲染、對話框交互、設計模式與反射這幾件事湊在了一起,作為練習素材的性價比頗高。
交叉編譯可在 Android 上運行的 qemu user mode(實作與錯誤排除)
前言 本文記錄在 Ubuntu 64 位元系統上對 qemu 專案進行交叉編譯的過程,並且只編譯與 qemu user mode 相關的程式碼。 下文中提到的「NDK」若
XML 格式資料的生成與解析:從語法基礎到 Android 實戰
一、什麼是 XML XML 的全名是 Extensible Markup Language,也就是「可擴充的標記語言」,它其實是 SGML(標準通用標記語言)裁剪而來的一
QQ證件號碼是什麼?怎麼查詢自己綁定的證件資料
很多人註冊QQ十幾年,卻從沒想過「證件號碼」那一欄裡放的是什麼。答案很直接:那就是你的身分證號。實名制推行之後,註冊與高風險操作幾乎都要求綁手機號或完成實名驗證,這個欄位
Android 自定義 View 入門:從 onMeasure 到 onDraw 的完整實踐
對不少剛入門的 Android 開發者來說,「自定義 View」幾乎是一道心理門檻;但如果你想在職業發展上再往上走一層,這又是繞不開的必修課。本文先把整體思路梳理清楚,再