实现效果:

在这里插入图片描述
实现功能:
在记事列表页点击右侧“+”按钮,跳转到记事项编辑页面,记事项编辑页面有记事项标题编辑、添加图片(可以不添加)、添加记事具体内容与设置记事项的优先级等输入框。用户根据要求输入对应内容。当用户点击保存按钮,会提取各输入框的内容判断是否全部有输入,若出现输入框无内容,则会提示保存失败,否则提示保存成功并且跳转到记事本列表页。当点击取消按钮,会弹出是否保存的提示框,有取消与保存按钮,点击取消则不会对编辑的内容进行保存,直接跳转页面,点击确定则保存内容到数据库中再跳转到列表显示页面。

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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--标题栏-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        android:weightSum="1">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:weightSum="1">
            <TextView
                android:id="@+id/tv_now"
                android:layout_width="wrap_content"
                android:layout_height="49dp"
                android:textColor="#ffffff"
                android:textAlignment="textEnd"
                android:gravity="end|center_vertical"
                android:paddingRight="8dp"
                android:layout_weight="1.50" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical"

            android:layout_weight="0.4">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"

                android:padding="2dp"
                android:layout_gravity="right">
                <Button
                    android:id="@+id/btn_save"
                    android:layout_width="50dp"
                    android:layout_height="40dp"
                    android:text="@string/btn_save"
                    android:background="#ffffff"
                    android:textColor="@color/colorPrimary"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="2dp"/>
                <Button
                    android:id="@+id/btn_return"
                    android:layout_width="50dp"
                    android:layout_height="40dp"
                    android:text="@string/btn_cancel"
                    android:background="#ffffff"
                    android:textColor="@color/colorPrimary"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="2dp"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <EditText
        android:id="@+id/et_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:drawableLeft="@drawable/title"
        android:drawablePadding="10dp"
        android:textSize="14dp"
        android:hint="标题">
        <requestFocus />
    </EditText>
    <ImageView
        android:id="@+id/pic"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@drawable/addp"
        />
    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="添加具体事项"
        android:textSize="16dp"
        android:gravity="left"/>

    <LinearLayout
        android:id="@+id/checklist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="right">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginRight="5dp"
            android:text="设置记事优先级"
            android:textSize="14dp"
            />
        <EditText
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/itempre"
            android:textSize="16dp"
            android:textColor="#888"
            android:hint="0~5"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/testpic"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </LinearLayout>

java代码:

ItemDetailActivity.java

package com.example.lsl.daily_note;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.example.lsl.daily_note.Note.deleteAllNote;

/**
 * Created by lsl on 2020/6/8.
 */

public class ItemDetailActivity extends AppCompatActivity {
    EditText mEditSearch;//搜索框
    Button mTvSearch;//搜索按钮
    private Button add;//添加按钮
    private Button delete;//删除按钮
    private Button up;//升序
    private Button down;//降序
    private ListView noteListView;
    private List<NoteInfo> noteList = new ArrayList<>();
    private List<NoteInfo> noteListup = new ArrayList<>();
    private List<NoteInfo> noteListdown = new ArrayList<>();
    private List<NoteInfo> notesearchList = new ArrayList<>();
    private ListAdapter mListAdapter;
    private ListAdapter mListSearchAdapter;

    private static NoteDataBaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_detail);
        dbHelper = new NoteDataBaseHelper(this,"MyNote.db",null,1);
        Intent intent=getIntent();

        //点击添加按钮跳转页面
        add = (Button) findViewById(R.id.btn_add);
        delete = (Button) findViewById(R.id.btn_delete);

        //先测试添加一条数据
      /*  ContentValues values = new ContentValues();
        values.put(Note.title,"测试笔记");
        values.put(Note.content,"以下为测试内容!!!");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        values.put(Note.time,sdf.format(date));
        Note.insertNote(dbHelper,values);*/

        initView();
        setListener();

        //跳转回主界面 刷新列表
        Intent intent1 = getIntent();
        if (intent1 != null){
            getNoteList();
            mListAdapter.refreshDataSet();//渲染列表
        }
    }

    //初始化视图
    private void initView(){
        noteListView = (ListView) findViewById(R.id.note_list);
        add = (Button) findViewById(R.id.btn_add);
        mTvSearch = (Button) findViewById(R.id.btn_search);
        mEditSearch = (EditText) findViewById(R.id.edit_search);
        up = (Button) findViewById(R.id.btn_up);
        down = (Button) findViewById(R.id.btn_down);
        //获取noteList
        getNoteList();
        mListAdapter = new ListAdapter(ItemDetailActivity.this,noteList);
        noteListView.setAdapter(mListAdapter);
    }
    //搜索刷新列表
    private void refreshListView() {
        mListAdapter.notifyDataSetChanged();
        noteListView.setAdapter(mListAdapter);
    }

    //设置监听器
private void setListener(){
    delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteAllNote(dbHelper);
            getNoteList();
            refreshListView();
            mListAdapter.refreshDataSet();
            Toast.makeText(ItemDetailActivity.this,"删除成功!",Toast.LENGTH_LONG).show();

        }
    });
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ItemDetailActivity.this,NoteEditorActivity.class);
            startActivity(intent);
            ItemDetailActivity.this.finish();
        }
    });

    up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            queryDataUp();
        }
    });
    down.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            queryDataDown();
        }
    });

    noteListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            NoteInfo noteInfo = noteList.get(position);
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("noteInfo",(Serializable)noteInfo);
            intent.putExtras(bundle);
            intent.setClass(ItemDetailActivity.this, NoteEditorActivity.class);
            startActivity(intent);
            ItemDetailActivity.this.finish();
        }
    });
    mTvSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //隐藏键盘
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    });
    /**
     * EditText搜索框对输入值变化的监听,实时搜索
     */
    // TODO: 2017/8/10 3、使用TextWatcher实现对实时搜索
    mEditSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

        @Override
        public void afterTextChanged(Editable editable) {
                String searchString = mEditSearch.getText().toString();
                queryData(searchString);
        }
    });

    noteListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            final NoteInfo noteInfo = noteList.get(position);
            String title = "提示";
            new AlertDialog.Builder(ItemDetailActivity.this)
                    .setIcon(R.drawable.book)
                    .setTitle(title)
                    .setMessage("确定要删除吗?")
                    .setPositiveButton(R.string.btn_confirm, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Note.deleteNote(dbHelper,Integer.parseInt(noteInfo.getId()));
                            refreshListView();
                            noteList.remove(position);
                            mListAdapter.refreshDataSet();
                            Toast.makeText(ItemDetailActivity.this,"删除成功!",Toast.LENGTH_LONG).show();
                        }
                    })
                    .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).create().show();
            return true;
        }
    });
}
    //从数据库中读取所有笔记 封装成List<NoteInfo>
    private void getNoteList(){
        noteList.clear();
        Cursor allNotes = Note.getAllNotes(dbHelper);
        noteInfoSet(noteList,allNotes);
    }
    private void noteInfoSet(List<NoteInfo> noteinfo,Cursor Notes){
        for (Notes.moveToFirst(); !Notes.isAfterLast(); Notes.moveToNext()){
            NoteInfo noteInfo = new NoteInfo();
            noteInfo.setId(Notes.getString(Notes.getColumnIndex(Note._id)));
            noteInfo.setTitle(Notes.getString(Notes.getColumnIndex(Note.title)));
            noteInfo.setContent(Notes.getString(Notes.getColumnIndex(Note.content)));
            noteInfo.setDate(Notes.getString(Notes.getColumnIndex(Note.time)));
            noteInfo.setDes(Notes.getString(Notes.getColumnIndex(Note.content)));
            noteInfo.setPre(Notes.getString(Notes.getColumnIndex(Note.pre)));
            noteInfo.setPhoto(Notes.getBlob(Notes.getColumnIndex(Note.picture)));
            noteinfo.add(noteInfo);
        }
    }
    private void getSearchList(String searchData){
        notesearchList.clear();
        Cursor allNotes = Note.getSearchNotes(dbHelper,searchData);
        noteInfoSet(notesearchList,allNotes);
    }
    private void getListup(){
        noteListup.clear();
        Cursor upNotes = Note.upNotes(dbHelper);
        noteInfoSet(noteListup,upNotes);
    }
    private void getListDown(){
        noteListdown.clear();
        Cursor downNotes = Note.downNotes(dbHelper);
        noteInfoSet(noteListdown,downNotes);
    }

    /**
     * 搜索数据库中的数据
     *
     * @param searchData
     */
    private void queryData(String searchData) {

        getSearchList(searchData);
        mListSearchAdapter = new ListAdapter(ItemDetailActivity.this,notesearchList);
        noteListView.setAdapter( mListSearchAdapter);
        mListSearchAdapter.refreshDataSet();//渲染列表
    }
    private void queryDataUp() {
        getListup();
        mListSearchAdapter = new ListAdapter(ItemDetailActivity.this,noteListup);
        noteListView.setAdapter( mListSearchAdapter);
        mListSearchAdapter.refreshDataSet();//渲染列表
   }
    private void queryDataDown() {
        getListDown();
        mListSearchAdapter = new ListAdapter(ItemDetailActivity.this,noteListdown);
        noteListView.setAdapter( mListSearchAdapter);
        mListSearchAdapter.refreshDataSet();//渲染列表
    }

    //给其他类提供dbHelper
    public static NoteDataBaseHelper getDbHelper() {
        return dbHelper;
    }
}

总结:
这是本人的移动应用课程的期末作业,大概写了一个星期左右,对于Java这门语言只能说是掌握个基础,没有深入的学习,导致代码中出现一些冗余,希望大佬看见不要见笑,对于同样是小白的同学可以借鉴一下。
因为代码量实在是大,所以我没有将主要的代码放上去,需要的同学可以下载(觉得有用就给个星呗)
github:https://github.com/hhhhhhe/Daily-Note
csdn:https://download.csdn.net/download/lsl30522/12562305

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐