今天開始完成記筆記功能,由於個人時間還比較充足,因此我先用本地數據庫寫了一遍。
遇到的問題:一打開添加頁面就閃退。
解決方法:編輯頁面沒有註冊。java
package com.xrj.biji;android
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;app
import java.text.SimpleDateFormat;
import java.util.Date;ide
public class EditActivity extends AppCompatActivity {this
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;code
import androidx.appcompat.app.AppCompatActivity;orm
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;get
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(); }
}