zoukankan      html  css  js  c++  java
  • Android网络编程之一个Android下菜单系统模块的实现(客户端—更新桌号)

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

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

    public class UpdateActivity extends ListActivity{
        
        ListView listView = null;
        ListAdapter adapter = null;
        String[] items = {"更新桌号信息", "更新菜单信息"};
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            listView = this.getListView();
            adapter = new ArrayAdapter<String>(UpdateActivity.this, android.R.layout.simple_list_item_1, items);
            listView.setAdapter(adapter);
            
        }
    View Code

    效果如下:

    然后复写onListItemClick()方法:

        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            // 将点击位置声明为一个final值在dialog中的按钮监听方法中使用
            final int pos = position;
            // 弹出dialog提醒是否确定更新, 防止误操作
            Dialog dialog = new AlertDialog.Builder(UpdateActivity.this).setMessage("确定要更新么?")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            if (0 == pos) {
                                // 第0个位置, 执行更新桌号方法
                                updateTables();
                            } else if (1 == pos) {
                                // 第1个位置, 执行更新菜单方法
                                updateMenus();
                            }
                        }
                    }).setNegativeButton("取消", null).show();
        }
    View Code

    更新桌号方法updateTables():

    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桌:

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

    我们看到桌位信息已经成功更新,下篇我们将完成菜单信息的更新。

  • 相关阅读:
    进程间通讯,线程间通讯
    进程与线程
    学习自测6.0
    学习自测5.0
    学习自测4.0
    学习自测3.0
    学习自测2.0
    学习自测1.0
    PS中怎么复制某个图层的效果?
    初学前端犯下的错误(用于反省)
  • 原文地址:https://www.cnblogs.com/moka/p/3088285.html
Copyright © 2011-2022 走看看