从Android中的数据库打印所有数据

时间:2017-02-15 06:16:46

标签: android database sqlite printing

我正在尝试从我的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`))";

请帮帮我

1 个答案:

答案 0 :(得分:0)

检查apache velocity库。

设置

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;
    }