您的位置:首页 > 理论基础 > 计算机网络

Android网络编程之一个Android下菜单系统模块的实现(客户端—更新桌号)

2013-05-20 11:33 701 查看
在客户端中我们使用一个ListActivity,这样可以无需布局文件,直接获得其ListView填充功能选项并复写其onListItemClick方法。这里我们先把“更新桌号信息”和“更新菜单信息”的界面都写出来,但先实现“更新桌号信息”功能。

新建一个ListActivity的子类UpdateActivity,并准备界面:

private void updateTables() {
String urlString = HttpUtil.BASE_URL + "servlet/UpdateTableServlet";
InputStream is = null;
URLConnection conn = null;

try {
// 实例化目标servlet的地址并取得连接的输入流
URL url = new URL(urlString);
conn = url.openConnection();
is = conn.getInputStream();

// 准备读取xml文件所需的所有类实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(is);
// 获取所有table节点装入列表
NodeList nodeList = doc.getElementsByTagName("table");

// 获得ContentResolver在更新前删除旧内容
ContentResolver resolver = this.getContentResolver();
Uri tableProviderURI = Tables.CONTENT_URI;
resolver.delete(tableProviderURI, null, null);

// 从xml中提取数据并用ContentProvider插入sqlite表中
for (int i = 0; i < nodeList.getLength(); i++) {
Element e = (Element) nodeList.item(i);
ContentValues values = new ContentValues();
values.put("_id", e.getElementsByTagName("id").item(0).getFirstChild().getNodeValue());
values.put("num", e.getElementsByTagName("num").item(0).getFirstChild().getNodeValue());
values.put("description", e.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
System.out.println("_id: " + e.getElementsByTagName("id").item(0).getFirstChild().getNodeValue());
resolver.insert(tableProviderURI, values);
}
Toast.makeText(UpdateActivity.this, "更新桌位成功", Toast.LENGTH_SHORT).show();

} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}


View Code

最后执行效果可在“点餐”模块中验证,我们在服务器数据库中tabletbl中新建一条第11桌:



然后使用更新操作,再进入点餐功能查看桌号下拉列表:







我们看到桌位信息已经成功更新,下篇我们将完成菜单信息的更新。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐