記事本 Android實現
一、效果展示
二、總體框架
1、框架圖
2、SQLite數據庫
三、實現步驟
1、activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#fefefe"
tools:context=".MainActivity">
<TextView
android:id="@+id/note_name"
android:layout_width="match_parent"
android:layout_height="45dp"
android:textSize="20sp"
android:textColor="@android:color/white"
android:gravity="center"
android:textStyle="bold"
android:background="#fb7a6a"
android:text="記事本"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/note_name"
android:cacheColorHint="#00000000"
android:divider="#E4E4E4"
android:dividerHeight="1dp"
android:fadingEdge="none"
android:listSelector="#00000000"
android:scrollbars="none"></ListView>
<ImageView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
2、新建列表子項布局文件item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="12dp">
<TextView
android:id="@+id/things"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:lineSpacingExtra="3dp"
android:paddingTop="10dp"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fb7a6a"
android:paddingTop="5dp"
android:paddingBottom="7dp"/>
</LinearLayout>
3、修改清單文件的theme 屬性,去掉默認標題欄。
android:theme="@style/Theme.AppCompat.NoActionBar">
4、新建 Bean .java文件,定義備忘錄信息的所有屬性,封裝備忘錄信息實體類,與數據庫表中信息對應。
package com.example.mynote;
public class Bean
{
private String id; //記錄的 id
private String bthings; //記錄的內容
private String btime; //保存記錄的時間
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getNotepadContent()
{
return bthings;
}
public void setNotepadContent(String notepadContent)
{
this.bthings = notepadContent;
}
public String getNotepadTime()
{
return btime;
}
public void setNotepadTime(String notepadTime)
{
this.btime = notepadTime;
}
}
5、新建數據適配器 NoteAdapter 對 ListView 控件進行數據適配
//創建 NoteAdapter 類,繼承自 BaseAdapter 類。
public class NoteAdapter extends BaseAdapter
{
private List <Bean> list; //數據准備
private Context mycontext;
private LayoutInflater myinflate; //加載布局視圖
//構造函數
public NoteAdapter(Context context,List <Bean> list)
{
this.list=list;
this.mycontext=context;
this.myinflate=LayoutInflater.from(context);
}
//實現4個方法
@Override
public int getCount()
{ //返回列表長度
return list.size();
}
@Override
public Object getItem(int position)
{ //返回控件
return list.get(position);
}
@Override
public long getItemId(int i)
{ //返回下標
return i;
}
@Override
//參數convertview用於復用列表項布局View,vgroup用於加載列表項xml布局
public View getView(int i, View convertview, ViewGroup vgroup)
{
ViewHolder myholder;
//如果沒有空閑的列表項就新創建一個
if(convertview==null)
{
convertview=myinflate.inflate(R.layout.item,null);
myholder=new ViewHolder(convertview);
convertview.setTag(myholder);
}
else
{
myholder=(ViewHolder) convertview.getTag();
}
final Bean info=(Bean) getItem(i);
myholder.textView_things.setText(info.getNotepadContent());
myholder.textView_time.setText(info.getNotepadTime());
return convertview;
}
}
6、在NoteAdapter.java中創建一個 ViewHolder 類來初始化列表子項 Item 界面中的控件。
class ViewHolder
{
TextView textView_things;
TextView textView_time;
View wholeView;
public ViewHolder(View view)
{
textView_things=(TextView) view.findViewById(R.id.things);
textView_time=(TextView) view.findViewById(R.id.time);
wholeView = view;
}
}
完整的NoteAdapter.java文件:
package com.example.mynote;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
//創建 NoteAdapter 類,繼承自 BaseAdapter 類。
public class NoteAdapter extends BaseAdapter
{
private List <Bean> list; //數據准備
private Context mycontext;
private LayoutInflater myinflate; //加載布局視圖
//構造函數
public NoteAdapter(Context context,List <Bean> list)
{
this.list=list;
this.mycontext=context;
this.myinflate=LayoutInflater.from(context);
}
//實現4個方法
@Override
public int getCount()
{ //返回列表長度
return list.size();
}
//根據位置,再去list里面找數據
@Override
public Object getItem(int position)
{ //返回控件
return list.get(position);
}
//返回位置
@Override
public long getItemId(int i)
{ //返回下標
return i;
}
//創一個格子,獲取兩個控件,填數據
@Override
//參數convertview用於復用列表項布局View,vgroup用於加載列表項xml布局
public View getView(int i, View convertview, ViewGroup vgroup)
{
ViewHolder myholder;
//如果沒有空閑的列表項(格子)就新創建一個
if(convertview==null)
{
convertview=myinflate.inflate(R.layout.item,null);
myholder=new ViewHolder(convertview);
convertview.setTag(myholder);
}
else
{
//有列表項(格子)就從里面拿
myholder=(ViewHolder) convertview.getTag();
}
//把getItem(i)第i個位置的內容拿出來
final Bean info=(Bean) getItem(i);
myholder.textView_things.setText(info.getNotepadContent());
myholder.textView_time.setText(info.getNotepadTime());
return convertview;
}
class ViewHolder
{
TextView textView_things;
TextView textView_time;
View wholeView;
public ViewHolder(View view)
{
textView_things=(TextView) view.findViewById(R.id.things);
textView_time=(TextView) view.findViewById(R.id.time);
wholeView = view;
}
}
}
7、創建數據庫幫助類 DBSQLiteHelper 實現對數據庫創建、數據表的創建。
public class DBSQLiteHelper extends SQLiteOpenHelper
{
public static final String DATABASE_NAME = "Notepad";//數據庫名
public static final int DATABASE_VERION = 1; //數據庫版本
public static final String DATABASE_NOTEPADTB = "Note"; //記事本數據表名
public static final String NOTEPAD_ID = "id"; //id字段名
public static final String NOTEPAD_CONTENT = "content"; //內容字段名
public static final String NOTEPAD_NOTETIME = "notetime"; //時間字段名
//建表語句
public static final String CREATE_NOTE = "create table Note ("
+ "id integer primary key autoincrement, "
+ "content text, "
+ "notetime text)";
//或以下建表語句
// public static final String CREATE_NOTE = "create table Note ("
// + NOTEPAD_ID+" integer primary key autoincrement, "
// + NOTEPAD_CONTENT+" text, "
// + NOTEPAD_NOTETIME+" text)";
//構造函數
public DBSQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context, name, factory, version);
Log.e("SQLiteHelper","construct func2");
}
public DBSQLiteHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERION);
Log.e("SQLiteHelper","construct func");
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_NOTE);
Log.e("SQLiteHelper","onCreate func");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String CREATE_USER = "create table User("+
"id integer primary key autoincrement,"+
"username text,"+
"password text)";
db.execSQL(CREATE_USER);
Log.e("SQLiteHelper","onUpgrade func");
}
}
8、新建DAO.java文件實現對數據庫中記錄的增刪改查,以及獲取當前日期。
查詢的結果是 List。刪除和修改時都需要指定 id。添加時不需要指定 id。為了預防書寫錯誤,可以將數據庫的名稱、表名、數據庫版本、數據庫表中 的列名定義為字符串常量
package com.example.mynote;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DAO
{
private static final String DATABASE_NOTEPADTB = "Note";
public static final String NOTEPAD_ID = "id"; //id字段名
public static final String NOTEPAD_CONTENT = "content"; //內容字段名
public static final String NOTEPAD_NOTETIME = "notetime"; //時間字段名
private DBSQLiteHelper dbOpenHelper;
public DAO(Context context)
{
//創建DBSQLiteHelper對象
dbOpenHelper = new DBSQLiteHelper(context);
//dbOpenHelper.getWritableDatabase(); //創建數據庫,可調用,否則在第一次查詢時調用
}
//添加數據
public boolean insertData(String userContent,String userTime)
{
//獲取數據庫對象
SQLiteDatabase sqLiteDatabase = dbOpenHelper.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NOTEPAD_CONTENT,userContent);
contentValues.put(NOTEPAD_NOTETIME,userTime);
return sqLiteDatabase.insert(DATABASE_NOTEPADTB,null,contentValues)>0;
}
//刪除數據
public boolean deleteData(String id)
{
SQLiteDatabase sqLiteDatabase=dbOpenHelper.getReadableDatabase();
String whereClause = NOTEPAD_ID + "=?";
String[] whereArgs = new String[]{String.valueOf(id)};
return sqLiteDatabase.delete(DATABASE_NOTEPADTB,whereClause,whereArgs)>0;
}
//修改數據
public boolean updateData(String id,String content,String time)
{
SQLiteDatabase sqLiteDatabase=dbOpenHelper.getReadableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(NOTEPAD_CONTENT,content);
contentValues.put(NOTEPAD_NOTETIME,time);
String whereClause = NOTEPAD_ID + "=?";
String[] whereArgs = new String[]{String.valueOf(id)};
return sqLiteDatabase.update(DATABASE_NOTEPADTB,contentValues,whereClause,whereArgs)>0;
}
//查詢數據
public List<Bean> query()
{
SQLiteDatabase sqLiteDatabase=dbOpenHelper.getReadableDatabase();
List<Bean> list = new ArrayList<Bean>();
Cursor cursor = sqLiteDatabase.query(DATABASE_NOTEPADTB,null,null,null,
null,null,NOTEPAD_ID+" desc");//按id降序排序
if (cursor.moveToFirst())
{
do{
Bean noteInfo=new Bean();
String id = String.valueOf(cursor.getInt(cursor.getColumnIndex(NOTEPAD_ID)));
String content = cursor.getString(cursor.getColumnIndex(NOTEPAD_CONTENT));
String time = cursor.getString(cursor.getColumnIndex(NOTEPAD_NOTETIME));
noteInfo.setId(id);
noteInfo.setNotepadContent(content);
noteInfo.setNotepadTime(time);
list.add(noteInfo);
}while(cursor.moveToNext());
cursor.close();
}
return list;
}
//獲取當前日期,格式化為yyyy年MM月dd日 HH:mm:ss形式的字符串
public static final String getTime()
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
public void initData()
{
insertData("天氣變冷了,記得穿棉衣哦",getTime());
insertData("今天要帶小憨豬去外面玩",getTime());
}
}
9、新建Record活動,在點擊列表子項和“添加按鈕”時跳轉該活動。
(1)Record.java文件
package com.example.mynote;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Record extends AppCompatActivity implements View.OnClickListener {
private ImageView note_back; //返回首頁
private TextView noteName; //根據情況顯示“添加便簽”or“修改便簽”
private TextView note_time; //便簽修改時間
private EditText content; //便簽正文
private ImageView delete; //清除
private ImageView note_save; // 保存
private String id;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
setContentView(R.layout.record);
note_back = (ImageView) findViewById(R.id.note_back);
note_time = (TextView)findViewById(R.id.tv_time);
content = (EditText) findViewById(R.id.note_content);
delete = (ImageView) findViewById(R.id.delete);
note_save = (ImageView) findViewById(R.id.note_save);
noteName = (TextView) findViewById(R.id.note_name);
initData();//根據來源決定頂部顯示"添加便簽"還是"修改便簽"
note_back.setOnClickListener(this); //返回首頁
delete.setOnClickListener(this); //清除便簽
note_save.setOnClickListener(this); //保存
}
//根據來源決定頂部顯示"添加便簽"還是"修改便簽"
//如果是長按打開顯示"修改便簽",否則顯示"添加便簽"
protected void initData()
{
noteName.setText("添加便簽");
Intent intent = getIntent();
if(intent!= null)
{
id = intent.getStringExtra("id");
if (id != null)
{ //"修改便簽",顯示原便簽內容
noteName.setText("修改便簽");
content.setText(intent.getStringExtra("content"));
note_time.setText(intent.getStringExtra("time"));
//修改顯示時間可見,新增不可見
note_time.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.note_back: //返回鍵功能,關閉當前頁面
finish();
break;
case R.id.delete: //“清除按鈕”功能:清除編輯框顯示
content.setText("");
break;
case R.id.note_save:
String noteContent=content.getText().toString().trim();
if (id != null){//修改操作
if (noteContent.length()>0){
DAO notepadDAO = new DAO(this);
if (notepadDAO.updateData(id, noteContent, notepadDAO.getTime())){
showToast("修改成功");
setResult(1);
finish();
}else {
showToast("修改失敗");
}
}else {
showToast("修改內容不能為空!");
}
}else {
//向數據庫中添加數據
if (noteContent.length()>0){
DAO notepadDAO = new DAO(this);
if (notepadDAO.insertData(noteContent, notepadDAO.getTime())){
showToast("保存成功");
setResult(1);
finish();
}else {
showToast("保存失敗");
}
}else {
showToast("內容不能為空!");
}
}
break;
}
}
public void showToast(String message){
Toast.makeText(Record.this,message,Toast.LENGTH_SHORT).show();
}
}
(2)record.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:background="#fefefe"
android:orientation="vertical"
tools:context=".Record">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#fb7a6a">
<ImageView
android:id="@+id/note_back"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="11dp"
android:src="@drawable/back" />
<TextView
android:id="@+id/note_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="記事本"
android:textColor="@android:color/white"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"
android:visibility="gone"
android:textColor="#fb7a6a"/>
<EditText
android:id="@+id/note_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="請輸入要添加的內容"
android:paddingLeft="5dp"
android:textColor="@android:color/black"
android:background="#fefefe" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#fb7a6a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@drawable/delete"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
<ImageView
android:id="@+id/note_save"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@drawable/save_note"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
</LinearLayout>
</LinearLayout>
10、修改 MainActivity.java文件,實現備忘錄列表顯示和新增功能
package com.example.mynote;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity{
private ListView listView;
private List<Bean> notelist;
private DAO notepadDAO;
NoteAdapter adapter;
//在onCreate ()方法中為 ListView 控件設置適配器,適配器的數據源來自數據庫表的查詢結果
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//用於顯示便簽的列表
listView = (ListView) findViewById(R.id.listview);
notepadDAO = new DAO(this); //創建 NotepadDAO 對象
notepadDAO.initData();
//下面的if判斷可刪,就像int n=0,n=3可直接寫成int n=3
if (notelist!=null)
{
notelist.clear();
}
//從數據庫中查詢數據(保存的所有便簽)
notelist = notepadDAO.query();//第一次調用,創建數據庫
adapter = new NoteAdapter(this, notelist);
//adapter把數據放進去
listView.setAdapter(adapter);
// 為“添加按鈕”設置監聽器,添加一條備忘錄
ImageView add = (ImageView) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this,Record.class);
// 給需要傳回數據的頁面取id
startActivityForResult(intent, 1);
}
});
//單擊:打開“修改便簽”頁面
/*為“添加按鈕”通過 setOnClickListener()方法設置監聽器,當點擊該按鈕時,跳轉到添加便簽的界面。當
添加或修改記錄后,記事本首頁的列表需要更新顯示,因此需要調用 startActivityForResult()根據返回結果決
定是否需要更新列表顯示。*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
Bean notepadBean = notelist.get(position);
Intent intent = new Intent(MainActivity.this, Record.class);
intent.putExtra("id", notepadBean.getId());
intent.putExtra("time", notepadBean.getNotepadTime()); //便簽的時間
intent.putExtra("content", notepadBean.getNotepadContent()); //便簽的內容
MainActivity.this.startActivityForResult(intent, 1);//修改備忘錄
}
});
//長按:打開對話框
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int pi) {
Bean notepadBean = notelist.get(position);
//根據id刪除
notepadDAO.deleteData(notepadBean.getId());
notelist.remove(position);
//刷新
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this,"刪除成功",Toast.LENGTH_SHORT).show();
}
};
AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity.this);
builder.setTitle("是否刪除此備忘錄?");
builder.setPositiveButton("確定", listener);
builder.setNegativeButton("取消", null);
builder.show();
return true;
}
});
}
/*當添加或修改返回后:需要刷新首頁的 ListView 的顯示。可以根據 resultCode 重新設置列表的 Adapter,
刷新顯示。*/
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//都為1表示執行跳轉活動且保存數據成功
if (requestCode==1 && resultCode==1)
{ //添加或者更改了備忘錄,需要更新顯示
//showQueryData();
if (notelist!=null)
{
//清除列表
notelist.clear();
}
//重新查詢
notelist = notepadDAO.query();
adapter = new NoteAdapter(this, notelist);
//adapter把數據放進去才會更新,所以上面的if (notelist!=null)……可以不要,重新更新會覆蓋以前的
listView.setAdapter(adapter);
}
}
/*初始化顯示:可以往數據庫(表)中填入一些測試數據,這樣在第一次打開 APP 時就可以看到備忘錄列
表,初始化顯示。方法是:在 NotepadDAO 可以提供 insertData()方法往記事本數據表 Note 中插入若干條
備忘錄記錄,然后在 MainActivity 中初始化 NotepadDAO 后調用該方法。*/
private void initData(){
notepadDAO.insertData("1今天是五一勞動節",notepadDAO.getTime());
notepadDAO.insertData("2今天心情很美麗",notepadDAO.getTime());
notepadDAO.insertData("3今天要帶小烏龜去學校",notepadDAO.getTime());
Log.e("SQLiteHelper","initData func");
}
}
四、相關問題
-
便簽記錄存放的數據庫及表是何時創建的?
DAO層調用helper類
notelist = notepadDAO.query();//第一次調用,創建數據庫
public DAO(Context context) { //創建DBSQLiteHelper對象 dbOpenHelper = new DBSQLiteHelper(context); //dbOpenHelper.getWritableDatabase(); //創建數據庫,可調用,否則在第一次查詢時調用 }
//建表語句 public static final String CREATE_NOTE = "create table Note (" + "id integer primary key autoincrement, " + "content text, " + "notetime text)";
public DBSQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_NOTE); }
-
打開APP時首頁面是如何顯示所有的便簽的?
//從數據庫中查詢數據(保存的所有便簽) notelist = notepadDAO.query();//第一次調用,創建數據庫 adapter = new NoteAdapter(this, notelist); //adapter把數據放進去 listView.setAdapter(adapter);
-
“添加”和“修改”打開活動時有什么不同,是如何處理的?
//根據來源決定頂部顯示"添加便簽"還是"修改便簽" //如果是長按打開顯示"修改便簽",否則顯示"添加便簽" protected void initData() { noteName.setText("添加便簽"); Intent intent = getIntent(); //看intent是否傳回數據 if(intent!= null) { id = intent.getStringExtra("id"); if (id != null) { //"修改便簽",顯示原便簽內容 noteName.setText("修改便簽"); content.setText(intent.getStringExtra("content")); note_time.setText(intent.getStringExtra("time")); //修改顯示時間可見,新增不可見 note_time.setVisibility(View.VISIBLE); } } }
-
如何實現添加便簽和修改便簽后刷新主頁面的顯示?
/*當添加或修改返回后:需要刷新首頁的 ListView 的顯示。可以根據 resultCode 重新設置列表的 Adapter,
刷新顯示。*/
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//都為1表示執行跳轉活動且保存數據成功
if (requestCode==1 && resultCode==1)
{ //添加或者更改了備忘錄,需要更新顯示
//showQueryData();
if (notelist!=null)
{
//清除列表
notelist.clear();
}
//重新查詢
notelist = notepadDAO.query();
adapter = new NoteAdapter(this, notelist);
//adapter把數據放進去才會更新,所以上面的if (notelist!=null)……可以不要,重新更新會覆蓋以前的
listView.setAdapter(adapter);
}
}