记录一下,为了以后自己可以更新知识,方便查找
public class TableActivity extends Activity{ private ArrayList<HashMap<String,String>> printerList; // 数据库操作类 private UserDao userDao; //线性布局 private LinearLayout linearLayout1; //返回按钮 private Button button3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_table); // 注册组件 userDao = new UserDao(this); button3 = (Button) findViewById(R.id.button3); linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1); //求出用户的数量,并且显示 int userSize = userDao.dbGetUserSizeAP(); TextView textView1 = new TextView(this); textView1.setText("共有" + userSize + "条数据"); textView1.setTextSize(24); linearLayout1.addView(textView1, 1);//添加的textView1放到activity_table.xml中tag=1的View那个位置 //如果用户数量为0就不用搞这么东西添加表格布局了 if (userSize > 0) { ArrayList<User> userList = userDao.dbQueryAllAP();//查出表中的所有用户放到一个ArrayList中~~~~~~~~~~~~~~~~~ TableLayout tableLayout1 = new TableLayout(this);//新建一个表格布局 tableLayout1.setStretchAllColumns(true);//自动宽度,使表格在横向占据100% //打印表头 TableRow tableRow = new TableRow(this);//新建一行 TextView textView = new TextView(this);//新建一个TextView textView.setTextSize(18);//设置字体 textView.setText("testtype"); tableRow.addView(textView);//放到行中,自动增加一个单元格 textView = new TextView(this); textView.setTextSize(18); textView.setText("DP"); tableRow.addView(textView); textView = new TextView(this); textView.setTextSize(18); textView.setText("DT"); tableRow.addView(textView); textView = new TextView(this); textView.setTextSize(18); textView.setText("DTed"); tableRow.addView(textView); textView = new TextView(this); textView.setTextSize(18); textView.setText("DTed"); tableRow.addView(textView); // 新建的行TableRow添加到表格TableLayout之中 tableLayout1.addView(tableRow, new TableLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); ArrayList<HashMap<String,String>> printerList = new ArrayList<HashMap<String, String>>(); //打印用户信息 for (int i = 0; i < userSize; i++) { User user = userList.get(i); HashMap<String,String> printerDate = new HashMap<String,String>(); printerDate.put("Testtype",user.getTesttype()); printerDate.put("dt", String.valueOf(user.getDp())); printerDate.put("dp",String.valueOf(user.getDt())); printerDate.put("dted",String.valueOf(user.getDped())); printerDate.put("dped",String.valueOf(user.getDped())); printerList.add(printerDate); // 一个用户占据一行 tableRow = new TableRow(this); textView = new TextView(this); textView.setTextSize(12); textView.setText(user.getTesttype()); tableRow.addView(textView); textView = new TextView(this); textView.setTextSize(12); textView.setText(String.valueOf(user.getDp())); tableRow.addView(textView); textView = new TextView(this); textView.setTextSize(12); textView.setText(String.valueOf(user.getDt())); tableRow.addView(textView); textView = new TextView(this); textView.setTextSize(12); textView.setText(String.valueOf(user.getDped())); tableRow.addView(textView); textView = new TextView(this); textView.setTextSize(12); textView.setText(String.valueOf(user.getDted())); tableRow.addView(textView); Button button = new Button(this); button.setText("删除"); button.setTextSize(12); button.setId(user.getId());//设置按钮的id就是用户的id button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { userDao.dbDeleteUserAP(view.getId());//这样可以获取按钮的id //修改密码,更新数据库之后,刷新一下这个TableActivity finish(); Intent intent = new Intent(TableActivity.this, TableActivity.class); startActivity(intent); Toast.makeText(TableActivity.this, "删除成功!", Toast.LENGTH_SHORT).show(); } }); tableRow.addView(button);//将这个按钮添加到这行中 // 新建的TableRow添加到TableLayout tableLayout1.addView(tableRow, new TableLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); } Toast.makeText(TableActivity.this,String.valueOf(printerList),Toast.LENGTH_LONG).show(); String printerDateSave = String.valueOf(printerList); saveToSDCard("aPenetrometerDate.txt",printerDateSave); linearLayout1.addView(tableLayout1, 5);//把这个表格放到activity_table.xml中tag=2的View那个位置 } //返回MainActivity button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { finish(); Intent intent = new Intent(TableActivity.this, MainActivity.class); startActivity(intent); } }); } public boolean saveToSDCard(String fileName, String content) { // judge weather the SDCard exits,and can be read and written if (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return false; } FileOutputStream fileOutputStream = null; File file = new File(Environment.getExternalStorageDirectory(), fileName); try { fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes()); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return false; } // 对物理按钮的监听 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: finish();// 关闭这个Activity。 Intent intent = new Intent(TableActivity.this, MainActivity.class); startActivity(intent); break; } return super.onKeyDown(keyCode, event); } } //表格工具类public class ExcelUtil { //内存地址 public static String root = Environment.getExternalStorageDirectory() .getPath(); public static void writeExcel(Context context, List<Order> exportOrder, String fileName) throws Exception { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)&&getAvailableStorage()>1000000) { Toast.makeText(context, "SD卡不可用", Toast.LENGTH_LONG).show(); return; } String[] artTitle = {"沥青针入度实验"}; String[] title = { "订单", "店名", "电话", "地址" }; File file; File dir = new File(context.getExternalFilesDir(null).getPath()); file = new File(dir, fileName + ".xls"); if (!dir.exists()) { dir.mkdirs(); } // 创建Excel工作表 WritableWorkbook wwb=null; OutputStream os = new FileOutputStream(file); wwb = Workbook.createWorkbook(os); // 添加第一个工作表并设置第一个Sheet的名字 WritableSheet sheet = wwb.createSheet("订单", 0); sheet.getSettings().setShowGridLines(true); sheet.getSettings().setOrientation(PORTRAIT );//LANDSCAPE 横向 ; PORTRAIT 纵向 sheet.getSettings().setScaleFactor(200);//收放比例 百分比形式 sheet.getSettings().setFitWidth((short) 1);//页宽 sheet.getSettings().setFitHeight((short) 0);//页高 sheet.getSettings().setPaperSize(PaperSize.A4);//A4规格:21*29.7cm(并非16开) /* sheet.getSettings().setTopMargin (HSSFSheet.TopMargin,(short)0.6);//页边距 上下左右 sheet.getSettings().setBottomMargin (0.3); sheet.getSettings().setLeftMargin (0.5); sheet.getSettings().setRightMargin (0.5); sheet.getSettings().setHeaderMargin(0.12);//页眉,页脚 sheet.getSettings().setFooterMargin(0.13);*/ //ws.mergeCells(0, 0, 0, 1);//合并单元格,第一个参数:要合并的单元格最左上角的列号,第二个参数: // 要合并的单元格最左上角的行号,第三个参数:要合并的单元格最右角的列号,第四个参数:要合并的单元格最右下角的行号, Label arttitle = new Label(0,0,artTitle[0],getArtTitle()); sheet.mergeCells(0,0,(title.length-1),1); sheet.addCell(arttitle); for (int i = 0; i < title.length; i++) { Label label; // Label(x,y,z) 代表单元格的第x+1列,第y+1行, 内容z // 在Label对象的子对象中指明单元格的位置和内容 label = new Label(i, 2, title[i], getHeader()); // 将定义好的单元格添加到工作表中 sheet.addCell(label); } for (int i = 0; i < exportOrder.size(); i++) { Order order = exportOrder.get(i); Label orderNum = new Label(0, i + 3, order.id,getBody()); Label restaurant = new Label(1, i + 3, order.restName,getBody()); Label nameLabel = new Label(2,i+3,order.restPhone,getBody()); Label address = new Label(3, i + 3, order.receiverAddr,getBody()); sheet.addCell(orderNum); sheet.addCell(restaurant); sheet.addCell(nameLabel); sheet.addCell(address); Toast.makeText(context, "写入成功", Toast.LENGTH_LONG).show(); } // 写入数据 wwb.write(); // 关闭文件 wwb.close(); } public static WritableCellFormat getArtTitle() { WritableFont fontArt = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);// 定义字体 try { fontArt.setColour(Colour.GREEN);// 蓝色字体 } catch (WriteException e1) { e1.printStackTrace(); } WritableCellFormat formatArt = new WritableCellFormat(fontArt); try { formatArt.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中 formatArt.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中 formatArt.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);// 黑色边框 formatArt.setBackground(Colour.YELLOW2);// 黄色背景 } catch (WriteException e) { e.printStackTrace(); } return formatArt; } public static WritableCellFormat getHeader() { WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD);// 定义字体 try { font.setColour(Colour.BLUE);// 蓝色字体 } catch (WriteException e1) { e1.printStackTrace(); } WritableCellFormat format = new WritableCellFormat(font); try { format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中 format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中 format.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);// 黑色边框 format.setBackground(Colour.YELLOW);// 黄色背景 } catch (WriteException e) { e.printStackTrace(); } return format; } public static WritableCellFormat getBody() { WritableFont fontbody = new WritableFont(WritableFont.ARIAL, 6, WritableFont.BOLD);// 定义字体 try { fontbody.setColour(Colour.BLUE);// 蓝色字体 } catch (WriteException e1) { e1.printStackTrace(); } WritableCellFormat formatbody = new WritableCellFormat(fontbody); try { formatbody.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中 formatbody.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中 formatbody.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);// 黑色边框 formatbody.setBackground(Colour.GOLD);// 黄色背景 } catch (WriteException e) { e.printStackTrace(); } return formatbody; } /** 获取SD可用容量 */ private static long getAvailableStorage() { StatFs statFs = new StatFs(root); long blockSize = statFs.getBlockSize(); long availableBlocks = statFs.getAvailableBlocks(); long availableSize = blockSize * availableBlocks; // Formatter.formatFileSize(context, availableSize); return availableSize; } }
//读取文件的三种路径:String printTxtPath = getApplicationContext().getPackageResourcePath() + "/files/" + fileName;
=> /data/app/com.example.fileoperation-2.apk/files/printMenu.txtString printTxtPath = getApplicationContext().getFilesDir();
String printTxtPath = getApplicationContext().getFilesDir().getAbsolutePath();
=> /data/data/com.example.fileoperation/files//获取当前程序路径
getApplicationContext().getFilesDir().getAbsolutePath();
//获取该程序的安装包路径
String path=getApplicationContext().getPackageResourcePath();
//获取程序默认数据库路径
getApplicationContext().getDatabasePath(s).getAbsolutePath();
直接调取文件路径不好用,因为要实现可直接操作的,所有这样考虑调取文件管理器