如何将数据从SQLite数据库显示到RecyclerView中

时间:2019-03-14 10:16:27

标签: android android-recyclerview android-sqlite recycler-adapter

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

食谱类别

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类的Adapter类:

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等, 在这里我创建了一种显示所有食谱的方法:

    @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();
}

没有错误,但是没有用。

1 个答案:

答案 0 :(得分:1)

确保已将arrayList传递到适配器中,并将适配器设置为recyclerView。