android studio项目实战——备忘录(附源码)

成果展示:

 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文件中相关的名称报错,得换成自己所创建工程的名字。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/588809.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【二】电力系统规约IEC 104详解

电力系统规约IEC 104详解 概述 很早就准备梳理出一下电力系统规约系列的文章&#xff0c;因为自己在实践过程中发现这方面太难找了&#xff0c;网上的资料也都比较陈旧。我接触和使用IEC系列规约也有一段时间了&#xff0c;本着总结和分享的想法&#xff0c;我想推出这系列的文…

在线的调试器pythontutor,支持C/C++

1. 背景介绍 对于C语言的学习最复杂的可能无疑就是指针的&#xff0c;指针因其灵活、晦涩难懂等特点而出名&#xff0c;本文并不介绍利用gdb的角度去分析它&#xff0c;而是通过一个在线网站而分析&#xff1b; 2.C代码调试 3. C代码调试 4在线网站 https://pythontutor.com/…

【项目纪实】某国有航空公司人力资源系统诊断咨询项目

公司的人力资源管理问题一直都比较严重&#xff0c;比如人员冗余、员工工作积极性差等问题&#xff0c;虽然经过多次的管理尝试&#xff0c;存在的问题仍然没有缓解。华恒智信人力资源咨询公司的老师特别专业&#xff0c;帮我们系统、全面的诊断了人力资源管理上存在的问题&…

正点原子[第二期]Linux之ARM(MX6U)裸机篇学习笔记-6.3--Cortex-A7寄存器介绍

前言&#xff1a; 本文是根据哔哩哔哩网站上“正点原子[第二期]Linux之ARM&#xff08;MX6U&#xff09;裸机篇”视频的学习笔记&#xff0c;在这里会记录下正点原子 I.MX6ULL 开发板的配套视频教程所作的实验和学习笔记内容。本文大量引用了正点原子教学视频和链接中的内容。…

Numerical Analysis(byRichard.L..Burden)【pdf高清英文原版】

专栏导读 作者简介&#xff1a;工学博士&#xff0c;高级工程师&#xff0c;专注于工业软件算法研究本文已收录于专栏&#xff1a;《有限元编程从入门到精通》本专栏旨在提供 1.以案例的形式讲解各类有限元问题的程序实现&#xff0c;并提供所有案例完整源码&#xff1b;2.单元…

HarmonyOS 4.0(鸿蒙开发)01 - 怎么学习鸿蒙引导篇

作为公司的全栈开发工程师 以及 未来的发展是有鸿蒙这个阶段的&#xff0c;以及本身具有这个技术栈由此后续会分享自己在实战中学习到的东西&#xff0c;碰到的bug都会分享出来&#xff0c;这是引导篇期待后续的更新 学习目标&#xff1a; 理解HarmonyOS操作系统的架构和开发…

目标检测算法YOLOv3简介

YOLOv3由Joseph Redmon等人于2018年提出&#xff0c;论文名为&#xff1a;《YOLOv3: An Incremental Improvement》&#xff0c;论文见&#xff1a;https://arxiv.org/pdf/1804.02767.pdf &#xff0c;项目网页&#xff1a;https://pjreddie.com/darknet/yolo/ 。YOLOv3是对YOL…

解决IDEA下springboot项目打包没有主清单属性

1.问题出现在SpringBoot学习中 , 运行maven打包后无法运行 报错为spring_boot01_Demo-0.0.1-SNAPSHOT.jar中没有主清单属性 SpringBoot版本为 2.6.13 Java 版本用的8 解决方法 1.执行clean 删除之前的打包 2.进行打包规范设置 2.1 3.进行问题解决 (借鉴了阿里开发社区) 使用…

利用PDAL2.7.1 实现点云滤波

利用PDAL2.7.1 实现点云滤波 本文介绍利用PDAL实现点云滤波方法&#xff0c;包含pipeline命令行运行、C代码两种方法&#xff0c;C代码分别介绍对点云文件进行滤波、点云全部在内存中进行滤波的pdal两种调用方法。并简单探究pdal的设计结构。 目录 1 pipeline命令调用方法2 文…

R语言4版本安装mvstats(纯新手)

首先下载mvstats.R文件 下载mvstats.R文件点此链接&#xff1a;https://download.csdn.net/download/m0_62110645/89251535 第一种方法 找到mvstats.R的文件安装位置&#xff08;R语言的工作路径&#xff09; getwd() 将mvstats.R保存到工作路径 在R中输入命令 source(&qu…

飞腾D2000+X100 TYPE6全国产核心板

飞腾D2000X100 TYPE6核心板 产品概述 飞腾D2000X100 TYPE6核心板为增强型自主控制器核心板&#xff0c;其核心芯片CPU采用飞腾D2000/8核工业版CPU、飞腾桥片X100、双通道DDR4L插槽、PHY芯片等。 产品特点 l 基于飞腾D2000X100桥片 l 丰富的PCIE扩展资源&#xff0c;一路PCIE…

C++入门系列-函数重载

&#x1f308;个人主页&#xff1a; 羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” 函数重载 自然语言当中&#xff0c;一个词可以有多重含义&#xff0c;人们可以通过上下文来判断该词真实的含义&#xff0c;即该词被重载了。 函数重载的概念 函数重载&#x…

A4的PDF按A3打印

先用办公软件打开&#xff0c;比如WPS。 选择打印-属性。 纸张选A3&#xff0c;如果是双面打印&#xff0c;选短边装订&#xff0c;然后在版面-页面排版-每张页数&#xff08;N合1&#xff09;选2。 不同打印机的具体配置可能不一样&#xff0c;但大体都是这个套路。

rocketmq dashboard控制台中topic状态无法展示

现象 在使用rocketmq控制台查看topic状态和订阅状态时&#xff0c;出现错误和没有信息的情况。 原因 rocketmq控制台版本问题&#xff0c;最新版本为1.0.1&#xff0c;支持rocketmq5版本&#xff0c;如果使用rocketmq4版本的服务无法兼容对应的数据。同理1.0.0版本也无法兼容ro…

中兴ZXV10 B860AV2.1机顶盒刷机

移动的电视盒子如果不续费&#xff0c;连桌面都进不去&#xff0c;趁着五一有空把系统刷了。整体上比较顺利。 注意这个盒子只有两个螺丝&#xff0c;盒子上已经标识&#xff0c;如上图左上角和右下角。盒子里面有卡扣&#xff0c;卸掉螺丝直接扣是很难打开的&#xff0c;需要用…

【CLion】clion无法加载或找不到cmakekists文件

一、问题表象 最近工作中&#xff0c;在git pull远程仓库最新版本程序后&#xff0c;平时打开CLion自动加载的工程CMakeLists文件突然失效&#xff08;显示找不到可编译的文件&#xff09;&#xff0c;无法debug程序。 二、原因分析 基于平时的编码经验和之前git pull也出现…

深度学习之基于CIFAR10图像分类可视化

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 深度学习之基于CIFAR-10图像分类可视化项目简介 一、项目背景 随着深度学习和计算机视觉技术的飞速发展&#xff…

边缘计算含义与应用简析

边缘计算概述 边缘计算使数据存储和处理靠近生成或收集数据的位置&#xff0c;而不是在位于数千公里的服务器上。它将通过保持灵活性在边缘无缝可靠地部署服务。它比云计算更安全&#xff0c;因为不需要传输数据。因此&#xff0c;在将数据从边缘移动到云端时&#xff0c;不用…

基于React实现B站评论区

今天继续来学习一下React&#xff0c;使用React实现B站评论区&#xff0c;如下图&#xff1a; 在使用React开发类似B站评论区的功能时&#xff0c;我们需要考虑以下几个关键点来构建一个基本的评论系统&#xff1a; 1. 设计组件结构 首先&#xff0c;设计组件结构是关键。至少…

什么是弹性云服务器(ECS)

弹性云服务器&#xff08;Elastic Cloud Server&#xff0c;ECS&#xff09;是由CPU、内存、操作系统、云硬盘组成的基础的计算组件。弹性云服务器创建成功后&#xff0c;您就可以像使用自己的本地PC或物理服务器一样&#xff0c;在云上使用弹性云服务器。 云服务器ECS&#x…
最新文章