简述
实现功能:
1:先将数据添加到数据库中(SQLite)
2: 使用RecyclerView将数据库中的数据展示出来
//备注:带加号的ImageButton用于添加数据,带勾的ImageButton用于获取数据并展示
//先按带加号的在按带勾的
实体类
public class Event {
private String Title;
private String Time;
private double Money;
public Event(){
}
public Event(String Title, String Time, double Money)
{
this.Title = Title;
this.Time = Time;
this.Money = Money;
}
public String getTitle() {
return Title;
}
public String getTime() {
return Time;
}
public double getMoney() {
return Money;
}
public void setTitle(String Title) {
this.Title = Title;
}
public void setTime(String Time) {
this.Time = Time;
}
public void setMoney(double Money) {
this.Money = Money;
}
}
数据库
建表
public class MyHelper extends SQLiteOpenHelper {
public static final String DataBaseName = "Bookkeeping.db";
public static final SQLiteDatabase.CursorFactory factory = null;
public static final int version = 1;
public static final String Title = "Title";
public static final String Time = "Time";
public static final String Money = "Money";
public static final String TableName = "BookkeepingTable";
public MyHelper(@Nullable Context context) {
super(context, DataBaseName, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table "+ TableName +" ( "+Title+" varchar(20) primary key, "+Time+" varchar(20), "+Money+" Double);";
// String sql = "create table BookkeepingTable ("
// + "Title text primary key, "
// + "Time text, "
// + "Money double)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+TableName);
onCreate(db);
}
}
Dao类
public class Dao {
public static final String TAG = "Bookkeeping";
private SQLiteDatabase DB;
private MyHelper helper;
public Dao(Context context){
helper = new MyHelper(context);
}
public void Insert(Event event){
DB = helper.getReadableDatabase();
if (DB.isOpen())
{
ContentValues values = new ContentValues();
values.put(MyHelper.Title,event.getTitle());
values.put(MyHelper.Time,event.getTime());
values.put(MyHelper.Money,event.getMoney());
long RowId = DB.insert(MyHelper.TableName,null,values);
if(RowId == -1)
Log.i(TAG, "数据插入失败!");
else
Log.i(TAG, "数据插入成功!"+RowId);
DB.close();
}
}
}
插入数据
效果图
布局代码
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="20sp"
android:singleLine="true"
android:hint="主题:"/>
<TextView
android:id="@+id/Time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:textSize="20sp"
android:singleLine="true"
android:hint="时间:"/>
<TextView
android:id="@+id/Money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="300dp"
android:textSize="20sp"
android:singleLine="true"
android:hint="金钱:"/>
</RelativeLayout>
适配器
public class MyRecyclerView extends RecyclerView.Adapter<MyRecyclerView.ViewHolder> {
private List<Event> EventList;
public MyRecyclerView(List<Event> EventList)
{
this.EventList = EventList;
}
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false);
return new ViewHolder(view);
}
//给子控件赋值
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Event event = EventList.get(position);
holder.Title.setText(event.getTitle());
holder.Time.setText(event.getTime());
holder.Money.setText(event.getMoney()+"");
}
@Override
public int getItemCount() {
return EventList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView Title,Money,Time;
public ViewHolder(@NonNull View itemView) {
super(itemView);
Title = (TextView) itemView.findViewById(R.id.Title);
Time = (TextView) itemView.findViewById(R.id.Time);
Money = (TextView) itemView.findViewById(R.id.Money);
}
}
}
获取数据
public class AddThing extends AppCompatActivity implements DatePicker.OnDateChangedListener{
private RadioGroup Group;
private RadioButton Pay,InCome;
private DatePicker datePicker;
private TextView TipsTitle,TipsTime,TipsMoney,TipsMoneyType;
private EditText editTitle,editTime,editMoney;
private Button btn_Submit;
public static int MoneyType = 0;
public static String DatePickerTime = null;
public static String Title = null;
public static String Time = null;
public static double Money = 0;
Dao dao = null;
Event event = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_thing);
btn_Submit = (Button) findViewById(R.id.submit);
datePicker = (DatePicker) findViewById(R.id.addThing);
Group = (RadioGroup) findViewById(R.id.Group);
Pay = (RadioButton) findViewById(R.id.Pay);
InCome = (RadioButton) findViewById(R.id.InCome);
TipsMoneyType = (TextView) findViewById(R.id.TipsMoneyType);
TipsTitle = (TextView) findViewById(R.id.TipsTitle);
TipsTime = (TextView) findViewById(R.id.TipsTime);
TipsMoney = (TextView) findViewById(R.id.TipsMoney);
editTitle = (EditText) findViewById(R.id.editTitle);
editTime = (EditText) findViewById(R.id.editTime);
editMoney = (EditText) findViewById(R.id.editMoney);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int monthOfYear = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
datePicker.init(year, monthOfYear, dayOfMonth, this);
Group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
if (radioButton.getText().equals("支付")) {
MoneyType = 1;
} else {
MoneyType = 0;
}
}
});
btn_Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dao = new Dao(AddThing.this);//创建数据库和表
Title = editTitle.getText().toString().trim();
Time = editTime.getText().toString();
try {
Money = Double.valueOf(editMoney.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
if ("".equals(Title))
{
Toast.makeText(AddThing.this,"不能为空",Toast.LENGTH_SHORT).show();
return;
}
dao.Insert(new Event(Title,Time,Money));
Log.d(Dao.TAG,"succees!");
}
});
}
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
editTime.setText(year + "-"+ monthOfYear+ "-"+ dayOfMonth+ "");
}
}
效果图
布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/TipsMoneyType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="请选择金钱类型:"/>
<RadioGroup
android:id="@+id/Group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp">
<RadioButton
android:id="@+id/Pay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="支付"/>
<RadioButton
android:id="@+id/InCome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="收入"/>
</RadioGroup>
</LinearLayout>
<TextView
android:id="@+id/TipsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入主题:"
android:layout_marginTop="120dp"/>
<EditText
android:id="@+id/editTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please input title:"
android:layout_marginLeft="90dp"
android:layout_marginTop="105dp"/>
<TextView
android:id="@+id/TipsTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择时间:"
android:layout_below="@id/TipsTitle"
android:layout_marginTop="50dp"/>
<EditText
android:id="@+id/editTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please choose time:"
android:layout_below="@id/editTitle"
android:layout_marginTop="23dp"
android:layout_marginLeft="80dp"/>
<DatePicker
android:id="@+id/addThing"
android:layout_width="match_parent"
android:layout_height="100dp"
android:calendarViewShown="false"
android:spinnersShown="true"
android:datePickerMode="spinner"
android:headerBackground="#ffffff"
android:layout_below="@id/editTime"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/TipsMoney"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入金额:"
android:layout_marginTop="20dp"
android:layout_below="@id/addThing"/>
<EditText
android:id="@+id/editMoney"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please input money:"
android:layout_below="@id/addThing"
android:layout_marginLeft="80dp"
android:layout_marginTop="3dp"/>
<Button
android:id="@+id/submit"
android:layout_height="wrap_content"
android:layout_width="100dp"
android:text="Submit"
android:layout_below="@id/editMoney"
android:textAllCaps="false"
android:layout_marginTop="80dp"
android:gravity="center"
android:layout_marginLeft="150dp"/>
</RelativeLayout>
显示数据
MainActivity.java中的代码如下:
public class MainActivity extends AppCompatActivity {
private RecyclerView Recyclerview;
private ImageButton imageButton,imageButtonGet;
private MyRecyclerView adapter;
private List<Event> EventList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Recyclerview = (RecyclerView) findViewById(R.id.EventDisplayInterface);
imageButton = (ImageButton) findViewById(R.id.AddButton);
imageButtonGet = (ImageButton) findViewById(R.id.getInformation);
//绑定适配器
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
Recyclerview.setLayoutManager(manager);
adapter = new MyRecyclerView(EventList);
Recyclerview.setAdapter(adapter);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AddThing.class);
startActivity(intent);
}
});
imageButtonGet.setOnClickListener(new View.OnClickListener() {
Event event = new Event();
@Override
public void onClick(View v) {
String Title = AddThing.Title;
String Time = AddThing.Time;
double Money = AddThing.Money;
if ( ("".equals(Title)) && ( "".equals(Time)))
{
Toast.makeText(MainActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
}
else {
event = new Event(Title, Time, Money);
EventList.add(event);
adapter.notifyItemChanged(EventList.size() - 1);
Recyclerview.scrollToPosition(EventList.size() - 1);
Toast.makeText(MainActivity.this,"111111111",Toast.LENGTH_SHORT).show();
}
}
});
}
}
适配器效果图
布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/EventDisplayInterface"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageButton
android:id="@+id/AddButton"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/add"
android:scaleType="centerInside"
android:layout_marginTop="480dp"
android:layout_marginLeft="20dp"
android:background="#00ff"/>
<ImageButton
android:id="@+id/getInformation"
android:layout_width="100dp"
android:layout_height="80dp"
android:src="@drawable/get"
android:scaleType="centerInside"
android:layout_marginTop="490dp"
android:layout_marginLeft="250dp"
android:background="#00ff"/>
</RelativeLayout>
最终效果图