今天开始完成记笔记功能,因为我的时间还比较充足,所以我先用本地数据库写了一遍。
遇到的问题:一打开添加页面就闪退。
解决方法:编辑页面没有注册。

package com.xrj.biji;

import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.view.KeyEvent;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;

public class EditActivity extends AppCompatActivity {

EditText et;
private String content;
private String time;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_layout);
    et = findViewById(R.id.et);
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        return true;
    }
    else if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent intent = new Intent();
        intent.putExtra("content", et.getText().toString());
        intent.putExtra("time",dataToStr());
        setResult(RESULT_OK,intent);
        finish();
        return true;
    }
    return super.onKeyDown(keyCode,event);
}

public String dataToStr() {
    Date date = new Date();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return simpleDateFormat.format(date);

}

}

package com.xrj.biji;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
FloatingActionButton btn;
TextView tv;
private ListView lv;//有分隔符,无限长
private NoteAdapter adapter;
private List<Note> noteList = new ArrayList<>();
private NoteDatabase dbHelper;
private Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (FloatingActionButton)findViewById(R.id.fab);
    lv = findViewById(R.id.lv);
    adapter = new NoteAdapter(getApplicationContext(),noteList);//初始化adapter
    refreshListView();//每次更新的时候刷新
    lv.setAdapter(adapter);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,EditActivity.class);//从Main跳转到Edit
            startActivityForResult(intent,0);//启动跳转,把返回值传回来
        }
    });
}

@Override
protected void onActivityResult (int requestCode,int resultCode,Intent data) { //接收返回的内容
    super.onActivityResult(requestCode,resultCode,data);
    String content = data.getStringExtra("content");
    String time = data.getStringExtra("time");
    Note note = new Note(content,time,1);
    DatabaseHelper dbHelper = new DatabaseHelper(context);
    dbHelper.open();
    dbHelper.addNote(note);
    dbHelper.close();
    refreshListView();
}

public void refreshListView() {
    DatabaseHelper dbHelper = new DatabaseHelper(context);
    dbHelper.open();
    if(noteList.size()>0)
        noteList.clear();
    noteList.addAll(dbHelper.getAllNotes());
    dbHelper.close();
    adapter.notifyDataSetChanged();
}

}

版权声明:本文为xrj-原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xrj-/p/12969540.html