android开发,在main_activity调用room数据库填充RecyclerView的Java代码
时间: 2023-12-03 10:46:16 浏览: 203
首先,在你的项目中需要配置 Room Persistence Library,具体可以参考官方文档。
接着,你需要创建一个数据实体类,用于定义存储在数据库中的数据结构,例如:
```java
@Entity(tableName = "notes")
public class Note {
@PrimaryKey(autoGenerate = true)
private int id;
private String title;
private String content;
public Note(String title, String content) {
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;
}
}
```
然后,你需要创建一个 DAO(Data Access Object)接口,用于定义对数据进行 CRUD(Create、Read、Update、Delete)操作的方法,例如:
```java
@Dao
public interface NoteDao {
@Query("SELECT * FROM notes")
List<Note> getAll();
@Insert
void insertAll(Note... notes);
@Delete
void delete(Note note);
@Update
void update(Note note);
}
```
接下来,在你的 Activity 中,你需要创建一个 RecyclerView 和一个 RecyclerView.Adapter,并将它们连接起来。
在 Adapter 中,你需要定义 ViewHolder 类,并实现 onCreateViewHolder、onBindViewHolder 和 getItemCount 方法,例如:
```java
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteViewHolder> {
private List<Note> notes;
public NoteAdapter(List<Note> notes) {
this.notes = notes;
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_note, parent, false);
return new NoteViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
Note note = notes.get(position);
holder.titleTextView.setText(note.getTitle());
holder.contentTextView.setText(note.getContent());
}
@Override
public int getItemCount() {
return notes.size();
}
static class NoteViewHolder extends RecyclerView.ViewHolder {
TextView titleTextView;
TextView contentTextView;
NoteViewHolder(View itemView) {
super(itemView);
titleTextView = itemView.findViewById(R.id.titleTextView);
contentTextView = itemView.findViewById(R.id.contentTextView);
}
}
}
```
最后,在 Activity 中,你需要获取 Room 数据库实例,并调用 DAO 中定义的方法来获取数据,并将数据传递给 RecyclerView.Adapter,例如:
```java
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private NoteAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new NoteAdapter(new ArrayList<Note>());
recyclerView.setAdapter(adapter);
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").build();
new AsyncTask<Void, Void, List<Note>>() {
@Override
protected List<Note> doInBackground(Void... voids) {
return db.noteDao().getAll();
}
@Override
protected void onPostExecute(List<Note> notes) {
super.onPostExecute(notes);
adapter = new NoteAdapter(notes);
recyclerView.setAdapter(adapter);
}
}.execute();
}
}
```
以上就是在 MainActivity 中调用 Room 数据库填充 RecyclerView 的 Java 代码。注意,为了避免在主线程中进行数据库操作而导致应用卡顿,我们使用了 AsyncTask 来进行异步操作。
阅读全文