Android手持机扫码出入库的开发详解-7.MVC模式应用案例
MVC模式应用案例
MVC模式程序图
flowchart TD
A[用户操作View界面] --> B[View触发事件]
B --> C[Controller处理事件]
C --> D[调用Model数据操作]
D --> E[Model返回数据结果]
E --> F[Controller更新View]
F --> G[用户看到更新后的界面]
MVC模式应用源代码
public class InventoryCheck {
private String code;
private String hwh;
private String name;
private String dateTime;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getHwh() {
return hwh;
}
public void setHwh(String hwh) {
this.hwh = hwh;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
}
<?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_Code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="材料编码:"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_Hwh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="货位号:"
android:textSize="18sp" />
<Button
android:id="@+id/button_Upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="上传数据" />
<Button
android:id="@+id/button_Back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="返回" />
</LinearLayout>
public class ScanCodeInventoryCheck extends Activity {
private TextView textView_Code;
private TextView textView_Hwh;
private Button button_Upload;
private Button button_Back;
private InventoryCheck inventoryCheck;
private InventoryCheckDAO inventoryCheckDAO;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_code_inventory_check);
textView_Code = (TextView) findViewById(R.id.textView_Code);
textView_Hwh = (TextView) findViewById(R.id.textView_Hwh);
button_Upload = (Button) findViewById(R.id.button_Upload);
button_Back = (Button) findViewById(R.id.button_Back);
inventoryCheckDAO = new InventoryCheckDAOSqliteImpl(this);
button_Upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UploadData();
}
});
button_Back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void handleScanData(String barcode) {
if (barcode.length() == 21) {
inventoryCheck.setCode(barcode);
textView_Code.setText("材料编码:" + barcode);
} else if (barcode.length() == 8 || barcode.length() == 9) {
inventoryCheck.setHwh(barcode);
textView_Hwh.setText("货位号:" + barcode);
}
if (inventoryCheck.getCode() != null && inventoryCheck.getHwh() != null) {
saveInventoryData();
}
}
private void saveInventoryData() {
try {
inventoryCheckDAO.insertData(inventoryCheck);
Toast.makeText(this, "数据保存成功", Toast.LENGTH_SHORT).show();
textView_Code.setText("材料编码:");
textView_Hwh.setText("货位号:");
inventoryCheck = new InventoryCheck();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "数据保存失败", Toast.LENGTH_SHORT).show();
}
}
}