最近在做类似备忘录的项目。需要实现备忘录的增删改查。备忘录的展示方式使用recyclerview,储存方式则使用litepal。
在build.gradle(app)中添加依赖
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'org.litepal.android:java:3.0.0'
先配置数据库方便之后两功能适配
Event便是这个数据库的名字,其中的含有其他属性
import org.litepal.crud.LitePalSupport;
public class Event extends LitePalSupport {
private int id;//序号
private String theme;//主题
private String type;//类型
private String times;//时间
private String content;//内容
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getTheme() {return theme;}
public void setTheme(String theme) {this.theme = theme;}
public String getType() {return type;}
public void setType (String type){ this.type = type;}
public String getTimes() {return times;}
public void setTimes(String times) { this.times = times;}
public String getContent() {return content;}
public void setContent(String content) {this.content = content;}
}
在AndroidManifest.xml文件的中添加代码
android:name="org.litepal.LitePalApplication"
如果没有这一步使用litepal的任何操作都会闪退
这样数据库的基本配置就已经完成了
public class Display {
private String content;
private int id;
public Display(String content) { this.content = content; }
public void setID(int id){ this.id = id ; }
public String getContent() { return content; }
public int getId(){ return id; }
}
作为实例对象
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="180dp"
android:padding="20dp"
>
<TextView
android:id="@+id/di_tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="@drawable/circle_textview"
android:padding="10dp"
/>
</LinearLayout>
这便是一个recyclerview项的布局
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class DisplayAdapter extends RecyclerView.Adapter<DisplayAdapter.DisplayViewHolder> {
private List<Display> mDisplayList;
static class DisplayViewHolder extends RecyclerView.ViewHolder{
TextView textview;
View eventView;
public DisplayViewHolder(View view) {
super(view);
eventView = view ;//整个recycleview单元
textview = view.findViewById(R.id.di_tv);//内部textview部件
}
}
public DisplayAdapter(List<Display> displayList){
mDisplayList = displayList;
}
@Override
public DisplayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.display_item, parent, false);
DisplayViewHolder holder = new DisplayViewHolder(view);
return holder;
}
public void onBindViewHolder(DisplayViewHolder holder, int position) {
Display display= mDisplayList.get(position);//position为list编号。从0开始
holder.textview.setText(display.getContent());
}
@Override
public int getItemCount() {
return mDisplayList.size();
}
}
这样,recyclerview也基本配置好了。
两大杀器皆已准备就绪,那么就需要能驾驭两个杀器的高手了。这里我们准备三个页面,一个用recyclerview展示备忘录的信息;一个新建备忘录;一个更改或删除。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/hp_rv_show"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/new_event2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:background="@drawable/circle_button"/>
<!-- 利用RelativeLayout布局形成悬浮按钮-->
</RelativeLayout>
</LinearLayout>
在布局内放置了一个recyclerview和一个button,button悬浮在recyclerview上。button用于跳转到新建备忘录页面。(关于background是因为这个button我设置了圆角)
外面的LinearLayout布局完全可以删掉,这里是因为方便以后在页面内做调整。
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.litepal.LitePal;
import java.util.ArrayList;
import java.util.List;
public class HomePage extends AppCompatActivity {
private List<Display> displayList = new ArrayList<>();
private RecyclerView displayRecyclerView;
private DisplayAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
ActionBar act = getSupportActionBar();//隐藏顶部标题
act.hide();//隐藏顶部标题
Button newEvent2 = findViewById(R.id.new_event2);
newEvent2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(HomePage.this, SetEvent.class);
startActivity(intent);
}
});//点击跳转到新建事件页面
LitePal.getDatabase();
//创建数据库
displayRecyclerView = (RecyclerView) findViewById(R.id.hp_rv_show);
displayRecyclerView.setLayoutManager(new LinearLayoutManager(this));
displayRecyclerView.setAdapter(new DisplayAdapter(displayList));
//配置recyclerview
initDisplays();//初始化
}
private void initDisplays() {//将litepal中的内容分配到recyclerview中并显示
List<Event> events = LitePal.findAll(Event.class);
for (Event event : events) {
Display display = new Display(event.getContent());
displayList.add(display);
display.setID(event.getId());
//adapter.notifyItemInserted(displayList.size()-1); // 当有新消息时, 刷新RecyclerView中的显示
displayRecyclerView.scrollToPosition(displayList.size() - 1); // 将 RecyclerView定位到最后一行
}
}
private void refreshDisplays() {//更新recyclerview中的内容
displayList.clear();
List<Event> events = LitePal.findAll(Event.class);
for (Event event : events) {
Display display = new Display(event.getContent());
displayList.add(display);
display.setID(event.getId());
//adapter.notifyItemInserted(displayList.size()-1); // 当有新消息时, 刷新RecyclerView中的显示
displayRecyclerView.scrollToPosition(displayList.size() - 1); // 将 RecyclerView定位到最后一行
}
}
}
这里我选择隐藏了安卓自带的菜单。(因为实在是太丑了)
然后使点击button后能跳转到新建事件页面。
将数据库中的内容取出来展示在recyclerview。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/new2_theme"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="set theme"
/>
<EditText
android:id="@+id/new2_type"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="set type"
/>
<EditText
android:id="@+id/new2_time"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="set time"
/>
<EditText
android:id="@+id/new2_content"
android:layout_width="match_parent"
android:layout_height="200dp"
android:hint="set content"
/>
<Button
android:id="@+id/new2_finish"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="finish"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
放置了4个编辑框用来编辑备忘录的信息,最后一个content为最终显示的内容。
一个确定按钮。
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SetEvent extends AppCompatActivity {
public static int ID=0;
private EditText ettheme;
private EditText ettype;
private EditText ettime;
private EditText etcontent;
private Button btfinish;
private Button btswitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new2);
ettheme = findViewById(R.id.new2_theme);
ettype = findViewById(R.id.new2_type);
ettime = findViewById(R.id.new2_time);
etcontent = findViewById(R.id.new2_content);
btfinish = findViewById(R.id.new2_finish);
btswitch = findViewById(R.id.new2_switch);
btfinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String theme=ettheme.getText().toString();
String time=ettime.getText().toString();//Integer.parseInt(etpage.getText().toString());
String type=ettype.getText().toString();
String content =etcontent.getText().toString(); //Float.valueOf(etprice.getText().toString());
//用字符串接收编辑框中的内容
Event event = new Event();
event.setId(ID++);//使每个事件都有自己独立的ID,方便查找和展示
event.setTheme(theme);
event.setType(type);
event.setTimes(time);
event.setContent(content);
event.save();
//上面为litepal的“增”操作
finish();//返回上一页面
}
});
}
}
上面的代码重复的代码块有很多,日后我会想办法精简代码。
功能是将4个编辑框中的内容赋值到字符串中再保存到数据库中。
完成了这步以后备忘录就已经具备了新建备忘录的功能。
这个功能实现起来有些麻烦,因为要设计recyclerview中单个item的点击事件,因此我们还要修改adapter适配器的代码。
还要让页面内的内容随着点击不同的item而改变内容,这涉及了在活动间传递数据。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<EditText
android:id="@+id/edit_event_theme"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="set theme"
/>
<EditText
android:id="@+id/edit_event_type"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="set type"
/>
<EditText
android:id="@+id/edit_event_time"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="set time"
/>
<EditText
android:id="@+id/edit_event_content"
android:layout_width="match_parent"
android:layout_height="200dp"
android:hint="set content"
/>
<Button
android:id="@+id/edit_event_finish_edit"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="finish"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="@+id/edit_event_delete_event"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="delete"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
这个布局和新建备忘录的很相似,展示原有的内容在编辑框内以供更改,再添加一个删除按钮。
只需重载onCreateViewHolder方法即可做到点击事件和传递数据(只需获取并传递被点击的item的id即可)。
@Override
public DisplayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.display_item, parent, false);
final DisplayViewHolder holder = new DisplayViewHolder(view);
holder.eventView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = holder.getAdapterPosition();
Display display = mDisplayList.get(position);//对于单个view的对象
Intent intent = new Intent(v.getContext(),EditEvent.class);//指定跳转的起点和终点
intent.putExtra("extra_data",display.getId());//向下一个活动传输信息
Toast.makeText(v.getContext(),"id="+display.getId(),Toast.LENGTH_SHORT).show();
v.getContext().startActivity(intent);//开始跳转
}
});
return holder;
}
这样,点击相应的item就可在展示相应的内容
import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.litepal.LitePal;
public class EditEvent extends AppCompatActivity {
private EditText ettheme;
private EditText ettype;
private EditText ettime;
private EditText etcontent;
private Button btfinish;
private Button btdelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_event);
Intent intent = getIntent();//接收上个Intent
final int data = intent.getIntExtra("extra_data",1);//接收上个Intent中key为"extra_data"的数据
ettheme = findViewById(R.id.edit_event_theme);
ettype = findViewById(R.id.edit_event_type);
ettime = findViewById(R.id.edit_event_time);
etcontent = findViewById(R.id.edit_event_content);
btfinish = findViewById(R.id.edit_event_finish_edit);
btdelete = findViewById(R.id.edit_event_delete_event);
initDisplays(data);//初始化,编辑框内显示原来的信息
btfinish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//更新数据
String theme=ettheme.getText().toString();
String time=ettime.getText().toString();//Integer.parseInt(etpage.getText().toString());
String type=ettype.getText().toString();
String content =etcontent.getText().toString(); //Float.valueOf(etprice.getText().toString());
ContentValues values = new ContentValues();
values.put("theme", theme);
LitePal.update(Event.class,values,data);
values.put("times", time);
LitePal.update(Event.class,values,data);
values.put("type", type);
LitePal.update(Event.class,values,data);
values.put("content", content);
LitePal.update(Event.class,values,data);
//更新数据库中的值
finish();//销毁活动
}
});
btdelete.setOnClickListener(new View.OnClickListener() {//删除功能
@Override
public void onClick(View v) {
LitePal.delete(Event.class,data);//删除条件
finish();//销毁活动
}
});
}
private void initDisplays(int id) {
Event event = LitePal.find(Event.class,id);
ettheme.setText(event.getTheme());
ettime.setText(event.getTimes());
ettype.setText(event.getType());
etcontent.setText(event.getContent());
}
}
展示被点击的item的具体内容,并可以随意更改。
点击delete按钮即可删除此备忘录
这样一个粗糙的备忘录就做好了。但是经过我的测试发现:新建、更改、删除备忘录后,主页面不会及时更新,需要退出程序在启动才会有相应变化,很是麻烦。我的想法是检测主页面活动是否在栈顶,当回到栈顶时刷新活动。目前暂未实现。