用android中的sqllite数据库写一个关于古诗的增、删、查、改功能的笔记本功能,请写一个详细的包括布局文件和java文件的示例
时间: 2024-02-11 21:04:03 浏览: 87
好的,以下是一个简单的示例,实现了关于古诗的增、删、查、改功能的笔记本功能。
1. 布局文件
在布局文件中,我们需要添加一些控件,包括输入框、按钮等。
activity_main.xml:
```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">
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="古诗笔记本"
android:textSize="24sp"
android:gravity="center"/>
<EditText
android:id="@+id/edit_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入标题"/>
<EditText
android:id="@+id/edit_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"/>
<Button
android:id="@+id/btn_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
```
2. Java文件
在Java文件中,我们需要定义一些变量和方法,包括数据库的创建和更新、增删查改等操作。
MainActivity.java:
```java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText mEditTitle;
private EditText mEditContent;
private Button mBtnAdd;
private Button mBtnQuery;
private Button mBtnUpdate;
private Button mBtnDelete;
private ListView mListView;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
mEditTitle = findViewById(R.id.edit_title);
mEditContent = findViewById(R.id.edit_content);
mBtnAdd = findViewById(R.id.btn_add);
mBtnQuery = findViewById(R.id.btn_query);
mBtnUpdate = findViewById(R.id.btn_update);
mBtnDelete = findViewById(R.id.btn_delete);
mListView = findViewById(R.id.listview);
// 创建或打开数据库
mDatabase = new NoteDatabaseHelper(this).getWritableDatabase();
// 添加按钮的点击事件
mBtnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = mEditTitle.getText().toString();
String content = mEditContent.getText().toString();
if (title.equals("") || content.equals("")) {
Toast.makeText(MainActivity.this, "请输入标题和内容", Toast.LENGTH_SHORT).show();
} else {
ContentValues values = new ContentValues();
values.put(NoteDatabaseHelper.TITLE, title);
values.put(NoteDatabaseHelper.CONTENT, content);
mDatabase.insert(NoteDatabaseHelper.TABLE_NAME, null, values);
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
mEditTitle.setText("");
mEditContent.setText("");
}
}
});
// 查询按钮的点击事件
mBtnQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = mDatabase.query(NoteDatabaseHelper.TABLE_NAME, null, null, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this,
R.layout.note_list_item,
cursor,
new String[]{NoteDatabaseHelper.TITLE, NoteDatabaseHelper.CONTENT},
new int[]{R.id.text_title, R.id.text_content},
0);
mListView.setAdapter(adapter);
}
});
// 列表项的点击事件
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
String title = cursor.getString(cursor.getColumnIndex(NoteDatabaseHelper.TITLE));
String content = cursor.getString(cursor.getColumnIndex(NoteDatabaseHelper.CONTENT));
mEditTitle.setText(title);
mEditContent.setText(content);
}
});
// 修改按钮的点击事件
mBtnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = mEditTitle.getText().toString();
String content = mEditContent.getText().toString();
if (title.equals("") || content.equals("")) {
Toast.makeText(MainActivity.this, "请选择要修改的笔记", Toast.LENGTH_SHORT).show();
} else {
ContentValues values = new ContentValues();
values.put(NoteDatabaseHelper.TITLE, title);
values.put(NoteDatabaseHelper.CONTENT, content);
mDatabase.update(NoteDatabaseHelper.TABLE_NAME, values, NoteDatabaseHelper.TITLE + "=?", new String[]{title});
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
mEditTitle.setText("");
mEditContent.setText("");
}
}
});
// 删除按钮的点击事件
mBtnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = mEditTitle.getText().toString();
if (title.equals("")) {
Toast.makeText(MainActivity.this, "请选择要删除的笔记", Toast.LENGTH_SHORT).show();
} else {
mDatabase.delete(NoteDatabaseHelper.TABLE_NAME, NoteDatabaseHelper.TITLE + "=?", new String[]{title});
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
mEditTitle.setText("");
mEditContent.setText("");
}
}
});
}
// 数据库帮助类
private static class NoteDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "notes.db"; // 数据库名称
private static final int DATABASE_VERSION = 1; // 数据库版本号
private static final String TABLE_NAME = "notes"; // 表名称
private static final String ID = "_id"; // 主键
private static final String TITLE = "title"; // 标题
private static final String CONTENT = "content"; // 内容
NoteDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
TITLE + " TEXT, " +
CONTENT + " TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
```
3. 列表项布局文件
在列表项布局文件中,我们需要添加一些控件,用于显示笔记的标题和内容。
note_list_item.xml:
```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="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="@color/colorPrimary"/>
<TextView
android:id="@+id/text_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"/>
</LinearLayout>
```
以上就是关于古诗的增、删、查、改功能的笔记本功能的示例,希望能对你有所帮助。
阅读全文