3.19 个人作业1源码

昨天发了所有的页面的代码,今天把所有的后台java文件发出

adddata.java

package com.example.temperature;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Adddata extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {



    private String like="";

    private EditText zhonggao1,geli1,yidong1,qita1;

    private Button save,delete;
    private EditText tem;
    private String name,time,locat,tem1;
    public LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();
    //BDAbstractLocationListener为7.2版本新增的Abstract类型的监听接口
    //原有BDLocationListener接口暂时同步保留。具体介绍请参考后文中的说明

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adddata);

        final CheckBox like1=(CheckBox) findViewById(R.id.wu);
        final CheckBox like2=(CheckBox) findViewById(R.id.zhonggao);
        final  CheckBox like3=(CheckBox) findViewById(R.id.geli);
        final CheckBox like4=(CheckBox) findViewById(R.id.yidong);
        final CheckBox like5=(CheckBox) findViewById(R.id.qita);
        like1.setOnCheckedChangeListener(this);
        like2.setOnCheckedChangeListener(this);
        like3.setOnCheckedChangeListener(this);
        like4.setOnCheckedChangeListener(this);
        like5.setOnCheckedChangeListener(this);

        zhonggao1=findViewById(R.id.zhonggao1);
        geli1=findViewById(R.id.geli1);
        yidong1=findViewById(R.id.yidong1);
        qita1=findViewById(R.id.qita1);

        save=(Button)findViewById(R.id.save);
        delete=(Button)findViewById(R.id.delete);
        tem=(EditText)findViewById(R.id.tem);
        tem1=tem.getText().toString();


        Dao dao=new Dao(Adddata.this);
        TextView name1=(TextView)findViewById(R.id.name1);
        name1.setText(dao.find().getName());
        name=dao.find().getName();

        TextView time1 = (TextView) findViewById(R.id.time1);
        time1.setText(getTime());
        time=getTime();
        mLocationClient = new LocationClient(getApplicationContext());
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
        //可选,是否需要地址信息,默认为不需要,即参数为false
        //如果开发者需要获得当前点的地址信息,此处必须为true
        option.setNeedNewVersionRgc(true);
        //可选,设置是否需要最新版本的地址信息。默认需要,即参数为true
        mLocationClient.setLocOption(option);
        //mLocationClient为第二步初始化过的LocationClient对象
        //需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用
        //更多LocationClientOption的配置,请参照类参考中LocationClientOption类的详细说明
        mLocationClient.start();
        TextView loca1 = (TextView) findViewById(R.id.loca1);
        loca1.setText(myListener.getLocat());
        locat=myListener.getLocat();





        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(like1.isChecked()){
                    like+=like1.getText().toString();
                }
                if(like2.isChecked()){
                    like+=like2.getText().toString()+"  说明:"+zhonggao1.getText().toString();
                }
                if(like3.isChecked()){
                    like+=like3.getText().toString()+"  说明:"+geli1.getText().toString();
                }
                if(like4.isChecked()){
                    like+=like4.getText().toString()+"  说明:"+yidong1.getText().toString();
                }
                if(like5.isChecked()){
                    like+=like5.getText().toString()+"  说明:"+qita1.getText().toString();
                }


                addDB(name,time,tem.getText().toString(),locat,like);
                Toast.makeText(Adddata.this, "填加成功", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(Adddata.this,MainActivity.class);
                startActivity(intent);
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Adddata.this,MainActivity.class);
                startActivity(intent);
            }
        });


    }



    public void addDB(String name,String time,String tem,String locat,String like){
        Temdao dao=new Temdao(Adddata.this);
        Tb_tem information=new Tb_tem(dao.getMaxID()+1,name,time, locat,tem,like);
        dao.add(information);
    }



    private String getTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Date curDate = new Date();
        String str = format.format(curDate);
        return str;
    }




    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }
}

addinfor.java

package com.example.temperature;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class Addinfor extends AppCompatActivity {
    private Button btnlnSave,btnlnCancel;
    private EditText tx_id,txname,txphone,txgrade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addinfor);
        btnlnSave=(Button) findViewById(R.id.btnlnSave);
        btnlnCancel=(Button) findViewById(R.id.btnlnCancel);
        tx_id=(EditText) findViewById(R.id.tx_id);
        txname=(EditText) findViewById(R.id.txname);
        txphone=(EditText) findViewById(R.id.txphone);
        txgrade=(EditText) findViewById(R.id.txgrade);

        btnlnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone=txphone.getText().toString();
                if(!phone.isEmpty()){
                    Dao dao=new Dao(Addinfor.this);
                    information information=new information(tx_id.getText().toString(),txname.getText().toString(),
                            txphone.getText().toString(),txgrade.getText().toString());
                    dao.add(information);
                    Toast.makeText(Addinfor.this, "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(Addinfor.this,Login.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(Addinfor.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btnlnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}

Dao.java

package com.example.temperature;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class Dao {

    private DB helper;
    private SQLiteDatabase db;
    public Dao(Context context){
        helper=new DB(context);
    }

    public void add(information information){
        db=helper.getWritableDatabase();
        db.execSQL("insert into information (_id,name,phone,grade) values (?,?,?,?)", new Object[]
                {information.get_id(),information.getName(),information.getPhone(),information.getGrade()});
    }
    public information find(){
        information information=new information();
        db=helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from information",null);
        if (cursor.moveToNext()){
            return new information(cursor.getString(cursor.getColumnIndex("_id")),
                    cursor.getString(cursor.getColumnIndex("name")),
                    cursor.getString(cursor.getColumnIndex("phone")),
                    cursor.getString(cursor.getColumnIndex("grade")));
        }
        return information;
    }



}

DB.java

package com.example.temperature;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DB extends SQLiteOpenHelper {
    private static final int VERSION=1;
    private static final String DBNAME="tem.db";
    public DB(Context context){
        super(context,DBNAME,null,VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table information(_id varcher(20) primary key,name varchar(10),phone varchar(20),grade varchar(20))");
        db.execSQL("create table Tb_tem(_id integer primary key,name varchar(10),time varchar(20),place varchar(40),tem varchar(20),special varchar(40))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

Figure.java

package com.example.temperature;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class Figure extends AppCompatActivity implements View.OnClickListener{

    private Button person,showmap;
    private Intent i = null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.figure);
        person = (Button) findViewById(R.id.person);
        showmap = (Button) findViewById(R.id.showmap);
        person.setOnClickListener(this);
        showmap.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.person:
                i = new Intent(Figure.this, Personfigure.class);
                startActivity(i);
                break;
            case R.id.showmap:
                i = new Intent(Figure.this, Mapfigure.class);
                startActivity(i);
                break;
        }
    }
}

information.java

package com.example.temperature;

public class information {

    private String _id;//学号
    private String name;  //姓名
    private String phone;  //手机
    private String grade;   //班级
    public information(){
        super();
    }
    public information(String id,String name,String phone,String grade){
        super();

        this._id=id;
        this.grade=grade;
        this.name=name;
        this.phone=phone;
    }




    public String get_id() {
        return _id;
    }

    public String getGrade() {
        return grade;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public void set_id(String _id) {
        this._id = _id;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

Login.java

package com.example.temperature;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class Login extends Activity {

    private Button btnadd,btnlogin;
    private EditText txphone;

    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        txphone=(EditText) findViewById(R.id.txphone);

        btnadd= (Button)findViewById(R.id.btnadd);

        btnlogin=(Button)findViewById(R.id.btnlogin);

        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Login.this,Addinfor.class);
                startActivity(intent);
            }
        });
        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Login.this,MainActivity.class);
                Dao dao=new Dao(Login.this);
                if(dao.find().getPhone().isEmpty()){Toast.makeText(Login.this, "请先注册", Toast.LENGTH_SHORT).show();}
                if(dao.find().getPhone().equals(txphone.getText().toString())){
                    startActivity(intent);
                }
                else {
                    Toast.makeText(Login.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }



}

MainActivity.java

package com.example.temperature;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

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.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Cell;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button addbtn, showbtn, exitbtn,printbtn,figurebtn;
    private Intent i = null;
    private DB helper;
    private SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addbtn = (Button) findViewById(R.id.add);
        showbtn = (Button) findViewById(R.id.show);
        exitbtn = (Button) findViewById(R.id.exit);
        printbtn = (Button) findViewById(R.id.print);
        figurebtn = (Button) findViewById(R.id.figure);
        addbtn.setOnClickListener(this);
        showbtn.setOnClickListener(this);
        exitbtn.setOnClickListener(this);
        printbtn.setOnClickListener(this);
        figurebtn.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add:
                i = new Intent(MainActivity.this, Adddata.class);
                startActivity(i);
                break;
            case R.id.show:
                i = new Intent(MainActivity.this, Showdata.class);
                startActivity(i);
                break;
            case R.id.exit:
                i = new Intent(MainActivity.this, Login.class);
                startActivity(i);
                break;
            case R.id.figure:
                i = new Intent(MainActivity.this, Figure.class);
                startActivity(i);
                break;
            case R.id.print:
                information user = getData1();//获取数据库数据1
                List<Tb_tem> people = getData2();
                boolean excel = createExcel(MainActivity.this, user, people);//创建表
                Toast.makeText(MainActivity.this, "导出成功!"+excel, Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private information getData1() {

        Dao dao=new Dao(MainActivity.this);
        information user=dao.find();
        return user;
    }

    private List<Tb_tem> getData2() {
        Temdao temdao=new Temdao(MainActivity.this);
        List<Tb_tem> tb_tem=temdao.get14();
        return  tb_tem;

    }


    private boolean createExcel(Context context,information user, List<Tb_tem> people){
        HSSFWorkbook mWorkbook = new HSSFWorkbook();
//        HSSFCellStyle cellStyle = mWorkbook.createCellStyle();
//        cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中,少jar包
        HSSFSheet mSheet = mWorkbook.createSheet("Student");
        //part1
        //Log.e("main",user.getUserclass());
        createPart1(mSheet ,user);
        //part2
        createExcelHead(mSheet);
        for (Tb_tem person : people) {//遍历元素集合,数据赋值
            createCell(person.getTime(), person.getPlace(), "健康", person.getSpecial(), "",mSheet);
        }

        HSSFRow last = mSheet.createRow(mSheet.getLastRowNum()+1);
        last.createCell(0).setCellValue("本人承诺:自觉履行疫情防控责任和义务,保证以上填报信息全部属实,如有隐瞒,自愿承担相应法律后果。");
        CellRangeAddress region1 = new CellRangeAddress(mSheet.getLastRowNum(), mSheet.getLastRowNum(), 0, 5);
        mSheet.addMergedRegion(region1);

        last = mSheet.createRow(mSheet.getLastRowNum()+1);
        last.createCell(0).setCellValue("本人签字:");
        last.createCell(4).setCellValue("签字日期:");

        region1 = new CellRangeAddress(mSheet.getLastRowNum(), mSheet.getLastRowNum(), 0, 1);
        mSheet.addMergedRegion(region1);
        region1 = new CellRangeAddress(mSheet.getLastRowNum(), mSheet.getLastRowNum(), 2, 3);
        mSheet.addMergedRegion(region1);
        String absolutePath = getApplicationContext().getFilesDir().getAbsolutePath();
        //获取路径 没有会自动创建
        String path = getExternalFilesDir(null).getPath();
        //拼接上文件名称
        String txtName = path + "/excel.xls";
        //路径地址/storage/emulated/0/Android/data/com.xxx.xxx/files/excel

        Toast.makeText(MainActivity.this, txtName, Toast.LENGTH_SHORT).show();

        //创建流文件写入
        File xlsFile = new File(txtName);
        try {
            if (!xlsFile.exists()) {
                xlsFile.createNewFile();
            }
            OutputStream os = new FileOutputStream(xlsFile);
            mWorkbook.write(os);// 或者以流的形式写入文件 mWorkbook.write(new FileOutputStream(xlsFile));
            mWorkbook.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private  void createPart1(HSSFSheet mSheet, information user){
        HSSFRow title = mSheet.createRow(0);
        title.createCell(0).setCellValue("学生14天健康情况登记表");
        CellRangeAddress region = new CellRangeAddress(0, 0, 0, 6);
        mSheet.addMergedRegion(region);

        title = mSheet.createRow(1);
        title.createCell(0).setCellValue("单位名称:");
        title.createCell(2).setCellValue(user.getGrade());

        title.createCell(4).setCellValue("填表日期:");
        region = new CellRangeAddress(1,1,0,1);
        mSheet.addMergedRegion(region);

        title = mSheet.createRow(2);
        title.createCell(0).setCellValue("姓名");
        title.createCell(1).setCellValue(user.getName());

        title.createCell(4).setCellValue("学号");
        title.createCell(5).setCellValue(user.get_id());
        region = new CellRangeAddress(2,2,1,3);
        mSheet.addMergedRegion(region);
        region = new CellRangeAddress(2,2,5,6);
        mSheet.addMergedRegion(region);

        title = mSheet.createRow(3);
        title.createCell(0).setCellValue("目前健康状况");
        title.createCell(1).setCellValue("健康");

        title.createCell(4).setCellValue("手机号");
        title.createCell(5).setCellValue(user.getPhone());

        region = new CellRangeAddress(3,3,1,3);
        mSheet.addMergedRegion(region);
        region = new CellRangeAddress(3,3,5,6);
        mSheet.addMergedRegion(region);

        title = mSheet.createRow(4);
        title.createCell(0).setCellValue("每日体温、健康状况监测(周期14天)");
        region = new CellRangeAddress(4, 4, 0, 6);
        mSheet.addMergedRegion(region);

        //
    }
    //表头
    private void createExcelHead(HSSFSheet mSheet) {
        HSSFRow headRow = mSheet.createRow(5);
        headRow.createCell(0).setCellValue("日期");
        headRow.createCell(1).setCellValue("体温");
        headRow.createCell(2).setCellValue("健康状况");
        headRow.createCell(3).setCellValue("当日所在地");
        headRow.createCell(4).setCellValue("备注");
    }

    /**
     *
     * @param time
     * @param tem
     * @param stat
     * @param locat
     * @param beizhu
     * 进行单元格的数据创建
     */
    private void createCell(String time, String tem, String stat, String locat, String beizhu,HSSFSheet sheet) {
        HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);

        dataRow.createCell(0).setCellValue(time);
        dataRow.createCell(1).setCellValue(tem);
        dataRow.createCell(2).setCellValue(stat);
        dataRow.createCell(3).setCellValue(locat);
        dataRow.createCell(4).setCellValue(beizhu);
    }




}
原文地址:https://www.cnblogs.com/cdl-sunshine/p/14905728.html