Android网络编程之一个Android下菜单系统模块的实现(客户端—开桌功能(下部))

本篇来讨论OrderActivity中的具体操作

组件等准备:

public class OrderActivity extends Activity {
    
    private Spinner tableNoSpinner = null;
    private EditText personNumEditText = null;
    private Button startBtn, addMealBtn, orderBtn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
        
        tableNoSpinner = (Spinner) findViewById(R.id.tableNoSpinner);
        personNumEditText = (EditText) findViewById(R.id.personNumEditText);
        startBtn = (Button) findViewById(R.id.startBtn);
        addMealBtn = (Button) findViewById(R.id.addMealBtn);
        orderBtn = (Button) findViewById(R.id.orderBtn);
        
        startBtn.setOnClickListener(new StartBtnListener());
        addMealBtn.setOnClickListener(new AddMealBtnListener());
        orderBtn.setOnClickListener(new OrderBtnListener());
        // 给桌号下拉列表填值
        setTableNoSpinner();
    }
View Code

效果:

 

填充桌号下拉列表:

    private void setTableNoSpinner() {
        // 调用testQuery获取结果集
        Cursor result = testQuery(null) ;
        OrderActivity.this.startManagingCursor(result) ;    
        List<Map<String,Object>> tables = new ArrayList<Map<String,Object>>() ;
        for (result.moveToFirst(); !result.isAfterLast(); result.moveToNext()) {
            Map<String,Object> table = new HashMap<String,Object>() ;
            table.put("_id", result.getInt(0)) ;
            table.put("num", result.getInt(1)) ;
            table.put("description", result.getString(2)) ;
            tables.add(table) ;
        }
        List<Integer> tableNoList = new ArrayList<Integer>();
        for (int i = 0; i < tables.size(); i++) {
            Map<String,Object> table = tables.get(i);
            tableNoList.add((Integer) table.get("_id"));
        }
        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(OrderActivity.this, android.R.layout.simple_spinner_item, tableNoList);
        tableNoSpinner.setAdapter(adapter);
        
    }
    
    private Cursor testQuery(String id) {
        if(id == null || "".equals(id)) {
            return super.getContentResolver().query(Tables.CONTENT_URI, null, null, null, Tables.SORT_ORDER);
        } else {
            return super.getContentResolver().query(Uri.withAppendedPath(Tables.CONTENT_URI,id), null, null, null, Tables.SORT_ORDER);
        }
    }
View Code

效果:

 

开桌的监听事件,负责把数据传到服务器端:

private class StartBtnListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            startOrder();
        }
        
    }
    private void startOrder() {
        // 得到开桌时间
        String orderTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        // 得到操作用户id(之前登陆的时候储存在本地)
        SharedPreferences sp = getSharedPreferences("user_msg", MODE_WORLD_READABLE);
        String userId = sp.getString("id", "");
        // 得到所选桌号
        String tableId = this.tableNoSpinner.getSelectedItem().toString();
        // 得到人数
        String personNum = this.personNumEditText.getText().toString();
        if (personNum == null || personNum.equals("")) {
            Toast.makeText(OrderActivity.this, "请输入人数", Toast.LENGTH_SHORT).show();
            return;
        }
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("orderTime", orderTime));
        params.add(new BasicNameValuePair("userId", userId));
        params.add(new BasicNameValuePair("tableId", tableId));
        params.add(new BasicNameValuePair("personNum", personNum));
        
        // 向服务器传数据
        String url = HttpUtil.BASE_URL + "servlet/StartTableServlet";
        HttpPost request = HttpUtil.getHttpPost(url); 
        String result = HttpUtil.queryStringForPost(request, params);
        Toast.makeText(OrderActivity.this, "开桌成功, 订单编号: " + result, Toast.LENGTH_SHORT).show();
    }
    
}
View Code

效果:

 

成功后,我们来检查MySQL数据库的信息是否更新:

第11号订单,6桌10人,信息已经成功添加,再来看tabletbl中的flag是否更新:

6号桌flag字段变为1,说明此桌已经有人,信息更新成功。

posted @ 2013-05-15 09:24  来杯冰镇魔卡  阅读(301)  评论(0编辑  收藏  举报