随手记(四)

接下来是各个版本的滑动,我用了四个fragment来实现,其中有些还需要来添加新功能。

FragmentAccount.java

package com.bank;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

public class FragmentBills extends Fragment {

        //列举数据的ListView
        private ListView mlistbills;
        // 适配器
        private SimpleAdapter mlistbillsAdapter;
        //数据库
        private MySQLiteHelper mMysql;
        private SQLiteDatabase mDataBase;
        // 存储数据的数组列表
        ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();  ;
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.bills, container, false);
                return view;
        }
        //从数据库获得适配器数据
        public void  GetData()
        {
                mMysql = new MySQLiteHelper(getActivity(), "finance.db", null, 1);
                mDataBase = mMysql.getReadableDatabase();

                Cursor cursor = mDataBase.rawQuery("select Fee,Budget from finance",null);
                
                int columnsSize = cursor.getColumnCount();

                HashMap<String,Object> map = new HashMap<String,Object>();
                while (cursor.moveToNext()) {
                        for (int i = 0; i < columnsSize; i++) {
                                if(cursor.getString(1).equals("衣")){
                                        map.put("Type",R.drawable.cloth);
                                } else if(cursor.getString(1).equals("食")) {
                                        map.put("Type",R.drawable.chart);
                                } else if(cursor.getString(1).equals("住")) {
                                        map.put("Type",R.drawable.zhu);
                                } else if(cursor.getString(1).equals("行")) {
                                        map.put("Type",R.drawable.getmoney);
                                }else if(cursor.getString(1).equals("其他")) {
                                        map.put("Type",R.drawable.getmoney);
                                }

                                map.put("Time", cursor.getString(2));
                                map.put("Fee", cursor.getString(3));
                                map.put("Remarks", cursor.getString(4));
                        }
                      listData.add(map);
                }


                DecimalFormat df = new DecimalFormat("###.##");
//                textPay.setText(String.valueOf(df.format(resultPay)));
//                textIncome.setText(String.valueOf(df.format(resultIncome)));
//                textRemainder.setText(String.valueOf(df.format(resultIncome - resultPay)));

                cursor.close();
                mDataBase.close();
                mMysql.close();
        }
}

FragmentFind.java

package com.bank;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import android.database.sqlite.SQLiteDatabase;

public class FragmentFind extends Fragment implements View.OnClickListener {
    private FrameLayout frameLayout_recorder;
    private FrameLayout frameLayout_bills;
    private FrameLayout frameLayout_test;
    private FrameLayout frameLayout_plan;
    private FrameLayout frameLayout_SubmitData;
    private FrameLayout frameLayout_ExportData;
    //数据库
    private MySQLiteHelper mMysql;
    private SQLiteDatabase mDataBase;
    //震动的类
    private Vibrator vibrator;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.deal_data, container, false);
        frameLayout_recorder = (FrameLayout) view.findViewById(R.id.frameLayoutRecorder);
        frameLayout_bills = (FrameLayout) view.findViewById(R.id.frameLayoutStream);
        frameLayout_test = (FrameLayout) view.findViewById(R.id.frameLayoutBalance);
        frameLayout_plan = (FrameLayout) view.findViewById(R.id.frameLayoutPlan);
        frameLayout_SubmitData = (FrameLayout) view.findViewById(R.id.frameLayoutSubmitdata);
        frameLayout_ExportData = (FrameLayout) view.findViewById(R.id.frameLayoutExportdata);

        frameLayout_test.setOnClickListener(this);
        frameLayout_bills.setOnClickListener(this);
        frameLayout_recorder.setOnClickListener(this);
        frameLayout_plan.setOnClickListener(this);
        frameLayout_SubmitData.setOnClickListener(this);
        frameLayout_ExportData.setOnClickListener(this);
        return view;
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.frameLayoutRecorder:
                Intent intent = new Intent(getActivity(), RecorderActivity.class);
                startActivity(intent);
                break;
            case R.id.frameLayoutStream:
                startActivity(new Intent(getActivity(), BillsActivity.class));
                break;
            case R.id.frameLayoutBalance:
                final int SHOW_INFO = 1;
                Intent testintent = new Intent(getActivity(), TestActivity.class);
                Bundle bundle = new Bundle();
                //传递name参数为tinyphp
                bundle.putString("Type", "其他");
                bundle.putString("Time", "2016-05-21");
                bundle.putString("Budget", "收入");
                bundle.putDouble("Fee", 12.5);
                bundle.putInt("ID", 12);
                bundle.putString("Remarks", "现金交易");
                testintent.putExtras(bundle);
                startActivityForResult(testintent, SHOW_INFO);
                break;
            case R.id.frameLayoutPlan:
                //消息通知栏
                //定义NotificationManager
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getActivity().getSystemService(ns);
                //定义通知栏展现的内容信息
                int icon = R.drawable.back;
                CharSequence tickerText = "通知栏";
                long when = System.currentTimeMillis();
                Notification notification = new Notification(icon, tickerText, when);

                //定义下拉通知栏时要展现的内容信息
                Context context = getActivity().getApplicationContext();
                CharSequence contentTitle = "已经完成同步";
                CharSequence contentText = "进入程序查看详情";
                Intent notificationIntent = new Intent(getActivity(), MainActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(getActivity(), 0,
                        notificationIntent, 0);
                Notification.Builder builder = new Notification.Builder(context)
                        .setAutoCancel(true)
                        .setContentTitle(contentTitle)
                        .setContentText(contentText)
                        .setContentIntent(contentIntent)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setWhen(System.currentTimeMillis())
                        .setOngoing(true);
                notification=builder.getNotification();
                //notification.setLatestEventInfo(context, contentTitle, contentText,contentIntent);

                //用mNotificationManager的notify方法通知用户生成标题栏消息通知
                mNotificationManager.notify(1, notification);
                startActivity(new Intent(getActivity(), PlanActivity.class));
                break;
            case R.id.frameLayoutSubmitdata:
                Toast.makeText(getActivity(), "正在同步", Toast.LENGTH_SHORT).show();
                Intent dataintent = new Intent();
                dataintent.setClass(getActivity(), SubmitDataService.class);
                getActivity().startService(dataintent);
                break;
            case R.id.frameLayoutExportdata:
                Toast.makeText(getActivity(),"正在导出数据",Toast.LENGTH_SHORT).show();
                ExportData();
                break;
            default:
                break;
        }
    }
    public void ExportData() {
        String outputFile = "finance.xls";
        try {
            // 判断是否存在SD卡
            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            } else {
                Toast.makeText(getActivity(), "sd卡不存在", Toast.LENGTH_LONG).show();
                return;
            }
            String sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Finance/";
            File file = new File(sdCardRoot + outputFile);
            // 创建新的Excel 工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();
            // 在Excel工作簿中建一工作表,其名为缺省值
            // 如要新建一名为"效益指标"的工作表,其语句为:
            // HSSFSheet sheet = workbook.createSheet("效益指标");
            HSSFSheet sheet = workbook.createSheet("消费记录");
            // 在索引0的位置创建行(最顶端的行)
            HSSFRow row = sheet.createRow((short) 0);
            //创建表头
            HSSFCell IDCell = row.createCell(0);
            IDCell.setCellType(HSSFCell.CELL_TYPE_STRING);
            IDCell.setCellValue("ID(ID号)");

            HSSFCell TypeCell = row.createCell(1);
            TypeCell.setCellType(HSSFCell.CELL_TYPE_STRING);
            TypeCell.setCellValue("Type(类型)");

            HSSFCell TimeCell = row.createCell(2);
            TimeCell.setCellType(HSSFCell.CELL_TYPE_STRING);
            TimeCell.setCellValue("Time(时间)");

            HSSFCell FeeCell = row.createCell(3);
            FeeCell.setCellType(HSSFCell.CELL_TYPE_STRING);
            FeeCell.setCellValue("Fee(费用)");

            HSSFCell RemarksCell = row.createCell(4);
            RemarksCell.setCellType(HSSFCell.CELL_TYPE_STRING);
            RemarksCell.setCellValue("Remarks(备注)");

            HSSFCell BudgetCell = row.createCell(5);
            BudgetCell.setCellType(HSSFCell.CELL_TYPE_STRING);
            BudgetCell.setCellValue("Budget(收支)");
            mMysql = new MySQLiteHelper(getActivity(), "finance.db", null, 1);
            mDataBase = mMysql.getReadableDatabase();
            Cursor cursor = mDataBase.rawQuery("select * from finance", null);
            cursor.moveToFirst();
            int columnsSize = cursor.getColumnCount();
            int number = 0;
            while (number < cursor.getCount()) {
                String budget = cursor.getString(cursor.getColumnIndex("Budget"));
                int ID = cursor.getInt(cursor.getColumnIndex("ID"));
                Double Fee = cursor.getDouble(cursor.getColumnIndex("Fee"));
                String Time = cursor.getString(cursor.getColumnIndex("Time"));
                String Remarks = cursor.getString(cursor.getColumnIndex("Remarks"));
                String Type = cursor.getString(cursor.getColumnIndex("Type"));
                row = sheet.createRow(number + 1);
                for (int i = 0; i < 6; i++) {
                    IDCell = row.createCell(i);
                    IDCell.setCellType(HSSFCell.CELL_TYPE_STRING);
                    switch (i) {
                        case 0:
                            IDCell.setCellValue(ID);
                            break;
                        case 1:
                            IDCell.setCellValue(Type);
                            break;
                        case 2:
                            IDCell.setCellValue(Time);
                            break;
                        case 3:
                            IDCell.setCellValue(Fee);
                            break;
                        case 4:
                            IDCell.setCellValue(Remarks);
                            break;
                        case 5:
                            IDCell.setCellValue(budget);
                            break;
                        default:
                            break;
                    }
                }
                cursor.moveToNext();
                number++;
            }
            cursor.close();
            mDataBase.close();
            mMysql.close();

            // 新建一输出文件流
            FileOutputStream fOut = new FileOutputStream(sdCardRoot + outputFile);
            // 把相应的Excel 工作簿存盘
            workbook.write(fOut);
            fOut.flush();
            // 操作结束,关闭文件
            fOut.close();
            workbook.close();
            //消息通知栏
            //定义NotificationManager
            String ns = Context.NOTIFICATION_SERVICE;
            NotificationManager mNotificationManager = (NotificationManager) getActivity().getSystemService(ns);
            //定义通知栏展现的内容信息
            int icon = R.drawable.back;
            CharSequence tickerText = "通知栏";
            long when = System.currentTimeMillis();
            Notification notification = new Notification(icon, tickerText, when);
            //定义下拉通知栏时要展现的内容信息
            Context context = getActivity().getApplicationContext();
            CharSequence contentTitle = "已经导出数据";
            CharSequence contentText = "点击查看文件";
            File path = new File(Environment.getExternalStorageDirectory().getPath());
            Intent notificationIntent = new Intent(Intent.ACTION_GET_CONTENT);
            notificationIntent.setDataAndType(Uri.fromFile(file), "Finance/");
            PendingIntent contentIntent = PendingIntent.getActivity(getActivity(), 0,
                    notificationIntent, 0);
           // notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
            Notification.Builder builder = new Notification.Builder(context)
                    .setAutoCancel(true)
                    .setContentTitle(contentTitle)
                    .setContentText(contentText)
                    .setContentIntent(contentIntent)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setWhen(System.currentTimeMillis())
                    .setOngoing(true);
            notification=builder.getNotification();
            //用mNotificationManager的notify方法通知用户生成标题栏消息通知
            mNotificationManager.notify(1, notification);
            vibrator = (Vibrator)getActivity().getSystemService(Context.VIBRATOR_SERVICE);
            long [] pattern = {100,400,100,400};   // 停止 开启 停止 开启
            vibrator.vibrate(pattern, -1);

        } catch (Exception e) {
            Toast.makeText(getActivity(), "写入失败" + e, Toast.LENGTH_LONG).show();
        } finally {
            //  Toast.makeText(getActivity(), "关闭", Toast.LENGTH_LONG).show();
        }
    }
}

目前主要完成这两个界面的功能。

 

posted @ 2017-06-13 16:00  小绒花  阅读(270)  评论(0编辑  收藏  举报