2017-02-15 82 views
0

我想從我的android應用程序打印。我想打印所有數據從數據庫和打印在一次。我正在使用藍牙打印機。一個值是一次打印,但我需要一次打印總值。我需要打印像酒店賬單 以下是我的代碼。打印所有數據來自數據庫在Android中

protected void connect() { 
     if(btsocket == null){ 
      Intent BTIntent = new Intent(getApplicationContext(), BTDeviceList.class); 
      this.startActivityForResult(BTIntent, BTDeviceList.REQUEST_CONNECT_BT); 
     } 
     else{ 

      OutputStream opstream = null; 
      try { 
       opstream = btsocket.getOutputStream(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      btoutputstream = opstream; 
      print_bt(); 

     } 

    } 


    private void print_bt() { 
     try { 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      btoutputstream = btsocket.getOutputStream(); 

      byte[] printformat = { 0x1B, 0x21, FONT_TYPE }; 
      btoutputstream.write(printformat); 
      //String msg = message.getText().toString(); 

      btoutputstream.write(orderid.getBytes()); 
      btoutputstream.write(orderid.getBytes()); 
      btoutputstream.write(0x0D); 
      btoutputstream.write(0x0D); 
      btoutputstream.write(0x0D); 
      btoutputstream.flush(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     try { 
      if(btsocket!= null){ 
       btoutputstream.close(); 
       btsocket.close(); 
       btsocket = null; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     try { 
      btsocket = BTDeviceList.getSocket(); 
      if(btsocket != null){ 
       print_bt(); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

This is my Database table That values i want to print: 
String datalista = "CREATE TABLE `user` (`id` integer NOT NULL,`fname` nvarchar(50),`quantity` numeric,`orderId` numeric,PRIMARY KEY(`id`))"; 

請幫我

回答

0

檢查阿帕奇速度圖書館。

設置

private void setupVelocityEngine() throws Exception { 
     Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, "your.class.path.VelocityLogger"); 
     Velocity.setProperty("resource.loader", "android"); 
     Velocity.setProperty("android.resource.loader.class", "your.class.path.AndroidResourceLoader"); 
     Velocity.setProperty("android.content.res.Resources", getResources()); 
     Velocity.setProperty("packageName", getPackageName()); 
     Velocity.init(); 
    } 

用法:

private String getPrintData(User user) throws InvalidDateException{ 
     String data; 
     Template temp = Velocity.getTemplate("your_template.vm"); 

     VelocityContext context = new VelocityContext(); 
     context.put("User Name", user.getName()); 
     context.put("address", user.getAddress()); 
     context.put("invoice_date", getTodaysDate()); 
     context.put("gender", user.getGender()); 
     context.put("age",user.getAge()); 

     StringWriter stringWriter = new StringWriter(); 
     temp.merge(context, stringWriter); 

     data = stringWriter.toString(); 
     return data; 
    } 
相關問題