用android中的sqllite数据库写一个关于古诗的增、删、查、改功能的笔记本项目,请写一个详细的包括布局文件和java文件的示例
时间: 2024-02-11 17:04:31 浏览: 85
好的,下面是一个简单的示例,实现了古诗的增、删、查、改功能。其中,布局文件为activity_main.xml,Java文件为MainActivity.java。
activity_main.xml:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入标题"
android:layout_marginTop="20dp"
android:padding="10dp"/>
<EditText
android:id="@+id/edit_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:layout_below="@id/edit_title"
android:padding="10dp"/>
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:layout_below="@id/edit_content"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:padding="10dp"/>
<Button
android:id="@+id/btn_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
android:layout_below="@id/edit_content"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/btn_add"
android:padding="10dp"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:layout_below="@id/edit_content"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/btn_query"
android:padding="10dp"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:layout_below="@id/edit_content"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/btn_update"
android:padding="10dp"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/btn_add"
android:layout_marginTop="20dp"/>
</RelativeLayout>
```
MainActivity.java:
```java
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText editTitle;
private EditText editContent;
private Button btnAdd;
private Button btnQuery;
private Button btnUpdate;
private Button btnDelete;
private ListView listView;
private MyDatabaseHelper dbHelper;
private SQLiteDatabase db;
private List<String> dataList = new ArrayList<>();
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTitle = findViewById(R.id.edit_title);
editContent = findViewById(R.id.edit_content);
btnAdd = findViewById(R.id.btn_add);
btnQuery = findViewById(R.id.btn_query);
btnUpdate = findViewById(R.id.btn_update);
btnDelete = findViewById(R.id.btn_delete);
listView = findViewById(R.id.listview);
dbHelper = new MyDatabaseHelper(this, "poetry.db", null, 1);
db = dbHelper.getWritableDatabase();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
listView.setAdapter(adapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = editTitle.getText().toString();
String content = editContent.getText().toString();
if (title.isEmpty() || content.isEmpty()) {
Toast.makeText(MainActivity.this, "标题或内容不能为空", Toast.LENGTH_SHORT).show();
} else {
ContentValues values = new ContentValues();
values.put("title", title);
values.put("content", content);
db.insert("Poetry", null, values);
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}
}
});
btnQuery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = db.query("Poetry", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
dataList.clear();
do {
String title = cursor.getString(cursor.getColumnIndex("title"));
String content = cursor.getString(cursor.getColumnIndex("content"));
dataList.add(title + "\n" + content);
} while (cursor.moveToNext());
adapter.notifyDataSetChanged();
}
cursor.close();
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = editTitle.getText().toString();
String content = editContent.getText().toString();
if (title.isEmpty() || content.isEmpty()) {
Toast.makeText(MainActivity.this, "标题或内容不能为空", Toast.LENGTH_SHORT).show();
} else {
ContentValues values = new ContentValues();
values.put("content", content);
db.update("Poetry", values, "title = ?", new String[] {title});
Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
}
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = editTitle.getText().toString();
if (title.isEmpty()) {
Toast.makeText(MainActivity.this, "标题不能为空", Toast.LENGTH_SHORT).show();
} else {
db.delete("Poetry", "title = ?", new String[] {title});
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
}
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = dataList.get(position);
String[] arr = item.split("\n");
editTitle.setText(arr[0]);
editContent.setText(arr[1]);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
db.close();
}
}
```
其中,MyDatabaseHelper是一个继承自SQLiteOpenHelper的类,用于创建和升级数据库。
阅读全文