体温上报APP1.2
1、学习进度表:
2、今日任务:完成第一阶段
体温上报:自动获取定位和上报时间
writeActivity
package com.example.reporttemperature; import androidx.appcompat.app.AppCompatActivity; import android.database.Cursor; import android.os.Bundle; import android.content.*; import android.database.sqlite.SQLiteDatabase; import android.text.TextUtils; import android.view.*; import android.widget.*; import java.util.Arrays; import java.util.Calendar; import android.location.Address; import java.util.List; import android.location.Geocoder; import java.util.Locale; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationClientOption.AMapLocationMode; import com.amap.api.location.AMapLocationListener; public class WriteActivity extends AppCompatActivity { private Button button_report; private EditText ed_date; private EditText ed_address; private EditText ed_name; private EditText ed_tem; private EditText ed_reason; private MyOpenHelper helper; private AMapLocationClient mLocationClient = null; private AMapLocationListener mLocationListener = null; private String ID_number; private RadioButton r1,r2,r3,r4,r5; private String special_situation,classroom; private RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_write); helper = new MyOpenHelper(this); findById();//获取控件 setListener(); ed_date.setText(getTime());//获取时间 //获取地址 get_address(); Intent get=getIntent(); ID_number=get.getStringExtra("IDnumber"); //从登录页面获取学号数据 String []a=search_user(ID_number); classroom=a[1]; ed_name.setText(a[0]);//填充姓名 ed_tem.setText("36.2");//填充体温 button_report.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取edittext数据 String text1 =ed_date .getText().toString().trim(); String text2 = ed_name.getText().toString().trim(); String text3 = ed_tem.getText().toString().trim(); String text4 = ed_address.getText().toString().trim(); String text5 = special_situation; String text6 = ed_reason.getText().toString().trim(); if (TextUtils.isEmpty(text1) ) { Toast.makeText(WriteActivity.this, "输入不能为空", Toast.LENGTH_LONG).show(); } else { add_tem(classroom,text1,text2,text3,text4,text5,text6); Toast.makeText(WriteActivity.this, "提交成功!", Toast.LENGTH_SHORT).show(); } Intent intent = new Intent(WriteActivity.this, CatalogActivity.class); intent.putExtra("IDnumber",ID_number); //finish();//结束当前活动 startActivity(intent); } }); } //获取控件 private void findById(){ ed_name=findViewById(R.id.editText_write_name); ed_date=findViewById(R.id.editDate); ed_address=findViewById(R.id.editAddress); ed_reason=findViewById(R.id.editText3); ed_tem=findViewById(R.id.editTemperature); button_report = (Button) findViewById(R.id.button_weite_report); radioGroup=findViewById(R.id.radioGroup); r1=findViewById(R.id.radioButton1); r2=findViewById(R.id.radioButton2); r3=findViewById(R.id.radioButton3); r4=findViewById(R.id.radioButton4); r5=findViewById(R.id.radioButton5); } //设置控件RadioGroup的监听器 private void setListener() { // TODO Auto-generated method stub //设置所有RadioGroup的状态改变监听器 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(checkedId==r1.getId()){ special_situation=r1.getText().toString(); }else if(checkedId==r2.getId()){ special_situation=r2.getText().toString(); }else if(checkedId==r3.getId()){ special_situation=r3.getText().toString(); }else if(checkedId==r4.getId()){ special_situation=r4.getText().toString(); }else if(checkedId==r5.getId()){ special_situation=r5.getText().toString(); } } }); } //地址显示在横线上 private void get_address(){ //声明AMapLocationClient类对象 //初始化定位 mLocationClient = new AMapLocationClient(getApplicationContext()); //启动定位 mLocationClient.startLocation(); mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation location) { if (location != null) { if (location.getErrorCode() == 0) { //解析定位结果 String result=getAddress(location); ed_address.setText(result); } } } }; //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); //异步获取定位结果 initLocation(); } private void initLocation() { AMapLocationClientOption option = new AMapLocationClientOption(); option.setGpsFirst(true); // 打开gps; //可选,是否需要地址信息,默认为不需要,即参数为false //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。 option.setLocationMode(AMapLocationMode.Hight_Accuracy); //可选,设置是否需要最新版本的地址信息。默认需要,即参数为true mLocationClient.setLocationOption(option); //mLocationClient为第二步初始化过的LocationClient对象 //需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用 //更多LocationClientOption的配置,请参照类参考中LocationClientOption类的详细说明 } private void close() { mLocationClient.stopLocation(); mLocationClient.unRegisterLocationListener(mLocationListener); mLocationClient.onDestroy(); } //解码地址 private String getAddress(AMapLocation location) { String result=""; try { if (location != null) { Geocoder gc = new Geocoder(this, Locale.getDefault()); List<Address>addresses = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1); Address obj=addresses.get(0); result=obj.getAddressLine(0); } } catch (Exception e) { e.printStackTrace(); } return result; } //获取时间 public String getTime(){ //获取年月日时分 Calendar calendar1 = Calendar.getInstance(); //年 int year = calendar1.get(Calendar.YEAR); //月 int month = calendar1.get(Calendar.MONTH) + 1; //日 int day = calendar1.get(Calendar.DAY_OF_MONTH); //获取系统时间 //小时 int hour = calendar1.get(Calendar.HOUR_OF_DAY); //分钟 int minute = calendar1.get(Calendar.MINUTE); String result=year + "年" + month + "月" + day + "日"+hour+"时"+minute+"分"; return result; } //查找用户信息 public String[] search_user(String id){ String[] stringArr={"",""}; List<String>list= Arrays.asList(stringArr); SQLiteDatabase db=helper.getReadableDatabase(); Cursor cursor=db.rawQuery("select * from user",null); while(cursor.moveToNext()){ String id_number=cursor.getString(cursor.getColumnIndex("idNumber")); String user_name=cursor.getString(cursor.getColumnIndex("name")); String user_classroom=cursor.getString(cursor.getColumnIndex("class")); if(id.equals(id_number)) { list.set(0,user_name); list.set(1,user_classroom); break; } } return list.toArray(stringArr); } //增加体温记录 public void add_tem(String classroom,String date, String name,String tem,String address,String special,String reason) { // MyOpenHelper dbHelper = new MyOpenHelper(this); //writableDatabase = dbHelper.getWritableDatabase(); SQLiteDatabase db=helper.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("class", classroom); contentValues.put("date", date); contentValues.put("temperature", tem); contentValues.put("name", name); contentValues.put("address",address); contentValues.put("special",special); contentValues.put("reason",reason); db.insert("temperature", null, contentValues); db.close(); } }
activity_write.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/main_activity" tools:context=".WriteActivity"> <TextView android:id="@+id/textName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="14dp" android:layout_marginLeft="14dp" android:layout_marginTop="16dp" android:text="用户姓名" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editText_write_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textName" /> <TextView android:id="@+id/textTemperature" android:layout_width="70dp" android:layout_height="wrap_content" android:layout_marginStart="14dp" android:layout_marginLeft="14dp" android:layout_marginBottom="10dp" android:text="测量体温" app:layout_constraintBottom_toTopOf="@+id/editTemperature" app:layout_constraintStart_toStartOf="parent" /> <EditText android:id="@+id/editTemperature" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="131dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textDate" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="11dp" android:layout_marginLeft="11dp" android:layout_marginEnd="11dp" android:layout_marginRight="11dp" android:layout_marginBottom="10dp" android:text="测量时间" app:layout_constraintBottom_toTopOf="@+id/editDate" app:layout_constraintEnd_toEndOf="@+id/textAddress" app:layout_constraintStart_toStartOf="@+id/editDate" /> <EditText android:id="@+id/editDate" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="42dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editTemperature" /> <TextView android:id="@+id/textAddress" android:layout_width="70dp" android:layout_height="wrap_content" android:layout_marginStart="11dp" android:layout_marginLeft="11dp" android:layout_marginBottom="9dp" android:text="所在位置" app:layout_constraintBottom_toTopOf="@+id/editAddress" app:layout_constraintStart_toStartOf="parent" /> <EditText android:id="@+id/editAddress" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="38dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editDate" /> <Button android:id="@+id/button_weite_report" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="144dp" android:layout_marginLeft="144dp" android:layout_marginTop="12dp" android:layout_marginEnd="179dp" android:layout_marginRight="179dp" android:layout_marginBottom="70dp" android:text="提交" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/radioGroup" app:layout_constraintVertical_bias="1.0" /> <TextView android:id="@+id/textView_special" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="11dp" android:layout_marginLeft="11dp" android:text="特殊情况" app:layout_constraintBottom_toTopOf="@+id/radioGroup" app:layout_constraintStart_toStartOf="parent" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="29dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editAddress"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="无" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="418dp" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2020年12月27日至今去过或现在居住在中高风险地区" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="461dp" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="本人或家人正在集中隔离" tools:layout_editor_absoluteX="96dp" tools:layout_editor_absoluteY="420dp" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="今日居住地变化" tools:layout_editor_absoluteX="15dp" tools:layout_editor_absoluteY="504dp" /> <RadioButton android:id="@+id/radioButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="其他情况" tools:layout_editor_absoluteX="192dp" tools:layout_editor_absoluteY="504dp" /> <EditText android:id="@+id/editText3" android:layout_width="411dp" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" tools:layout_editor_absoluteY="578dp" /> </RadioGroup> </androidx.constraintlayout.widget.ConstraintLayout>
浏览体温上报信息
ListActivity
package com.example.reporttemperature; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; public class ListActivity extends AppCompatActivity { private MyOpenHelper helper=new MyOpenHelper(this); private Button button_back,button_rewrite; //private TextView showDate,showName,showAddress,showReason,showTem,showSpecial; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); information_show(); button_back=findViewById(R.id.button_list_back); button_rewrite=findViewById(R.id.button_list_write); button_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ListActivity.this, CatalogActivity.class); finish();//结束当前活动 startActivity(intent); } }); button_rewrite.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ListActivity.this, WriteActivity.class); startActivity(intent); } }); } public void information_show(){ SQLiteDatabase db=helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from temperature", null); //String[] stringArr=new String [30]; //List<String> list= Arrays.asList(stringArr); //List<String>list=new ArrayList<>(); String [] list1=new String[100]; int i=0; while (cursor.moveToNext()) { String newDate = cursor.getString(cursor.getColumnIndex("date")); String newName = cursor.getString(cursor.getColumnIndex("name")); String newAddress = cursor.getString(cursor.getColumnIndex("address")); String newReason= cursor.getString(cursor.getColumnIndex("reason")); String newSpecial= cursor.getString(cursor.getColumnIndex("special")); String newTemperature= cursor.getString(cursor.getColumnIndex("temperature")); String str=newDate+"\n"+newName+"\n"+newAddress+"\n"+newTemperature+"\n"+newSpecial+"\n"+newReason; list1[i]=str; ++i; } cursor.close(); String []list2=new String[i]; int k=i; for(int j=0;j<i&&k>=0;j++){ --k; list2[k]=list1[j]; } ListView listView=(ListView)findViewById(R.id.list_view); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list2); listView.setAdapter(adapter); } }
activity_list.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/activity" tools:context=".ListActivity"> <Button android:id="@+id/button_list_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="86dp" android:layout_marginLeft="86dp" android:layout_marginTop="50dp" android:layout_marginEnd="60dp" android:layout_marginRight="60dp" android:layout_marginBottom="42dp" android:text="退出" app:layout_constraintBottom_toTopOf="@+id/list_view" app:layout_constraintEnd_toStartOf="@+id/button_list_write" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button_list_write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="60dp" android:layout_marginLeft="60dp" android:layout_marginTop="52dp" android:layout_marginEnd="89dp" android:layout_marginRight="89dp" android:layout_marginBottom="40dp" android:text="继续填报" app:layout_constraintBottom_toTopOf="@+id/list_view" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/button_list_back" app:layout_constraintTop_toTopOf="parent" /> <ListView android:id="@+id/list_view" android:layout_width="320dp" android:layout_height="580dp" android:layout_marginStart="55dp" android:layout_marginLeft="55dp" android:layout_marginTop="40dp" android:layout_marginEnd="36dp" android:layout_marginRight="36dp" android:layout_marginBottom="11dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button_list_write"> </ListView> </androidx.constraintlayout.widget.ConstraintLayout>