使用 Android 和 XML 构建动态用户界面(10)
 
- UID
- 1066743
|

使用 Android 和 XML 构建动态用户界面(10)
保存并提交数据现在必须创建一种方法,允许用户通过验证数据并将数据提交到一个服务器来保存数据。
保存现在应该回顾一下 RunForm 类的 DisplayForm() 方法。回想一下,该方法的第一个选项负责绘制表单。接下来,您将检查提交按钮的 onClick() 处理程序,如 所示。
清单 11. onClick() 处理程序1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// check if this form is Valid
if (!CheckForm())
{
AlertDialog.Builder bd = new AlertDialog.Builder(ll.getContext());
AlertDialog ad = bd.create();
ad.setTitle("Error");
ad.setMessage("Please enter all required (*) fields");
ad.show();
return;
}
if (theForm.getSubmitTo().equals("loopback")) {
// just display the results to the screen
String formResults = theForm.getFormattedResults();
Log.i(tag,formResults);
AlertDialog.Builder bd = new AlertDialog.Builder(ll.getContext());
AlertDialog ad = bd.create();
ad.setTitle("Results");
ad.setMessage(formResults);
ad.show();
return;
} else {
if (!SubmitForm()) {
AlertDialog.Builder bd = new AlertDialog.Builder(ll.getContext());
AlertDialog ad = bd.create();
ad.setTitle("Error");
ad.setMessage("Error submitting form");
ad.show();
return;
}
}
}
} );
|
当用户选择提交按钮时,表单条目将被检查,以确保所有必要字段都已填充。否则,将显示一个 AlertDialog 来提醒用户填充所有字段。假定数据输入令人满意,现在可以提交数据了。.
数据提交过程由这个教程应用程序的两个部分完成。如果表单的 submitTo 字段已经设置回送值,则这些值只需回显到屏幕上。这有利于实现测试目的。如果您对表单工具对数据的收集效果感到满意,现在可以将其指向一个服务器页面,该页面负责记录表单条目。
展示了 CheckForm() 方法。这段代码非常直观。每个字段都被检查,看看它们是否必要。如果字段必要但用户没有提供相应信息,将设置一个标志。您可以增强这种标志,向用户提供更具体的反馈。
清单 12. CheckForm() 方法1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| private boolean CheckForm()
{
try {
int i;
boolean good = true;
for (i=0;i<theForm.fields.size();i++) {
String fieldValue = (String)
theForm.fields.elementAt(i).getData();
Log.i(tag,theForm.fields.elementAt(i)
.getName() + " is [" + fieldValue + "]");
if (theForm.fields.elementAt(i).isRequired()) {
if (fieldValue == null) {
good = false;
} else {
if (fieldValue.trim().length() == 0) {
good = false;
}
}
}
}
return good;
} catch(Exception e) {
Log.e(tag,"Error in CheckForm()::" + e.getMessage());
e.printStackTrace();
return false;
}
}
|
现在可以将收集的数据提交到服务器了。检查 中的 SubmitForm()。
清单 13. SubmitForm() 方法1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| private boolean SubmitForm()
{
try {
boolean ok = true;
this.progressDialog = ProgressDialog.show(this,
theForm.getFormName(), "Saving Form Data", true,false);
this.progressHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// process incoming messages here
switch (msg.what) {
case 0:
// update progress bar
progressDialog.setMessage("" + (String) msg.obj);
break;
case 1:
progressDialog.cancel();
finish();
break;
case 2:
progressDialog.cancel();
break;
}
super.handleMessage(msg);
}
};
Thread workthread = new Thread(new TransmitFormData(theForm));
workthread.start();
return ok;
} catch (Exception e) {
Log.e(tag,"Error in SubmitForm()::" + e.getMessage());
e.printStackTrace();
// tell user that the submission failed....
Message msg = new Message();
msg.what = 1;
this.progressHandler.sendMessage(msg);
return false;
}
}
|
|
|
|
|
|
|