Search code examples
androidandroid-sqliteandroid-recyclerview

How to display data from SQLite Database into RecyclerView


I want to display data from my SQLite Database to my RecyclerView (with LinearLayoutManager). I created the RecyclerView layout for the list and a CardView layout for the single item. I created the database with Recipe table and I can save a new Recipe. My goal is to show the title of Recipe inside the CardView.

Recipe class

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;
}

(plus setter/getter method)

Inside the DatabaseHelper, I created a method to get all the Recipes adding to a List:

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;
}

This is my Adapter class with ViewHolder class for the RecyclerView:

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

Finally, I declared RecyclerView, List, Adapter, etc in the MainActivity, where I created a method to display all 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();
}

There's no errors but it doesn't work.


Solution

  • make sure you have passed the arrayList into adapter and set adapter to recyclerView.