小項目demon3

MoreActivity:android

public class MoreActivity extends BaseActivity {數據庫

 Button btn_browseOrder;
 Button btn_change_table;
 Button btn_merge_table;
 Button btn_option;
  private Gallery pictureGallery = null ;
  private int[] picture = {
    R.drawable.gallery1,  R.drawable.gallery2,    R.drawable.gallery3,    R.drawable.gallery4,    R.drawable.gallery,    R.drawable.gallery6,  };
  private int index = 0 ;   
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState); }app

  @Override
 protected void initViews() {
  this.setContentView(R.layout.more);  
  pictureGallery = (Gallery)findViewById(R.id.gallery_more);
  ImageAdapter adapter = new ImageAdapter(this);
  this.pictureGallery.setAdapter(adapter);
  Timer timer = new Timer();
  timer.schedule(task, 2000, 2000); 
  btn_browseOrder = (Button) this.findViewById(R.id.btn_browseOrder);
  btn_change_table = (Button) this.findViewById(R.id.btn_change_table);
  btn_merge_table = (Button) this.findViewById(R.id.btn_merge_table);
  btn_option = (Button) this.findViewById(R.id.btn_option);
  //設置監聽
  BtnListener listener = new BtnListener();  btn_browseOrder.setOnClickListene(listener);  btn_change_table.setOnClickListener(listener);  btn_merge_table.setOnClickListener(listener);  btn_option.setOnClickListener(listener);
 }ide

 class BtnListener implements OnClickListener{
  @Override
  public void onClick(View v) {
   Intent i_startAct = null;
   switch(v.getId()){
   case R.id.btn_browseOrder://查單
    i_startAct = new Intent(MoreActivity.this, OrderListActivity.class);
    i_startAct.putExtra("mode", MODE_BROWSE_ORDER);
    break;
   case R.id.btn_change_table://換臺
    i_startAct = new Intent(MoreActivity.this, ChangeTableActivity.class);
    i_startAct.putExtra("mode", MODE_CHANGE_TABLE);
    break;
   case R.id.btn_merge_table://並臺
    i_startAct = new Intent(MoreActivity.this, ChangeTableActivity.class);
    i_startAct.putExtra("mode", MODE_MERGE_TABLE);
    break;
   case R.id.btn_option://更新數據選項
    i_startAct = new Intent(MoreActivity.this, OptionMenuActivity.class);
    i_startAct.putExtra("mode", MODE_OPTION);
    break;   }
   i_startAct.putExtra("username", getIntent().getStringExtra("username"));
   startActivity(i_startAct);  } }
 private TimerTask task = new TimerTask() {
  @Override
  public void run() {
   Message message = new Message();
   message.what = 2 ;
   index = pictureGallery.getSelectedItemPosition();
   index ++ ;
   handler.sendMessage(message); } };
 private Handler handler = new Handler(){ui

  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   switch (msg.what) {
   case 2:
    pictureGallery.setSelection(index);
    break;this

 default:
    break;   }  } };

 class ImageAdapter extends BaseAdapter{
  private int GalleryItemBackground;
     private Context context ;
    public ImageAdapter(Context context ){
     this.context = context ;
     TypedArray typedArray = context.obtainStyledAttributes(R.styleable.Gallery);
     GalleryItemBackground = typedArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
    } 
 public int getCount() {
   return Integer.MAX_VALUE;
  }
  public Object getItem(int position) {
   return position;
  }線程

  public long getItemId(int position) {
   return position;
  }code

  public View getView(int position, View convertView, ViewGroup parent) {
   ImageView imageView = new ImageView(context);
   imageView.setImageResource(picture[position % picture.length]);
   imageView.setScaleType(ImageView.ScaleType.FIT_XY);
   imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
   imageView.setBackgroundResource(GalleryItemBackground);
   return imageView;  }   }
 @Override
 protected void getExtras() {}}xml

 more.xml:
pdo

ChangeTableActivity:

public class ChangeTableActivity extends BaseActivity {TextView tv_tableA, tv_tableB;
 Spinner spnr_tableNumA, spnr_tableNumB;
 Button btn_tableChange;
 Button btn_back;
 int mode = 0;//標識當前模式
 String username; 
 String[] availableTableList;
  Handler my_handler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
   //關閉進度條
   if(p_dialog.isShowing())
    p_dialog.dismiss();
   switch(msg.what){
   case WHAT_LOADING_COMPLETED: //爲桌號列表插入內容
    setTableList();
    break;
   case WHAT_SUBMIT_COMPLETE: //提交完成
    Log.i("aaa", msg.getData().getString("msg")+" bbb");
    if(msg.getData().getInt("code") == 200){
     Toast.makeText(getApplicationContext(),
         getString(R.string.submit_success),
         Toast.LENGTH_SHORT).show();
     //更新數據庫中的訂單號
     OrderLogic.updateOrderId(getApplicationContext(),
           (String)spnr_tableNumA.getSelectedItem(),
           (String)spnr_tableNumB.getSelectedItem(),
           msg.getData().getString("new_id"),
           mode);
    }else{  
     Toast.makeText(getApplicationContext(),
       msg.getData().getString("msg"),
       Toast.LENGTH_SHORT).show();    }
    break;
   case WHAT_SUBMIT_FAILED://提交失敗
    Toast.makeText(getApplicationContext(),
        getString(R.string.submit_failed),
        Toast.LENGTH_SHORT).show();
    break; }  } };
 TableLogic tableLogic = new TableLogic(); 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState); 
  //進度條
  p_dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  p_dialog.setMessage(getString(R.string.is_loading));
  p_dialog.show();//顯示進度條
  //讀取桌號線程
  new Thread(){
   @Override
   public void run() {
    availableTableList = tableLogic.getFreeTable(ChangeTableActivity.this);
    //爲handler發送空消息初始化UI
    my_handler.sendEmptyMessage(WHAT_LOADING_COMPLETED);   }  }.start();}

 /**
  * 控件初始化
  */
 @Override
 protected void initViews() {
  this.setContentView(R.layout.table_change);
    tv_tableA = (TextView) this.findViewById(R.id.tv_tableA);
  tv_tableB = (TextView) this.findViewById(R.id.tv_tableB);
  spnr_tableNumA = (Spinner) this.findViewById(R.id.spnr_tableNumA);
  spnr_tableNumB = (Spinner) this.findViewById(R.id.spnr_tableNumB);
  btn_tableChange = (Button) this.findViewById(R.id.btn_tableChange);
  btn_back = (Button) this.findViewById(R.id.btn_back4table);
//  根據換桌、並桌設置TextView內容和Spinner標題
  switch(mode){
  case MODE_CHANGE_TABLE://換臺
   tv_tableA.setText(getString(R.string.cur_table_num));
   tv_tableB.setText(getString(R.string.new_table_num));
   spnr_tableNumA.setPrompt(getString(R.string.cur_table_num));
   spnr_tableNumB.setPrompt(getString(R.string.new_table_num));
   break;
  case MODE_MERGE_TABLE://並臺
   tv_tableA.setText(getString(R.string.table_num_a));
   tv_tableB.setText(getString(R.string.table_num_b));
   spnr_tableNumA.setPrompt(getString(R.string.table_num_a));
   spnr_tableNumB.setPrompt(getString(R.string.table_num_b));
   break;
  }
  //設置提交按鈕監聽
  btn_tableChange.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View arg0) {
    if(isSameTable()){
     Toast.makeText(ChangeTableActivity.this,
        getString(R.string.same_table_alert), Toast.LENGTH_SHORT).show();
    }else{
     //顯示確認提交對話框
     showConfirmDialog();}  } });
  //設置返回按鈕監聽
  btn_back.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    ChangeTableActivity.this.finish();  } });}

 /**
  * 爲桌號列表插入內容
  */
 private void setTableList() {
  //設置spinner的內容
  ArrayAdapter<String> adapter_tableNum =
    new ArrayAdapter<String>(
      this,
    android.R.layout.simple_spinner_item,
    availableTableList);
  adapter_tableNum.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spnr_tableNumA.setAdapter(adapter_tableNum);
  spnr_tableNumB.setAdapter(adapter_tableNum);
 }

 /**
  * 檢查兩桌號是否相同
  * @return
  */
 private boolean isSameTable() {
  if(spnr_tableNumA.getSelectedItem().equals(spnr_tableNumB.getSelectedItem()))
   return true;
  return false;}

 /**
  * 顯示確認對話框
  */
 private void showConfirmDialog() {
  //建立dialog提示確認
  AlertDialog.Builder dlg_builder = new AlertDialog.Builder(ChangeTableActivity.this);
  //標題
  dlg_builder.setTitle(getString(R.string.confirm));
  //構建提示語句
  StringBuilder dlgMessageBuilder = new StringBuilder();
  switch(mode){
  case MODE_CHANGE_TABLE://換桌
   dlgMessageBuilder.append("肯定將");
   dlgMessageBuilder.append((String)spnr_tableNumA.getSelectedItem());
   dlgMessageBuilder.append("桌換到");
   dlgMessageBuilder.append((String)spnr_tableNumB.getSelectedItem());
   dlgMessageBuilder.append("桌嗎?");
   break;
  case MODE_MERGE_TABLE://並桌
   dlgMessageBuilder.append("肯定將");
   dlgMessageBuilder.append((String)spnr_tableNumA.getSelectedItem());
   dlgMessageBuilder.append("桌合併到");
   dlgMessageBuilder.append((String)spnr_tableNumB.getSelectedItem());
   dlgMessageBuilder.append("桌嗎?");
   break;
  }
  dlg_builder.setMessage(dlgMessageBuilder.toString());
  dlgMessageBuilder = null;//StringBuilder置空
  //確認按鈕
  dlg_builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener(){
   @Override
   public void onClick(DialogInterface dialog, int which) {
    new Thread(){
     @Override
     public void run() {
      HashMap<String, Object> result = null;
      //提交數據,獲取返回值
      result = tableLogic.changeTable(getApplicationContext(),
            (String)spnr_tableNumA.getSelectedItem(),  (String)spnr_tableNumB.getSelectedItem(),  username, mode);
      if(result!=null && result.size()>=2){
       Message msg = my_handler.obtainMessage();
       Bundle data = new Bundle();
       data.putInt("code", Integer.valueOf((String)result.get("rt")));
       data.putString("msg", (String)result.get("rtmsg"));
       data.putString("new_id", (String)result.get("new_id"));
       msg.setData(data);
       msg.what=WHAT_SUBMIT_COMPLETE;
       my_handler.sendMessage(msg);
      }else{
       //提交失敗,給handler發送消息
       my_handler.sendEmptyMessage(WHAT_SUBMIT_FAILED);
      }   } }.start();  } });
  //取消按鈕
  dlg_builder.setNegativeButton(getString(R.string.cancel), null);
  dlg_builder.show();
 }@Override protected void getExtras() {  //獲取當前模式標識和用戶名Username  mode = getIntent().getIntExtra("mode", MODE_CHANGE_TABLE);  username = getIntent().getStringExtra("username"); }}

相關文章
相關標籤/搜索