/*
* XmlGui application.
* Written by Frank Ableson for IBM Developerworks
* June 2010
* Use the code as you wish -- no warranty of fitness, etc, etc.
*/
package com.msi.ibm;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import android.app.AlertDialog.*;
import java.util.ListIterator;
import java.lang.Thread;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
public class RunForm extends Activity {
/** Called when the activity is first created. */
String tag = RunForm.class.getName();
XmlGuiForm theForm;
ProgressDialog progressDialog;
Handler progressHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String formNumber = "";
Intent startingIntent = getIntent();
if(startingIntent == null) {
Log.e(tag,"No Intent? We're not supposed to be here...");
finish();
return;
}
formNumber = startingIntent.getStringExtra("formNumber");
Log.i(tag,"Running Form [" + formNumber + "]");
if (GetFormData(formNumber)) {
DisplayForm();
}
else
{
Log.e(tag,"Couldn't parse the Form.");
AlertDialog.Builder bd = new AlertDialog.Builder(this);
AlertDialog ad = bd.create();
ad.setTitle("Error");
ad.setMessage("Could not parse the Form data");
ad.show();
}
}
private boolean DisplayForm()
{
try
{
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
sv.addView(ll);
ll.setOrientation(android.widget.LinearLayout.VERTICAL);
// walk thru our form elements and dynamically create them, leveraging our mini library of tools.
int i;
for (i=0;i<theForm.fields.size();i++) {
if (theForm.fields.elementAt(i).getType().equals("text")) {
theForm.fields.elementAt(i).obj = new XmlGuiEditBox(this,(theForm.fields.elementAt(i).isRequired() ? "*" : "") + theForm.fields.elementAt(i).getLabel(),"");
ll.addView((View) theForm.fields.elementAt(i).obj);
}
if (theForm.fields.elementAt(i).getType().equals("numeric")) {
theForm.fields.elementAt(i).obj = new XmlGuiEditBox(this,(theForm.fields.elementAt(i).isRequired() ? "*" : "") + theForm.fields.elementAt(i).getLabel(),"");
((XmlGuiEditBox)theForm.fields.elementAt(i).obj).makeNumeric();
ll.addView((View) theForm.fields.elementAt(i).obj);
}
if (theForm.fields.elementAt(i).getType().equals("choice")) {
theForm.fields.elementAt(i).obj = new XmlGuiPickOne(this,(theForm.fields.elementAt(i).isRequired() ? "*" : "") + theForm.fields.elementAt(i).getLabel(),theForm.fields.elementAt(i).getOptions());
ll.addView((View) theForm.fields.elementAt(i).obj);
}
}
Button btn = new Button(this);
btn.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
ll.addView(btn);
btn.setText("Submit");
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;
}
}
}
} );
setContentView(sv);
setTitle(theForm.getFormName());
return true;
} catch (Exception e) {
Log.e(tag,"Error Displaying Form");
return false;
}
}
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 we failed....
Message msg = new Message();
msg.what = 1;
this.progressHandler.sendMessage(msg);
return false;
}
}
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;
}
}
private boolean GetFormData(String formNumber) {
try {
Log.i(tag,"ProcessForm");
URL url = new URL("http://servername/xmlgui" + formNumber + ".xml");
Log.i(tag,url.toString());
InputStream is = url.openConnection().getInputStream();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document dom = db.parse(is);
Element root = dom.getDocumentElement();
NodeList forms = root.getElementsByTagName("form");
if (forms.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
有几个网站从事一些非盈利服务,提供一些可轻松设置和使用的表单来进行民意测验和数据收集。本教程介绍一个简单的架构来为 Android 设计类似的应用程序 — 允许非编程员从移动用户收集数据的动态用户界面。在本教程中,您将创建一个拥有服务器端和移动端的示例表单引擎。 http://www.ibm.com/developerworks/cn/xml/tutorials/x-andddyntut/index.html
资源推荐
资源详情
资源评论
收起资源包目录
formsengine.zip (36个子文件)
androidsrc
bin
XMLGUI.apk 21KB
classes.dex 19KB
com
msi
ibm
XmlGui$1.class 2KB
RunForm.class 9KB
R$layout.class 373B
R$id.class 401B
R$drawable.class 379B
RunForm$2.class 1KB
XmlGuiForm.class 3KB
XmlGuiFormField.class 3KB
XmlGui.class 1KB
R$attr.class 319B
RunForm$1.class 2KB
R.class 482B
R$string.class 406B
RunForm$TransmitFormData.class 3KB
XmlGuiEditBox.class 2KB
XmlGuiPickOne.class 2KB
resources.ap_ 11KB
res
drawable-ldpi
icon.png 2KB
raw
drawable-hdpi
icon.png 4KB
values
strings.xml 160B
drawable-mdpi
icon.png 3KB
layout
main.xml 910B
assets
default.properties 380B
gen
com
msi
ibm
R.java 761B
src
com
msi
ibm
RunForm.java 12KB
XmlGui.java 1KB
XmlGuiForm.java 3KB
XmlGuiEditBox.java 1KB
XmlGuiPickOne.java 1KB
XmlGuiFormField.java 2KB
AndroidManifest.xml 776B
serversrc
xmlgui1-post.php 518B
xmlgui2.xml 692B
xmlgui1.xml 489B
共 36 条
- 1
资源评论
- messi_yang2014-02-13非常好,可以直接拿来投入自己的项目中
江王
- 粉丝: 3
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功