成果展示:
1.前期准备
(1)在配置文件中添加权限及启动页面顺序
①展开工程,打开app下方的androidmanifest.xml,添加权限,如下:
<uses-permission android:name="android.permission.camera"/>
<uses-permission android:name="android.permission.write_external_storage"/>
② 依旧在androidmanifest.xml文件中添加启动页面顺序的功能代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ts_menu"> //这里注意修改成自己创建的包名
<uses-permission android:name="android.permission.camera" />
<uses-permission android:name="android.permission.write_external_storage" />
<uses-permission android:name="android.permission.read_external_storage"/>
<application
android:allowbackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundicon="@mipmap/ic_launcher_round"
android:supportsrtl="true"
android:theme="@style/apptheme">
<activity android:name=".addinfoactivity"></activity>
<activity android:name=".loginactivity">
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
<activity android:name=".mainactivity">
</activity>
</application>
</manifest>
(2)添加依赖
展开工程,打开app下方的build.gradle ,添加依赖,如下:依赖添加好之后,要记着同步,在页面右上角的位置单击:sync now 即可。
implementation 'com.android.support:recyclerview-v7:+'
implementation 'com.github.bumptech.glide:glide:4.9.0'
api 'com.blankj:utilcode:1.23.7'
(3)素材
一共5张图片,粘贴到工程的drawable文件夹下来,其中bgone.png,bgthree.jpg两个图片是登录界面和信息添加界面的背景,buttonbg.png,savebg.png图片是添加备忘录按钮和保存按钮的背景,另外一张背景图片是sunshine.jpg是一张默认显示的照片。选择你自己喜欢的图片添加进去吧!
2.所需的布局文件
2.1 activity_login.xml
登录布局界面的实现
<?xml version="1.0" encoding="utf-8"?>
<linearlayout 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:orientation="vertical"
android:background="@drawable/bgone"
tools:context=".loginactivity">
<textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户登录"
android:textstyle="bold"
android:textcolor="#000000"
android:textsize="40sp"
android:layout_margin="100dp"
android:gravity="center"/>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textcolor="#000000"
android:textsize="30sp" />
<edittext
android:id="@+id/edittext_inputname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:textcolor="#000000"
android:textsize="30sp" />
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textcolor="#000000"
android:textsize="30sp" />
<edittext
android:id="@+id/edittext_inputpwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:textcolor="#000000"
android:inputtype="textpassword"
android:textsize="30sp" />
</linearlayout>
<checkbox
android:id="@+id/checkbox_reme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:layout_gravity="right"
android:layout_margin="10dp"/>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:textcolor="#000000"
android:textsize="30sp"
android:layout_weight="1"/>
<button
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
android:textcolor="#000000"
android:textsize="30sp"
android:layout_weight="1"/>
</linearlayout>
</linearlayout>
2.2 activity_main.xml文件代码:
添加备忘录界面
<?xml version="1.0" encoding="utf-8"?>
<linearlayout 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:orientation="vertical"
tools:context=".mainactivity">
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="愿这小小的备忘录,记下我生活中的点点滴滴"
android:textstyle="bold"
android:textcolor="#000000"
android:layout_gravity="center"
android:layout_margin="10dp" />
<button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加备忘录"
android:textstyle="bold"
android:textcolor="#000000"
android:layout_gravity="right"
android:background="@drawable/buttonbg"
android:layout_margin="10dp" />
<android.support.v7.widget.recyclerview
android:id="@+id/recy_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</linearlayout>
2.3 activity_add_info.xml文件代码:
备忘录信息添加布局界面
<?xml version="1.0" encoding="utf-8"?>
<linearlayout 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:orientation="vertical"
android:background="@drawable/bgthree"
tools:context=".addinfoactivity">
<edittext
android:id="@+id/edittext_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="备忘录的标题"
android:textstyle="bold"
android:textcolor="#000000"
android:textsize="30sp"/>
<view
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#009688"
android:layout_margintop="-10dp"/>
<edittext
android:id="@+id/edittext_content"
android:layout_width="match_parent"
android:layout_height="200dp"
android:hint="备忘录的内容"
android:textstyle="bold"
android:textcolor="#000000"
android:textsize="20sp"
android:gravity="top"/>
<view
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#009688"
android:layout_margintop="-10dp"/>
<textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选用下面的任一种方式,添加一张心情图片:"
android:textstyle="bold"
android:textcolor="#000000"
android:textsize="15sp"
android:gravity="top"
android:layout_margin="10dp"/>
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10dp">
<button
android:id="@+id/button_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照"
android:textstyle="bold"
android:textcolor="#000000"
android:textsize="15sp"
android:layout_margin="10dp"/>
<button
android:id="@+id/button_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="从图库中选择"
android:textstyle="bold"
android:textcolor="#000000"
android:textsize="15sp"
android:layout_margin="10dp"/>
</linearlayout>
<imageview
android:id="@+id/imageview_preview"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:src="@drawable/sunshine"
android:layout_marginbottom="20dp"
android:layout_gravity="center"/>
<button
android:id="@+id/button_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"
android:textstyle="bold"
android:textcolor="#000000"
android:textsize="30sp"
android:background="@drawable/savebg"
android:layout_margin="10dp"/>
</linearlayout>
2.4 recy_item.xml文件代码:
主界面--子布局界面
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#7aeccc"
android:id="@+id/item_layout"
android:layout_margin="5dp" >
<linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_margin="5dp"
>
<textview
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标题"
android:textsize="20sp"
android:textstyle="bold"
android:textcolor="#000000"/>
<textview
android:id="@+id/item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"
android:textcolor="#000000"/>
</linearlayout>
<linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="5dp">
<imageview
android:id="@+id/item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher_round"/>
<textview
android:id="@+id/item_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间"
android:textcolor="#000000"
android:layout_gravity="center"/>
</linearlayout>
</linearlayout>
3.所需的java类文件
以下是所需要添加的package,及java类文件。
package所需要添加的文件有adapter、bean、db三个package包。
java类文件除了开始的主文件mainactivity,还需添加memoadapter、memobean、mydbhelper、addinfoactivity、loginactivity5个java类文件。
3.1 memoadapter文件代码:
备忘录的自定义适配器功能代码
package com.example.ts_menu.adapter;
import android.app.alertdialog;
import android.content.context;
import android.content.dialoginterface;
import android.database.sqlite.sqlitedatabase;
import android.graphics.color;
import android.graphics.drawable.gradientdrawable;
import android.os.build;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.textview;
import androidx.annotation.nonnull;
import androidx.annotation.requiresapi;
import androidx.recyclerview.widget.recyclerview;
import com.bumptech.glide.glide;
import com.example.ts_menu.r;
import com.example.ts_menu.bean.memobean;
import com.example.ts_menu.db.mydbhelper;
import java.util.list;
import java.util.random;
//1 类文件后面添加泛型
//2 鼠标定位类文件行红色波浪线处,alt+enter键:添加未实现的方法
//3 鼠标定位类文件行viewholder处,alt+enter键:添加内部类
//4 鼠标定位界面最下方内部类viewholder处,添加extends recyclerview.viewholder
//5 鼠标定位界面最下方内部类viewholder红色波浪线处,alt+enter键:添加构造方法
//6 定义两个对象:上下文环境和数组
//7 定义两个对象下方的空白处:alt+insert键,添加适配器的构造方法
public class memoadapter extends recyclerview.adapter<memoadapter.viewholder> {
private context mcontext;
private list<memobean> arr1;
private mydbhelper mhelper1;
private sqlitedatabase db;
public memoadapter(context mcontext, list<memobean> arr1) {
this.mcontext = mcontext;
this.arr1 = arr1;
}
//负责加载item布局
@nonnull
@override
public memoadapter.viewholder oncreateviewholder(@nonnull viewgroup parent, int i) {
view view= layoutinflater.from(mcontext).inflate(r.layout.recy_item,parent,false);
viewholder mholder=new viewholder(view);
return mholder;
}
//负责加载item的数据
@requiresapi(api = build.version_codes.jelly_bean)
@override
public void onbindviewholder(@nonnull memoadapter.viewholder mholder, final int i) {
final memobean memobean=arr1.get(i);
mholder.item_title.settext(memobean.gettitle());
mholder.item_content.settext(memobean.getcontent());
mholder.item_time.settext(memobean.gettime());
glide.with(mcontext).load(memobean.getimgpath()).into(mholder.item_img);
// 完善:设置recyclerview中每一个子项的颜色和形状
random random = new random();
int color = color.argb(255, random.nextint(256), random.nextint(256), random.nextint(256));
gradientdrawable gradientdrawable = new gradientdrawable();
gradientdrawable.setshape(gradientdrawable.rectangle);//形状
gradientdrawable.setcornerradius(10f);//设置圆角radius
gradientdrawable.setcolor(color);//颜色
mholder.item_layout.setbackground(gradientdrawable);//设置为background
//完善:单击其中的一个子项,弹出删除功能
mholder.item_layout.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
//弹出对话框,删除
alertdialog.builder dialog=new alertdialog.builder(mcontext);
dialog.setmessage("确定删除吗?");
dialog.setpositivebutton("确定", new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialoginterface, int abc) {
//从数据库当中删除掉
mhelper1= new mydbhelper(mcontext);
db=mhelper1.getwritabledatabase();
db.delete("tb_memory","title=?",new string[]{arr1.get(i).gettitle()});
arr1.remove(i);
notifyitemremoved(i);
dialoginterface.dismiss();
}
});
dialog.setnegativebutton("取消",null);
dialog.setcancelable(false);
dialog.create();
dialog.show();
}
});
}
//recyview一共有多少个子项
@override
public int getitemcount() {
return arr1.size();
}
public class viewholder extends recyclerview.viewholder{
textview item_title,item_content,item_time;
imageview item_img;
linearlayout item_layout;
public viewholder(@nonnull view itemview) {
super(itemview);
item_title=itemview.findviewbyid(r.id.item_title);
item_content=itemview.findviewbyid(r.id.item_content);
item_img=itemview.findviewbyid(r.id.item_image);
item_time=itemview.findviewbyid(r.id.item_time);
item_layout=itemview.findviewbyid(r.id.item_layout);
}
}
}
3.2 memobean文件代码:
一个javabean文件,为了存储备忘录的信息
package com.example.ts_menu.bean;
public class memobean {
private string title;
private string content;
private string imgpath;
private string time;
public memobean(string title, string content, string imgpath, string time) {
this.title = title;
this.content = content;
this.imgpath = imgpath;
this.time = time;
}
public string gettitle() {
return title;
}
public void settitle(string title) {
this.title = title;
}
public string getcontent() {
return content;
}
public void setcontent(string content) {
this.content = content;
}
public string getimgpath() {
return imgpath;
}
public void setimgpath(string imgpath) {
this.imgpath = imgpath;
}
public string gettime() {
return time;
}
public void settime(string time) {
this.time = time;
}
}
3.3 mydbhelper文件代码:
数据库文件
package com.example.ts_menu.db;
import android.content.context;
import android.database.sqlite.sqlitedatabase;
import android.database.sqlite.sqliteopenhelper;
public class mydbhelper extends sqliteopenhelper {
private static string dbname="zsmemo.db";
private static int version=1;
//构造方法
public mydbhelper( context context) {
super(context, dbname, null, version);
}
// 创建数据库
@override
public void oncreate(sqlitedatabase db) {
//创建数据表
db.execsql("create table tb_memory(_id integer primary key,title string (200),content string (2000),imgpath string (200),mtime string (50))");
}
//升级数据库
@override
public void onupgrade(sqlitedatabase sqlitedatabase, int i, int i1) {
}
}
3.4 addinfoactivity文件代码:
拍照功能直接闪退:
if(build.version.sdk_int>=build.version_codes.n) { strictmode.vmpolicy.builder builder=new strictmode.vmpolicy.builder(); strictmode.setvmpolicy(builder.build()); }
使用上述代码防止闪退。
在拍照功能时应该将保存路径代码改为tmp_path=environment.getexternalstoragepublicdirectory(environment.directory_dcim).getabsolutepath()+"/image"+randtime+".jpg";
有效防止拍照后确定不了问题。
package com.example.ts_menu;
import android.content.contentvalues;
import android.content.intent;
import android.database.sqlite.sqlitedatabase;
import android.net.uri;
import android.os.environment;
import android.provider.mediastore;
import android.os.bundle;
import android.text.format.time;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
import android.widget.imageview;
import android.widget.toast;
import androidx.annotation.nullable;
import androidx.appcompat.app.appcompatactivity;
import com.blankj.utilcode.util.uriutils;
import com.bumptech.glide.glide;
import com.example.ts_menu.db.mydbhelper;
import com.example.ts_menu.db.mydbhelper;
import java.io.file;
import java.io.ioexception;
public class addinfoactivity extends appcompatactivity {
//定义对象
edittext edit_title,edit_content;
button btn_camera,btn_photo,btn_save;
imageview img_preview;
string tmp_path,disp_path;
mydbhelper mhelper;
sqlitedatabase db;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_add_info);
//1 绑定控件
initview();
//2 单击按钮、拍照、从图库中选择照片
btnonclick();
//3 接受拍好照片、接受从图库当中选择的照片 ------方法:系统回调
//4 把信息保存到数据库中
btnsave();
}
//1 绑定控件-----代码
private void initview() {
edit_title=findviewbyid(r.id.edittext_title);
edit_content=findviewbyid(r.id.edittext_content);
btn_camera=findviewbyid(r.id.button_camera);
btn_photo=findviewbyid(r.id.button_photo);
img_preview=findviewbyid(r.id.imageview_preview);
btn_save=findviewbyid(r.id.button_save);
mhelper=new mydbhelper(addinfoactivity.this);
db= mhelper.getwritabledatabase();
}
//2 单击按钮、拍照----------代码
private void btnonclick() {
btn_camera.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
//拍照
time time=new time();
time.settonow();
string randtime=time.year+(time.month+1)+time.monthday+time.hour+time.minute+time.second+"";
// tmp_path= environment.getexternalstoragedirectory()+"/image"+ randtime+".jpg";
tmp_path=environment.getexternalstoragepublicdirectory(environment.directory_dcim).getabsolutepath()+"/image"+randtime+".jpg";
file imgfile=new file(tmp_path);
try {
imgfile.createnewfile();
} catch (ioexception e) {
e.printstacktrace();
}
intent intent=new intent("android.media.action.image_capture");
intent.putextra(mediastore.extra_output, uri.fromfile(imgfile) );
startactivityforresult(intent,11);
}
});
btn_photo.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
//选择照片
intent intent=new intent("android.intent.action.get_content");
intent.settype("image/*");
startactivityforresult(intent,22);
}
});
}
//3 接受拍好照片、接受从图库当中选择的照片 ------方法:系统回调
@override
protected void onactivityresult(int requestcode, int resultcode, @nullable intent data) {
switch (requestcode){
case 11:
if(resultcode==result_ok){
disp_path=tmp_path;
glide.with(addinfoactivity.this).load(disp_path).into(img_preview);
}
break;
case 22:
uri imageuri=data.getdata();
if (imageuri==null)
{
return;
}
disp_path=uriutils.uri2file(imageuri).getpath();
glide.with(addinfoactivity.this).load(disp_path).into(img_preview);
break;
default:
break;
}
}
//4 把信息保存到数据库中-------------代码
private void btnsave() {
btn_save.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
//保存信息到数据库代码
time time=new time();
time.settonow();
contentvalues contentvalues=new contentvalues();//一行
contentvalues.put("title",edit_title.gettext().tostring());//1行——1列
contentvalues.put("content",edit_content.gettext().tostring());//1行——3列
contentvalues.put("imgpath",disp_path);
contentvalues.put("mtime",time.year+"/"+(time.month+1)+"/"+time.monthday);
db.insert("tb_memory",null,contentvalues);
toast.maketext(addinfoactivity.this,"保存成功",toast.length_short).show();
//跳转到主界面
intent intent=new intent(addinfoactivity.this,mainactivity.class);
startactivity(intent);
finish();
}
});
}
}
3.5 loginactivity文件代码:
package com.example.ts_menu;
import android.content.intent;
import android.content.sharedpreferences;
//import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.checkbox;
import android.widget.edittext;
import androidx.appcompat.app.appcompatactivity;
public class loginactivity extends appcompatactivity {
//定义对象
private edittext edit_inputname,edit_inputpwd;
private checkbox check_reme;
private button btn_login;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_login);
//1 绑定控件
initview();
//2 单击登录按钮,将用户名和密码保存起来
btnloginonclick();
//3 下次启动,直接显示用户名和密码
displayinfo();
}
//1 绑定控件--------代码
private void initview() {
edit_inputname=findviewbyid(r.id.edittext_inputname);
edit_inputpwd=findviewbyid(r.id.edittext_inputpwd);
check_reme=findviewbyid(r.id.checkbox_reme);
btn_login=findviewbyid(r.id.button_login);
}
//2 单击登录按钮,将用户名和密码保存起来----代码
private void btnloginonclick() {
btn_login.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
//保存用户名和密码
sharedpreferences.editor editor=getsharedpreferences("myinfo",0).edit();
editor.putstring("name",edit_inputname.gettext().tostring());
editor.putstring("pwd",edit_inputpwd.gettext().tostring());
editor.putboolean("st",check_reme.ischecked());
editor.commit();
//跳转到第二页
intent intent=new intent(loginactivity.this,mainactivity.class);
startactivity(intent);
}
});
}
//3 下次启动,直接显示用户名和密码-----代码
private void displayinfo() {
string strname=getsharedpreferences("myinfo",0).getstring("name","");
string strpwd=getsharedpreferences("myinfo",0).getstring("pwd","");
boolean status=getsharedpreferences("myinfo",0).getboolean("st",false);
if(status==true){
edit_inputname.settext(strname);
edit_inputpwd.settext(strpwd);
check_reme.setchecked(true);
}else{
edit_inputname.settext("");
edit_inputpwd.settext("");
check_reme.setchecked(false);
}
}
}
3.6 mainactivity文件代码:
package com.example.ts_menu;
import android.content.intent;
import android.database.cursor;
import android.database.sqlite.sqlitedatabase;
import android.os.bundle;
import android.view.view;
import android.widget.adapter;
import android.widget.button;
import androidx.appcompat.app.appcompatactivity;
import androidx.recyclerview.widget.recyclerview;
import androidx.recyclerview.widget.staggeredgridlayoutmanager;
import com.example.ts_menu.adapter.memoadapter;
import com.example.ts_menu.bean.memobean;
import com.example.ts_menu.db.mydbhelper;
import java.util.arraylist;
import java.util.list;
public class mainactivity extends appcompatactivity {
//定义对象
private button btn_add;
private recyclerview recy_view;
private mydbhelper mhelper;
sqlitedatabase db;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
//1 绑定控件
initview();
//2 对单击添加单击事件
btnonclicknext();
//3完善:从数据库获取数据,显示到recyclerview控件里面
recydisplay();
}
//1 绑定控件-----------代码
private void initview() {
btn_add=findviewbyid(r.id.button_add);
recy_view=findviewbyid(r.id.recy_view);
mhelper=new mydbhelper(mainactivity.this);
db=mhelper.getwritabledatabase();
}
//2 对单击添加单击事件-----代码
private void btnonclicknext() {
btn_add.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
//单击后跳转到一下页
intent intent=new intent(mainactivity.this,addinfoactivity.class);
startactivity(intent);
finish();
}
});
}
//3完善:从数据库获取数据,显示到recyclerview控件里面---------------代码
private void recydisplay() {
//3.1准备数据-----------标题、内容、图片、时间(类)
list<memobean> arr=new arraylist();
//从数据库里面取数据了
cursor cursor=db.rawquery("select * from tb_memory",null);
while(cursor.movetonext()){
string mytitle=cursor.getstring(cursor.getcolumnindex("title"));
string mycontent=cursor.getstring(cursor.getcolumnindex("content"));
string myimgpath=cursor.getstring(cursor.getcolumnindex("imgpath"));
string mymtime=cursor.getstring(cursor.getcolumnindex("mtime"));
memobean memobean=new memobean(mytitle,mycontent,myimgpath,mymtime);
arr.add(memobean);
}
cursor.close();
//3.2 子布局 recy_item
//3.3 数据------桥(适配器memoadapter)----------------子布局
memoadapter adapter=new memoadapter(mainactivity.this,arr);
//3.4 确定显示的方式
staggeredgridlayoutmanager st=new staggeredgridlayoutmanager(2,staggeredgridlayoutmanager.vertical);
recy_view.setlayoutmanager(st);
//3.5 让数据显示出来
recy_view.setadapter(adapter);
}
}
注意:activity文件中相关的名称报错,得换成自己所创建工程的名字。
发表评论