安卓備忘錄,資料增刪改查
1AddActivity.java
package com.example.administrator.myapplication_s;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddActivity extends Activity {
SQLiteOpenHelper sqLiteDbHelper;
Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tianjia);
sqLiteDbHelper = new SQLiteDbHelper(this, "contact.db", null, 1);
saveButton = (Button) findViewById(R.id.button1);
saveButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = sqLiteDbHelper.getWritableDatabase();
MessageDao messageDao = new MessageDao(db);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd");// HH:mm:ss
Date date = new Date(System.currentTimeMillis());
EditText txtTitle = (EditText) findViewById(R.id.EditText1);
EditText txtContext = (EditText) findViewById(R.id.EditText2);
String dates = simpleDateFormat.format(date);
String title = txtTitle.getText().toString();
String context = txtContext.getText().toString();
Message message = new Message();
message.setDate(dates);
message.setContent(context);
message.setTitle(title);
messageDao.save(message);
Toast.makeText(AddActivity.this, "資料庫儲存成功!", Toast.LENGTH_LONG).show();
Intent intent = new Intent( AddActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
2 MainActivity.java
package com.example.administrator.myapplication_s;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity {
Button AddButton;
SQLiteDbHelper sqLiteDbHelper;
ListView listview;
static SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button AddButton=(Button)findViewById(R.id.button1);
AddButton.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,AddActivity.class);
startActivity(intent);
}
});
sqLiteDbHelper=new SQLiteDbHelper(this,"contact.db",null,1);
SQLiteDatabase db = sqLiteDbHelper.getWritableDatabase();
MessageDao messageDao=new MessageDao(db);
List<Message> list= messageDao.query();
int m = list.size();
Message message;
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < m; i++) {
message = list.get(i);
Map<String, Object> map = new HashMap<String, Object>();
map.put("id",message.getId());
map.put("date", message.getDate());
map.put("title", message.getTitle());
map.put("content", message.getContent());
data.add(map);
}
listview = (ListView) findViewById(R.id.listview_note);
//給列表增加內容 利用message_layout模板。
adapter = new SimpleAdapter(MainActivity.this, data, R.layout.message, new String[]{"id","date", "title","content"}, new int[]{R.id.msg_id,R.id.msg_date, R.id.msg_title});
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView Id=(TextView)view.findViewById(R.id.msg_id);
String strid=Id.getText().toString();
Intent intent=new Intent(MainActivity.this,XsActivity.class);
intent.putExtra("id",strid);
startActivity(intent);
}
});
}
}
3 Message.java
package com.example.administrator.myapplication_s;
public class Message {
private int id;
private String title;
private String content;
private String date;
public Message() {
}
public Message(int id, String date,String title, String content) {
this.id = id;
this.date = date;
this.title = title;
this.content = content;
}
public Message( String date,String title, String content) {
this.date = date;
this.title = title;
this.content = content;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public String getDate() {
return date;
}
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public void setDate(String date) {
this.date = date;
}
}
4 MessageDao.java
package com.example.administrator.myapplication_s;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class MessageDao{
SQLiteDatabase db;
public MessageDao(SQLiteDatabase db) {
this.db = db;
}
public boolean save(Object objects) {
try {
Message message = (Message) objects;
ContentValues values = new ContentValues();
values.put("date",String.valueOf(message.getDate()));
values.put("title", message.getTitle());
values.put("content", message.getContent());
long index = db.insert("T_Message", null, values);
if (index > 0)
return true;
else {
return false;
}
} catch (Exception e) {
return false;
}
}
public boolean update(Object objects) {
try {
Message message = (Message) objects;
ContentValues values = new ContentValues();
values.put("date",String.valueOf(message.getDate()));
values.put("title", message.getTitle());
values.put("content", message.getContent());
long index = db.update("T_Message", values, "id=?", new String[]{String.valueOf(message.getId())});
if (index > 0)
return true;
else {
return false;
}
} catch (Exception e) {
return false;
}
}
public boolean delete(String id) {
try {
int index = db.delete("T_Message", "id=?", new String[]{id});
if (index > 0)
return true;
else {
return false;
}
} catch (Exception e) {
return false;
}
}
public List query() {
Cursor cursor = db.rawQuery("select * from T_Message",new String[]{});
//Cursor cursor=db.query(false,"T_Message",new String[]{"id","date","title","content"},null,null,null,null,null,null);
List<Message> list =new ArrayList<Message>();
while(cursor.moveToNext()){
Message message = new Message(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));
list.add(message);
}
return list;
}
public List visit(String id_query) {
Cursor cursor = db.rawQuery("select * from T_Message where id = ?",new String[]{id_query});
//Cursor cursor=db.query("T_Message",new String[]{"id","date","title","content"},"id=?",new String[]{id_query},null,null,null);
List<Message> list =new ArrayList<Message>();
while(cursor.moveToNext()){
Message message = new Message(cursor.getInt(0),cursor.getString(1),cursor.getString(2),cursor.getString(3));
list.add(message);
}
return list;
}
}
5 SQLiteDbHelper.java
package com.example.administrator.myapplication_s;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLiteDbHelper extends SQLiteOpenHelper {
private String CREATE_CONTACT_TABLE="CREATE TABLE T_Message(id INTEGER PRIMARY KEY," +
"date VARCHAR(50),title VARCHAR(50),content VARCHAR(200))";
public SQLiteDbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_CONTACT_TABLE);//完成建表
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
6 XsActivity.java
package com.example.administrator.myapplication_s;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.List;
public class XsActivity extends Activity {
SQLiteDbHelper sqLiteDbHelper;
int id_query;
MessageDao messageDao;
EditText detail_title;
EditText detail_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xianshi);
sqLiteDbHelper=new SQLiteDbHelper(this,"contact.db",null,1);
Intent intent=getIntent();
String id=intent.getStringExtra("id");
id_query=Integer.parseInt(id);
SQLiteDatabase db = sqLiteDbHelper.getWritableDatabase();
messageDao=new MessageDao(db);
List<Message> list=messageDao.visit(id);
detail_title=(EditText)this.findViewById(R.id.EditText1);
detail_title.setText(list.get(0).getTitle());
detail_content=(EditText)this.findViewById(R.id.EditText2);
detail_content.setText(list.get(0).getContent());
}
//返回按鈕
public void btnReturn_detail(View view){
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}
一 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="@drawable/drawable"
tools:context="com.example.administrator.myapplication_s.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="備忘錄"
android:background="#abb41e8c"
android:textSize="20dp"
android:gravity="center"
android:textColor="#fff"
android:id="@+id/textView" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ListView
android:id="@+id/listview_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:text="新建"
android:textSize="20dp"
android:id="@+id/button1"/>
</RelativeLayout>
二 message.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:background="@drawable/drawable"
android:orientation="vertical">
<LinearLayout
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/msg_id"
android:layout_width="300dp"
android:layout_height="match_parent"
android:visibility="gone"
android:height="50dp"/>
<TextView
android:id="@+id/msg_date"
android:layout_width="150dp"
android:layout_height="match_parent"
android:gravity="center|left"
android:textSize="15dp"
android:height="50dp"/>
<TextView
android:id="@+id/msg_title"
android:layout_width="150dp"
android:layout_height="match_parent"
android:gravity="center|left"
android:textSize="15dp"
android:height="50dp"/>
</LinearLayout>
</LinearLayout>
三 tianjia.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="@drawable/drawable"
tools:context="com.example.administrator.myapplication_s.MainActivity"
>
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="新增備忘錄"
android:gravity="center"
android:textSize="20dp"
android:background="@color/colorPrimary"
android:textColor="#ffffff"
/>
<EditText
android:id="@+id/EditText1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/TextView1"
android:hint="主題"
android:layout_centerHorizontal="true"
android:background="@drawable/drawable"/>
<EditText
android:id="@+id/EditText2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:hint="內容"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/EditText1"
android:layout_alignStart="@+id/EditText1"
android:background="@drawable/drawable"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="完成"
android:textSize="20dp"/>
</RelativeLayout>
四 xianshi.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="@drawable/drawable"
tools:context="com.example.administrator.myapplication_s.MainActivity">
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="檢視備忘錄"
android:gravity="center"
android:textSize="20dp"
android:background="@color/colorPrimary"
android:textColor="#fff"/>
<EditText
android:id="@+id/EditText1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/TextView1"
android:hint="顯示主題"
android:layout_centerHorizontal="true"
android:background="@drawable/drawable"/>
<EditText
android:id="@+id/EditText2"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="20dp"
android:layout_below="@+id/EditText1"
android:hint="顯示內容"
android:layout_centerHorizontal="true"
android:background="@drawable/drawable"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnReturn_detail"
android:layout_alignParentBottom="true"
android:text="返回"
android:textSize="20dp"/>
</RelativeLayout>
五
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.myapplication_s">
<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="@android:style/Theme.NoTitleBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddActivity">
</activity>
<activity android:name=".XsActivity">
</activity>
</application>
</manifest>