使用RecyclerView和SQLite数据库的一个待办事项展示app。
数据库执行类
包括创建数据库以及创建数据库中使用的方法
package com.example.dailyjob_sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DataHelper extends SQLiteOpenHelper {
public DataHelper(@Nullable Context context) {
super(context,"jobList.db", null, 1);//定义数据库名称,版本号等信息
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建数据库,表名为"job"
db.execSQL("CREATE TABLE job(id INTEGER PRIMARY KEY,title TEXT,description TEXT,month INTEGER,day INTEGER)");
//其中done为boolean类型,但是SQLite 没有直接的布尔类型,通常使用 INTEGER 类型来表示布尔值(0 表示 false,1 表示 true)。
//所以在创建表时应将 done 列的类型改为 INTEGER
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Boolean insertData(JobModel jobModel)
{
ContentValues cv=new ContentValues();
cv.put("title",jobModel.getTitle());
cv.put("description",jobModel.getDescription());
cv.put("month",jobModel.getMonth());
cv.put("day",jobModel.getDay());
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
long insert=sqLiteDatabase.insert("job",null,cv);//insert方法返回的是一个long值
//the row ID of the newly inserted row, or -1 if an error occurred
sqLiteDatabase.close();
if (insert==-1)
{
return false;
}
else
{
return true;
}
}
public Boolean deleteData(String title)
{
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
int delete=sqLiteDatabase.delete("job","title"+"=?",new String[]{String.valueOf(title)});
sqLiteDatabase.close();
if(delete==1)
{
return true;
}
else
{
return false;
}
}
public Boolean editData(JobModel jobModel) {
//编辑数据
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("title", jobModel.getTitle());
cv.put("description", jobModel.getDescription());
cv.put("month", jobModel.getMonth());
cv.put("day", jobModel.getDay());
// 确认数据库中是否存在该记录
Cursor cursor = sqLiteDatabase.query("job", null, "id=?", new String[]{String.valueOf(jobModel.getId())}, null, null, null);
if (cursor.getCount() == 0) {
cursor.close();
sqLiteDatabase.close();
return false;
}
cursor.close();
// 执行更新操作
int update = sqLiteDatabase.update("job", cv, "id=?", new String[]{String.valueOf(jobModel.getId())});
sqLiteDatabase.close(); // 关闭数据库连接
// 在 DataHelper 的 editData 方法中添加日志
if (update == 0) {
// 如果更新操作影响的行数为0,返回false并打印日志
System.out.println("Update failed for ID " + jobModel.getId());
return false;
} else {
return true;
}
}
public Cursor getData()
{
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
Cursor cursor=sqLiteDatabase.rawQuery("SELECT * FROM job",null);
return cursor;
}
}
任务信息类
包括任务的基本信息,以及对每个信息的编辑方法
package com.example.dailyjob_sqlite;
public class JobModel {
private Integer id;
private String title;
private String description;
private Integer month;
private Integer day;
public JobModel(Integer id, String title, String description, Integer month, Integer day) {
this.id = id;
this.title = title;
this.description = description;
this.month = month;
this.day = day;
}
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public Integer getMonth() {
return month;
}
public Integer getDay() {
return day;
}
public void setId(Integer id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setMonth(Integer month) {
this.month = month;
}
public void setDay(Integer day) {
this.day = day;
}
}
首页
<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/view_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="252dp"
android:text="浏览日程"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/addjob_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="432dp"
android:text="添加日程"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.dailyjob_sqlite;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addBtn,viewBtn;
addBtn=findViewById(R.id.addjob_button);
viewBtn=findViewById(R.id.view_btn);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, addJob.class);
startActivity(intent);
}
});
viewBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, JobList.class);
startActivity(intent);
}
});
}
}
没有特别的内容,都是点击按钮之后跳转到相应的界面,再执行后续的操作。
添加任务
使用了DatePicker去选择日期
<?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"
tools:context=".addJob">
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="321dp"
android:layout_height="574dp"
android:foregroundGravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.227">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="标题:"
android:textSize="24sp" />
<EditText
android:id="@+id/addtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入标题"
android:inputType="text"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="内容:"
android:textSize="24sp" />
<EditText
android:id="@+id/adddescription_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:ems="10"
android:hint="请输入内容"
android:inputType="text"
android:textSize="24sp" />
</LinearLayout>
<DatePicker
android:id="@+id/datePicker_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.122" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/add_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:text="确定"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
此页功能完整代码
package com.example.dailyjob_sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
public class addJob extends AppCompatActivity {
EditText addTitle,addDescription;
Button button;
DatePicker datePicker_add;
DataHelper dataHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_job);
addTitle=findViewById(R.id.addtitle_text);
addDescription=findViewById(R.id.adddescription_text);
datePicker_add=findViewById(R.id.datePicker_add);
button=findViewById(R.id.add_btn);
DateChange();
ClickBtn();
}
private void DateChange()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
datePicker_add.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(addJob.this,"日期设置为"+(monthOfYear+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
}
});
}
}
private void ClickBtn()
{
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title=addTitle.getText().toString();
String description=addDescription.getText().toString();
int month=datePicker_add.getMonth()+1;
int day=datePicker_add.getDayOfMonth();
JobModel jobModel=new JobModel(-1,title,description,month,day);
dataHelper=new DataHelper(addJob.this);
dataHelper.insertData(jobModel);
Toast.makeText(addJob.this,"添加成功:"+title,Toast.LENGTH_SHORT).show();
Intent intent=new Intent(addJob.this,MainActivity.class);
startActivity(intent);
}
});
}
}
注意其中日期选择器监听器的使用
附上官方文档中DatePicker.OnDateChangedListener的使用方法
另外,月份一般是从0开始的,所以最后输出的时候往往要提前加一。(我也不知道什么时候需要加一,所以需要经常测试)
浏览日程
我使用RecyclerView去展示数据库中的信息,所以需要去编辑RecyclerView中的item
<?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="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/module"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="30dp"
android:orientation="vertical">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:textSize="40sp"
android:textStyle="bold" />
<TextView
android:id="@+id/description_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="description"
android:textSize="34sp"
android:textStyle="italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/month_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="month"
android:textSize="20sp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="月"
android:textSize="20sp" />
<TextView
android:id="@+id/day_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="day"
android:textSize="20sp" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="日"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
之后给item创建适配器类,配置方法
package com.example.dailyjob_sqlite;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context context;
private ArrayList<JobModel> jobModel;
private ClickListener listener;//是ClickListener接口的一个引用,用来存储传递进来的具体实现。
public interface ClickListener//是一个接口,定义了一个方法 moduleClick(JobModel job)。这个方法会在 RecyclerView 的某个项被点击时调用。
{
void moduleClick(JobModel jobModel);
}
public void setClickListener(ClickListener listener)//这是一个公共方法,用来设置 listener 对象。这允许外部类(比如 JobList)提供具体的实现
{
this.listener=listener;
}
public RecyclerAdapter(Context context,ArrayList<JobModel> jobModel) {
this.context=context;
this.jobModel=jobModel;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(this.context).inflate(R.layout.item,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) {
holder.title.setText(jobModel.get(position).getTitle());
holder.description.setText(jobModel.get(position).getDescription());
holder.month.setText(String.valueOf(jobModel.get(position).getMonth()));
holder.day.setText(String.valueOf(jobModel.get(position).getDay()));
}
@Override
public int getItemCount() {
return this.jobModel.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title,description,month,day;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title_text);
description=itemView.findViewById(R.id.description_text);
month=itemView.findViewById(R.id.month_text);
day=itemView.findViewById(R.id.day_text);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION)//检查 position 是否有效
{
//调用 listener.moduleClick 方法,并传递当前项的 JobModel 对象。
listener.moduleClick(jobModel.get(position));
}
}
});
}
}
}
至于任务列表界面,我只放上了一个RecyclerView
<?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"
tools:context=".JobList">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
接下来是列表的功能
package com.example.dailyjob_sqlite;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
public class JobList extends AppCompatActivity {
RecyclerView recyclerView;
public ArrayList<JobModel> jobModel=new ArrayList<>();
DataHelper db;
RecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_list);
db=new DataHelper(JobList.this);
recyclerView=findViewById(R.id.RecyclerView);
adapter=new RecyclerAdapter(JobList.this,jobModel);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
display();
adapter.setClickListener(new RecyclerAdapter.ClickListener() {
@Override
public void moduleClick(JobModel jobModel) {
AlertDialog.Builder dialog=new AlertDialog.Builder(JobList.this);
dialog.setTitle("执行的操作");
dialog.setNegativeButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String title=jobModel.getTitle();
Boolean delete=db.deleteData(title);
if (delete==true)
{
Toast.makeText(JobList.this,"已删除",Toast.LENGTH_SHORT).show();
finish();
}
else
{
Toast.makeText(JobList.this,"删除失败",Toast.LENGTH_SHORT).show();
}
}
});
dialog.setPositiveButton("编辑", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(JobList.this, EditJob.class);
String title=jobModel.getTitle();
String description= jobModel.getDescription();
int month=jobModel.getMonth();
int day=jobModel.getDay();
int id=jobModel.getId();
intent.putExtra("id",id);
intent.putExtra("title",title);
intent.putExtra("description",description);
intent.putExtra("month",month-1);
intent.putExtra("day",day);
startActivity(intent);
}
});
dialog.create();
dialog.show();
}
});
}
private void display()
{
Cursor cursor= db.getData();
if(cursor.getCount()==0)
{
Toast.makeText(JobList.this,"无数据",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(JobList.this,MainActivity.class);
startActivity(intent);
}
else
{
while (cursor.moveToNext())
{
//job表的顺序:id title description month day done
int id=cursor.getInt(0);
String title=cursor.getString(1);//获取 cursor 中第二列的值(索引从 0 开始计数)。
String description=cursor.getString(2);//获取 cursor 中第三列的值。
Integer month=cursor.getInt(3);
Integer day=cursor.getInt(4);
jobModel.add(new JobModel(id,title,description,month,day));
}
}
}
}
点击了某一组数据后,会有一个弹窗,根据弹窗的指示可以对数据进行删除和编辑操作。
编辑任务
在弹窗中点击编辑,会跳转到编辑页面。
<?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"
tools:context=".EditJob">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="标题:"
android:textSize="24sp" />
<EditText
android:id="@+id/edittitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入标题"
android:inputType="text"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="内容:"
android:textSize="24sp" />
<EditText
android:id="@+id/editdescription_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="请输入内容"
android:inputType="text"
android:textSize="24sp" />
</LinearLayout>
<DatePicker
android:id="@+id/dataPicker_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/edit_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="28dp"
android:text="确定"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
功能
package com.example.dailyjob_sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.Calendar;
public class EditJob extends AppCompatActivity {
EditText editTitle, editDescription;
DatePicker datePicker_edit;
Button button;
Calendar calendar = Calendar.getInstance();
DataHelper dataHelper;
JobModel jobModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_job);
datePicker_edit = findViewById(R.id.dataPicker_edit);
Intent intent = getIntent();
String title = intent.getStringExtra("title");
String description = intent.getStringExtra("description");
int year = calendar.get(Calendar.YEAR);
int month = intent.getIntExtra("month", 0);//获取int类型的值需要添加默认值
int day = intent.getIntExtra("day", 0);
int id = intent.getIntExtra("id",-1);
int newMonth=datePicker_edit.getMonth();
int newDay=datePicker_edit.getDayOfMonth()+1;
changeDate(year, month, day);
jobModel=new JobModel(id,title,description,newMonth,newDay);
editTitle = findViewById(R.id.edittitle_text);
editDescription = findViewById(R.id.editdescription_text);
editTitle.setText(title);
editDescription.setText(description);
ClickBtn(id);
}
private void changeDate(int year, int month, int day) {
dataHelper=new DataHelper(this);
datePicker_edit.init(year, month, day, new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(EditJob.this,"日期设置为"+(monthOfYear+1)+"月"+dayOfMonth+"日",Toast.LENGTH_SHORT).show();
}
});
}
private void ClickBtn(int id)
{
button = findViewById(R.id.edit_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newTitle = editTitle.getText().toString();
String newDes = editDescription.getText().toString();
int newMonth = datePicker_edit.getMonth() + 1;
int newDay = datePicker_edit.getDayOfMonth();
jobModel.setTitle(newTitle);
jobModel.setDescription(newDes);
jobModel.setMonth(newMonth);
jobModel.setDay(newDay);
if (dataHelper.editData(jobModel)) {
Toast.makeText(EditJob.this, "修改成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(EditJob.this, "修改失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}
END
安卓自学笔记。
瑕疵很多,敬请指导。