首页 > 解决方案 > 如何将 SQLite 数据库中的数据显示到 RecyclerView

问题描述

我想将我的 SQLite 数据库中的数据显示到我的 RecyclerView(使用 LinearLayoutManager)。我为列表创建了 RecyclerView 布局,并为单个项目创建了 CardView 布局。我用配方表创建了数据库,我可以保存一个新的配方。我的目标是在 CardView 中显示食谱的标题。

食谱类

public class Recipe {

private int id;
private String title;
private String photo;
private String instructions;
private int targetPeople;
private int time;



public Recipe() {

    this.id = id;
    this.title = title;
    this.photo = photo;
    this.instructions = instructions;
    this.targetPeople = targetPeople;
    this.time = time;
}

(加上 setter/getter 方法)

在 DatabaseHelper 中,我创建了一个方法来将所有食谱添加到列表中:

public List<Recipe> getAllRecipes() {

    // sorting orders
    String sortOrder =
            RECIPE_TITLE + " ASC";
    List<Recipe> recipeList = new ArrayList<Recipe>();

    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery("SELECT * FROM " + TBL_RECIPE, null);

    // Traversing through all rows and adding to list
    if (cursor.moveToFirst()) {

        do {

            Recipe recipe = new Recipe();
            recipe.setTitle(cursor.getString(cursor.getColumnIndex("TITLE")));

            // Adding user record to list
            recipeList.add(recipe);
        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    // return user list
    return recipeList;
}

这是我的适配器类和 RecyclerView 的 ViewHolder 类:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private Context mContext;
private List<Recipe> recipeList;

//constructor of adapter
public MyAdapter(Context mContext, List<Recipe> recipeList) {
    this.mContext = mContext;
    this.recipeList = recipeList;

}

//ViewHolder Class
public class ViewHolder extends RecyclerView.ViewHolder {

    public final TextView textViewTitle;

    //constructor for ViewHolder
    public ViewHolder(View itemView) {
        super(itemView);
        textViewTitle = itemView.findViewById(R.id.descriptionView);
    }

}

//return an instance of ViewHolder Class
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {

    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_card, parent, false);

    return new ViewHolder(itemView);
}

//binding data to our ViewHolder
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {

    // retrieve UI elements inside viewHolder object

    viewHolder.textViewTitle.setText(recipeList.get(position).getTitle());

}

//return the size of the list
@Override
public int getItemCount() {
    return recipeList.size();
}
}

最后,我在 MainActivity 中声明了 RecyclerView、List、Adapter 等,在这里我创建了一个显示所有 Recipes 的方法:

    @SuppressLint("StaticFieldLeak")
private void displayAllRecipes() {
    // AsyncTask is used that SQLite operation does not block the UI Thread.
    new AsyncTask<Void, Void, ArrayList<Recipe>>() {
        @Override
        protected ArrayList<Recipe> doInBackground(Void... params) {

            recipeList.clear();
            recipeList.addAll(dbHelper. getAllRecipes());
            return recipeList;
        }

        @Override
        protected void onPostExecute(ArrayList<Recipe> aVoid) {
            super.onPostExecute(aVoid);
            resultAdapter.notifyDataSetChanged();
        }
    }.execute();
}

没有错误,但它不起作用。

标签: androidandroid-sqliteandroid-recyclerview

解决方案


确保您已将 arrayList 传递给适配器并将适配器设置为 recyclerView。


推荐阅读