1.新建project
file->new->new project,选择Basic Activity
2.在content_main.xml中添加一个List View
1 <ListView 2 android:id="@+id/lv_main" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" />
3.绘制一个页面来展示数据(list_item.xml)
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="80dp" 5 android:orientation="vertical"> 6 7 <TextView 8 android:id="@+id/tv_name" 9 android:layout_width="150dp" 10 android:layout_height="80dp" 11 android:layout_marginLeft="10dp" 12 android:layout_alignParentLeft="true" 13 android:gravity="center" 14 android:singleLine="true" 15 android:textSize="35sp" 16 android:text="姓名" 17 android:ellipsize="marquee" /> 18 19 <TextView 20 android:id="@+id/tv_date" 21 android:layout_width="wrap_content" 22 android:layout_height="80dp" 23 android:layout_toRightOf="@+id/tv_name" 24 android:layout_marginLeft="15dp" 25 android:textSize="20sp" 26 android:gravity="center" 27 android:text="日期"/> 28 29 <TextView 30 android:id="@+id/tv_bodyTem" 31 android:layout_width="wrap_content" 32 android:layout_height="80dp" 33 android:text="体温" 34 android:textSize="30sp" 35 android:layout_marginRight="20dp" 36 android:layout_alignParentRight="true" 37 android:gravity="center"/> 38 39 40 </RelativeLayout>
4.Bean存储
1 package com.example.tem; 2 3 import java.io.Serializable; 4 5 public class TemBean implements Serializable{ 6 7 public String name; 8 public String date; 9 public String bodyTem; 10 11 @Override 12 public String toString() { 13 return "TemBean{" + 14 "name='" + name + '\'' + 15 ", date='" + date + '\'' + 16 ", bodyTem='" + bodyTem + '\'' + 17 '}'; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 public String getDate() { 29 return date; 30 } 31 32 public void setDate(String date) { 33 this.date = date; 34 } 35 36 public String getBodyTem() { 37 return bodyTem; 38 } 39 40 public void setBodyTem(String bodyTem) { 41 this.bodyTem = bodyTem; 42 } 43 44 }
5.编写适配器(什么是适配器“https://blog.csdn.net/geekmubai/article/details/89297238”)
1 package com.example.tem; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.TextView; 9 10 import java.util.List; 11 12 public class TemListAdapter extends BaseAdapter{ 13 private List<TemBean> mlist; 14 private Context mContext; 15 private LayoutInflater mlayoutInflater; 16 public TemListAdapter(Context context, List<TemBean> list){ 17 mContext=context; 18 mlist=list; 19 mlayoutInflater=LayoutInflater.from(context); 20 } 21 @Override 22 public int getCount() { 23 return mlist.size(); 24 } 25 26 @Override 27 public Object getItem(int position) { 28 return mlist.get(position); 29 } 30 31 @Override 32 public long getItemId(int position) { 33 return position; 34 } 35 36 @Override 37 public View getView(int position, View convertView, ViewGroup parent) { 38 ViewHolder viewHolder; 39 if(convertView==null) 40 { 41 viewHolder=new ViewHolder(); 42 convertView=mlayoutInflater.inflate(R.layout.list_item,null); 43 viewHolder.mTvName=convertView.findViewById(R.id.tv_name); 44 viewHolder.mTvDate=convertView.findViewById(R.id.tv_date); 45 viewHolder.mTvBodyTem=convertView.findViewById(R.id.tv_bodyTem); 46 convertView.setTag(viewHolder); 47 }else{ 48 viewHolder=(ViewHolder) convertView.getTag(); 49 } 50 TemBean bean=mlist.get(position); 51 viewHolder.mTvName.setText(bean.name); 52 viewHolder.mTvDate.setText(bean.date); 53 viewHolder.mTvBodyTem.setText(bean.bodyTem); 54 return convertView; 55 } 56 57 private static class ViewHolder{ 58 public TextView mTvName; 59 public TextView mTvDate; 60 public TextView mTvBodyTem; 61 } 62 }
6.建立数据库
1 package com.example.tem; 2 3 import android.content.ContentValues; 4 import android.content.Context; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.database.sqlite.SQLiteOpenHelper; 8 9 import androidx.annotation.Nullable; 10 11 public class DatabaseHelper extends SQLiteOpenHelper { 12 public static final String NAME = "name"; 13 public static final String DATE = "date"; 14 public static final String BODYTEM = "bodytem"; 15 public static final String TemPost = "tempost";//数据表名 16 17 public DatabaseHelper(@Nullable Context context) { 18 super(context, "temperature", null, 1);//数据库名 19 } 20 21 @Override 22 public void onCreate(SQLiteDatabase db) { 23 db.execSQL("create table if not exists TemPost("+ 24 "id integer primary key, "+ 25 "name varchar, "+ 26 "date varchar, "+ 27 "bodytem varchar)"); 28 } 29 public void insertTem(TemBean TemBean){//增 30 SQLiteDatabase database=getWritableDatabase(); 31 ContentValues cv=new ContentValues(); 32 cv.put(NAME,TemBean.name); 33 cv.put(DATE,TemBean.date); 34 cv.put(BODYTEM,TemBean.bodyTem); 35 database.insert(TemPost,null,cv); 36 } 37 public Cursor getAllTemData(){//查 38 SQLiteDatabase database=getWritableDatabase(); 39 return database.query("TemPost",null,null,null,null,null,"DATE"+" ASC"); 40 } 41 public void deleteAllData(){//删 42 SQLiteDatabase database=getWritableDatabase(); 43 database.delete("TemPost",null,null); 44 } 45 46 @Override 47 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 48 } 49 50 }
7.使用fab图标的click弹出填写界面,先做界面(new_tem_data.xml)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:gravity="center"> 7 8 <EditText 9 android:id="@+id/et_tem_name" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:layout_margin="4dp" 13 android:hint="姓名"/> 14 15 <EditText 16 android:id="@+id/et_tem_bodytem" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:layout_margin="4dp" 20 android:hint="体温"/> 21 <DatePicker 22 android:id="@+id/dp_tem_date" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_margin="4dp" 26 android:datePickerMode="spinner" 27 android:calendarViewShown="false"/> 28 29 </LinearLayout>
8.更改MainActivity中的onClick方法
1 package com.example.tem; 2 3 import android.content.DialogInterface; 4 import android.content.Intent; 5 import android.database.Cursor; 6 import android.os.Bundle; 7 8 import com.google.android.material.floatingactionbutton.FloatingActionButton; 9 import com.google.android.material.snackbar.Snackbar; 10 11 import androidx.appcompat.app.AlertDialog; 12 import androidx.appcompat.app.AppCompatActivity; 13 import androidx.appcompat.widget.Toolbar; 14 15 import android.os.Parcelable; 16 import android.view.LayoutInflater; 17 import android.view.View; 18 import android.view.Menu; 19 import android.view.MenuItem; 20 import android.widget.DatePicker; 21 import android.widget.EditText; 22 import android.widget.ListView; 23 24 import java.io.Serializable; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 public class MainActivity extends AppCompatActivity { 29 private List<TemBean> mTemBeanList; 30 private DatabaseHelper mdatabaseHelper; 31 private TemListAdapter mAdapter; 32 33 @Override 34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.activity_main); 37 Toolbar toolbar = findViewById(R.id.toolbar); 38 setSupportActionBar(toolbar); 39 40 41 mdatabaseHelper=new DatabaseHelper(this); 42 mTemBeanList=new ArrayList<>(); 43 ListView temList=(ListView)findViewById(R.id.lv_main); 44 initTemData(); 45 mAdapter = new TemListAdapter(this,mTemBeanList); 46 temList.setAdapter(mAdapter); 47 48 FloatingActionButton fab = findViewById(R.id.fab); 49 fab.setOnClickListener(new View.OnClickListener() { 50 @Override 51 public void onClick(View view) { 52 AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); 53 LayoutInflater inflater=LayoutInflater.from(MainActivity.this); 54 View viewDialog=inflater.inflate(R.layout.new_tem_data,null); 55 final EditText name=(EditText) viewDialog.findViewById(R.id.et_tem_name); 56 final EditText bodytem=(EditText)viewDialog.findViewById(R.id.et_tem_bodytem); 57 final DatePicker date=(DatePicker) viewDialog.findViewById(R.id.dp_tem_date); 58 builder.setView(viewDialog); 59 builder.setTitle("填写体温"); 60 builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 61 @Override 62 public void onClick(DialogInterface dialog, int which) { 63 TemBean temBean=new TemBean(); 64 temBean.name=name.getText().toString(); 65 temBean.bodyTem=bodytem.getText().toString(); 66 temBean.date=date.getYear()+"-"+(date.getMonth()+1)+"-"+date.getDayOfMonth(); 67 mdatabaseHelper.insertTem(temBean); 68 mTemBeanList.add(temBean); 69 mAdapter.notifyDataSetChanged(); 70 } 71 }); 72 builder.setNegativeButton("Cancel",null);//点击取消 73 builder.create().show();//显示dialog的布局 74 } 75 }); 76 } 77 78 private void initTemData() { 79 Cursor cursor=mdatabaseHelper.getAllTemData(); 80 if(cursor!=null){ 81 while(cursor.moveToNext()){ 82 TemBean temBean1=new TemBean(); 83 temBean1.name=cursor.getString(cursor.getColumnIndex("name")); 84 temBean1.date=cursor.getString(cursor.getColumnIndex("date")); 85 temBean1.bodyTem=cursor.getString(cursor.getColumnIndex("bodytem")); 86 mTemBeanList.add(temBean1); 87 } 88 cursor.close(); 89 } 90 } 91 92 @Override 93 public boolean onCreateOptionsMenu(Menu menu) { 94 // Inflate the menu; this adds items to the action bar if it is present. 95 getMenuInflater().inflate(R.menu.menu_main, menu); 96 return true; 97 } 98 99 @Override 100 public boolean onOptionsItemSelected(MenuItem item) { 101 // Handle action bar item clicks here. The action bar will 102 // automatically handle clicks on the Home/Up button, so long 103 // as you specify a parent activity in AndroidManifest.xml. 104 int id = item.getItemId(); 105 106 //noinspection SimplifiableIfStatement 107 if (id == R.id.action_settings) { 108 return true; 109 } 110 111 return super.onOptionsItemSelected(item); 112 } 113 }
完成
效果图: