Android sqlite recyclerview 完整示例
时间: 2023-08-05 17:45:55 浏览: 211
android sqlite 示例
以下是一个简单的 Android SQLite RecyclerView 完整示例:
1. 创建一个名为 "Note" 的 Java 类表示笔记:
```java
public class Note {
private int id;
private String title;
private String content;
public Note() {}
public Note(int id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
```
2. 创建一个名为 "NoteDatabaseHelper" 的类用于创建和管理 SQLite 数据库:
```java
public 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 COLUMN_ID = "id";
private static final String COLUMN_TITLE = "title";
private static final String COLUMN_CONTENT = "content";
public NoteDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_TITLE + " TEXT,"
+ COLUMN_CONTENT + " TEXT" + ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void addNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, note.getTitle());
values.put(COLUMN_CONTENT, note.getContent());
db.insert(TABLE_NAME, null, values);
db.close();
}
public List<Note> getAllNotes() {
List<Note> notesList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Note note = new Note();
note.setId(cursor.getInt(0));
note.setTitle(cursor.getString(1));
note.setContent(cursor.getString(2));
notesList.add(note);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return notesList;
}
public void updateNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, note.getTitle());
values.put(COLUMN_CONTENT, note.getContent());
db.update(TABLE_NAME, values, COLUMN_ID + " = ?",
new String[] { String.valueOf(note.getId()) });
db.close();
}
public void deleteNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, COLUMN_ID + " = ?",
new String[] { String.valueOf(note.getId()) });
db.close();
}
}
```
3. 创建一个名为 "NoteAdapter" 的类来显示笔记列表:
```java
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.MyViewHolder> {
private List<Note> notesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, content;
public MyViewHolder(View view) {
super(view);
title = view.findViewById(R.id.title);
content = view.findViewById(R.id.content);
}
}
public NoteAdapter(List<Note> notesList) {
this.notesList = notesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Note note = notesList.get(position);
holder.title.setText(note.getTitle());
holder.content.setText(note.getContent());
}
@Override
public int getItemCount() {
return notesList.size();
}
}
```
4. 创建一个名为 "MainActivity" 的 Activity,显示 RecyclerView:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NoteAdapter noteAdapter;
private List<Note> notesList = new ArrayList<>();
private NoteDatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
databaseHelper = new NoteDatabaseHelper(this);
notesList.addAll(databaseHelper.getAllNotes());
noteAdapter = new NoteAdapter(notesList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(noteAdapter);
}
@Override
protected void onResume() {
super.onResume();
notesList.clear();
notesList.addAll(databaseHelper.getAllNotes());
noteAdapter.notifyDataSetChanged();
}
public void addNewNote() {
Note note = new Note();
note.setTitle("Title");
note.setContent("Content");
databaseHelper.addNote(note);
notesList.add(note);
noteAdapter.notifyItemInserted(notesList.size() - 1);
}
}
```
5. 创建一个名为 "activity_main.xml" 的布局文件来显示 RecyclerView:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:clipToPadding="false" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:clickable="true"
android:src="@drawable/ic_add_white_24dp" />
</RelativeLayout>
```
6. 创建一个名为 "note_list_row.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="wrap_content"
android:padding="8dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:textSize="14sp"
android:textColor="@android:color/darker_gray" />
</RelativeLayout>
```
7. 在 MainActivity 中添加一个 FloatingActionButton 来添加新笔记:
```java
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNewNote();
}
});
```
这是一个简单的 Android SQLite RecyclerView 完整示例,它可以让你了解如何在 Android 应用中使用 SQLite 和 RecyclerView。
阅读全文