title: How to display data from SQLite Database into RecyclerView
body: <p>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.</p>
  
  <p><strong>Recipe class</strong></p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>(plus setter/getter method)</p>
  
  <p>Inside the DatabaseHelper, I created a method to get all the Recipes adding to a List:</p>
  
  <pre><code>public List&lt;Recipe&gt; getAllRecipes() {
  
      // sorting orders
      String sortOrder =
              RECIPE_TITLE + " ASC";
      List&lt;Recipe&gt; recipeList = new ArrayList&lt;Recipe&gt;();
  
      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;
  }
  </code></pre>
  
  <p>This is my Adapter class with ViewHolder class for the RecyclerView:</p>
  
  <pre><code>public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  
  private Context mContext;
  private List&lt;Recipe&gt; recipeList;
  
  //constructor of adapter
  public MyAdapter(Context mContext, List&lt;Recipe&gt; 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();
  }
  }
  </code></pre>
  
  <p>Finally, I declared RecyclerView, List, Adapter, etc in the MainActivity,
  where I created a method to display all Recipes:</p>
  
  <pre><code>    @SuppressLint("StaticFieldLeak")
  private void displayAllRecipes() {
      // AsyncTask is used that SQLite operation does not block the UI Thread.
      new AsyncTask&lt;Void, Void, ArrayList&lt;Recipe&gt;&gt;() {
          @Override
          protected ArrayList&lt;Recipe&gt; doInBackground(Void... params) {
  
              recipeList.clear();
              recipeList.addAll(dbHelper. getAllRecipes());
              return recipeList;
          }
  
          @Override
          protected void onPostExecute(ArrayList&lt;Recipe&gt; aVoid) {
              super.onPostExecute(aVoid);
              resultAdapter.notifyDataSetChanged();
          }
      }.execute();
  }
  </code></pre>
  
  <p>There's no errors but it doesn't work.</p>