这是一个用sqlite存储的简单的记事本,主要实现数据的增加和删除功能。
- xml样式
主界面 mainlayout.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" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="40dp">
<EditText
android:id="@+id/edcontent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="top"
android:hint=" 好记性不如烂笔头 "
android:textColorHint="#ff22ff"/>
<Button
android:id="@+id/addbtn"
android:layout_width="80dp"
android:layout_height="match_parent"
android:text="add"/>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
listview样式 celllayout.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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
</LinearLayout>
- java代码
DB操作NotesDB.java
package note;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class NotesDB extends SQLiteOpenHelper{
public static final String TABLE_NAME = "notes";
public static final String CONTENT = "content";
public static final String ID = "_id";
public static final String TIME = "time";
public NotesDB(Context context) {
super(context, "notes", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," + CONTENT
+ " TEXT NOT NULL," + TIME + " TEXT NOT NULL)");
}
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
设置listview适配器MyAdapter.java
package note;
import com.example.note.R;
import com.example.note.R.layout;
import android.R.string;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter{
private Context context;
private Cursor cursor;
private LinearLayout Layout;
public MyAdapter( Context context, Cursor cursor){
this.context=context;
this.cursor=cursor;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return cursor.getCount();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return cursor.getPosition();
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater =LayoutInflater.from(context);
Layout =(LinearLayout) inflater.inflate(R.layout.celllayout, null);
TextView contenttv=(TextView) Layout.findViewById(R.id.content);
TextView timetv= (TextView) Layout.findViewById(R.id.time);
cursor.moveToPosition(arg0);
String content= cursor.getString(cursor.getColumnIndex("content"));
String time= cursor.getString(cursor.getColumnIndex("time"));
contenttv.setText(content);
timetv.setText(time);
return Layout;
}
}
主界面MianActivity.java
import java.text.SimpleDateFormat;
import java.util.Date;
import com.example.note.R;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MianActivity extends Activity implements OnClickListener{
private NotesDB notesDB;
private SQLiteDatabase dbwrite=null;
private SQLiteDatabase dbReader=null;
private EditText ed;
private Button addbtn;
private ListView lv;
private MyAdapter adapter;
private Cursor mcursor ;
private int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
notesDB=new NotesDB(this);
dbwrite =notesDB.getWritableDatabase();
lv=(ListView) findViewById(R.id.listview);
ed= (EditText) findViewById(R.id.edcontent);
addbtn=(Button) findViewById(R.id.addbtn);
addbtn.setOnClickListener(this);
dbReader=notesDB.getReadableDatabase();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
mcursor=dbReader.query(NotesDB.TABLE_NAME, null, null, null, null, null, null);
mcursor.moveToPosition(arg2);
id = mcursor.getInt(0);
dbwrite.delete(NotesDB.TABLE_NAME,
"_id=" + id, null);
selectDB();
}
});
}
public void addDb(){
ContentValues cv = new ContentValues();
cv.put(NotesDB.CONTENT, ed.getText().toString());
cv.put(NotesDB.TIME, getTime());
dbwrite.insert(NotesDB.TABLE_NAME, null, cv);
ed.setText("");
}
private String getTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date();
String str = format.format(curDate);
return str;
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.addbtn:
if (ed.getText()==null){}
else{
addDb();}
break;
default:
break;
}
selectDB();
}
public void selectDB(){
Cursor cursor=dbReader.query(NotesDB.TABLE_NAME, null, null, null, null, null, null);
adapter=new MyAdapter(this, cursor);
lv.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
selectDB();
}
}