title: How to insert data in SQLite and display it using RecyclerView
body: <p>i made RecyclerView to display data from Mysql and it work good now i want to make a favourite button when i clicked on it save this item in a favourite and display it in new activity in RecyclerView </p>
  
  <p>I think i should use Sqlite to save data in it then display in favourite activity but i don't know how i can make that please help me </p>
  
  <p>I tried to make Sqlite </p>
  
  <pre><code>public class DB_Sqlit extends SQLiteOpenHelper {
  
  public static final String BDname = "mdata.db";
  
  public DB_Sqlit(Context context) {
      super(context, BDname, null, 1);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      db.execSQL("create table favorite ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, img TEXT, url TEXT, num TEXT, size TEXT)");
          }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("DROP TABLE IF EXISTS favorite");
      onCreate(db);
  
  }
  
  
  
  public Boolean Insert_to_favorite(String name, String img, String url, String num, String size) {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put("name", name);
      contentValues.put("img", img);
      contentValues.put("url", url);
      contentValues.put("num", num);
      contentValues.put("size", size);
  
  
      long result = db.insert("favorite", null, contentValues);
  
      if (result == -1)
          return false;
      else
          return true;
  
  }
  
  
  public ArrayList getAllList_Favorite() {
      ArrayList&lt;listitem_gib&gt; arraylist = new ArrayList&lt;listitem_gib&gt;();
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor rs = db.rawQuery("select * from favorite", null);
      rs.moveToFirst();
      while (!rs.isAfterLast()) {
          String id = rs.getString(rs.getColumnIndex("id"));
          String name = rs.getString(rs.getColumnIndex("name"));
          String img = rs.getString(rs.getColumnIndex("img"));
          String url = rs.getString(rs.getColumnIndex("url"));
          String num = rs.getString(rs.getColumnIndex("num"));
          String size = rs.getString(rs.getColumnIndex("size"));
  
  
          arraylist.add(new listitem_gib());
          rs.moveToNext();
      }
      return arraylist;
  }
  
  public int get_check_List_Favorite(String Title) {
  
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor rs = db.rawQuery("select * from favorite Where name like '"+ Title +"'", null);
      rs.moveToFirst();
      int count = rs.getCount();
      return count;
  }
  
  
  public Integer Delete(String id) {
      SQLiteDatabase db = this.getWritableDatabase();
      return db.delete("favorite", "id = ?", new String[]{id});
  }}
  </code></pre>
  
  <h2>RecyclerView Activity</h2>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_r_arabic);
  
      GetDataAdapter1 = new ArrayList&lt;&gt;();
  
      recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
  
      progressBar = (ProgressBar) findViewById(R.id.progress_bar);
  
      recyclerView.setHasFixedSize(true);
  
      recyclerViewlayoutManager = new LinearLayoutManager(this);
  
      recyclerView.setLayoutManager(recyclerViewlayoutManager);
  
      progress_layout = (LinearLayout) findViewById(R.id.progress_layout);
      progress_layout.setVisibility(View.VISIBLE);
  
      JSON_DATA_WEB_CALL();
  
  }
  
  public void JSON_DATA_WEB_CALL(){
  
      Intent intent = getIntent();
      story_type = intent.getStringExtra("story_type");
  
  
      String GET_JSON_DATA_HTTP_URL = "http://grassyhat.com/android/" + story_type + ".php";
  
      jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,
  
              new Response.Listener&lt;JSONArray&gt;() {
                  @Override
                  public void onResponse(JSONArray response) {
  
                      progress_layout.setVisibility(View.GONE);
  
                      JSON_PARSE_DATA_AFTER_WEBCALL(response);
                  }
              },
              new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
  
                  }
              });
  
      requestQueue = Volley.newRequestQueue(this);
  
      requestQueue.add(jsonArrayRequest);
  }
  
  public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
  
      for(int i = 0; i&lt;array.length(); i++) {
  
          listitem_gib GetDataAdapter2 = new listitem_gib();
  
          JSONObject json = null;
          try {
              json = array.getJSONObject(i);
  
              GetDataAdapter2.setId(json.getString(id));
  
              GetDataAdapter2.setName(json.getString(name));
  
              GetDataAdapter2.seturl(json.getString(url));
  
              GetDataAdapter2.setimg(json.getString(img));
  
              GetDataAdapter2.setnum(json.getString(num));
  
              GetDataAdapter2.setsize(json.getString(size));
  
          } catch (JSONException e) {
  
              e.printStackTrace();
          }
          GetDataAdapter1.add(GetDataAdapter2);
      }
  
      recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
  
  
      //RecyclerView needs a layout manager in order to display data so here we create one
      StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
  
      //Here we set the layout manager and the adapter to the listview
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setAdapter(recyclerViewadapter);
  
  }
  </code></pre>
  
  <h2>RecyclerViewAdapter</h2>
  
  <pre><code>public class RecyclerViewAdapter extends RecyclerView.Adapter&lt;RecyclerViewAdapter.ViewHolder&gt; {
  
  Context context;
  
  List&lt;listitem_gib&gt; getDataAdapter;
  
  public RecyclerViewAdapter(List&lt;listitem_gib&gt; getDataAdapter, Context context){
  
      super();
  
      this.getDataAdapter = getDataAdapter;
      this.context = context;
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_layout, parent, false);
  
      ViewHolder viewHolder = new ViewHolder(v,context,getDataAdapter);
  
      return viewHolder;
  }
  
  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {
  
      listitem_gib getDataAdapter1 =  getDataAdapter.get(position);
  
      holder.name.setText(getDataAdapter1.getName());
  
      holder.num.setText(getDataAdapter1.getnum());
  
  
      Picasso.with(context).load("http://grassyhat.com/android/image/" + getDataAdapter1.getimg()).into(holder.img1);
  
  
  }
  
  @Override
  public int getItemCount() {
  
      return getDataAdapter.size();
  }
  
  class ViewHolder extends RecyclerView.ViewHolder
              implements View.OnClickListener{
  
  
      public TextView name;
      public TextView num;
      public ImageView img1;
  
      Context context;
      List&lt;listitem_gib&gt; getDataAdapter;
  
  
  
      public ViewHolder(View itemView, Context context ,List&lt;listitem_gib&gt; getDataAdapter ) {
  
          super(itemView);
          itemView.setOnClickListener(this);
          this.getDataAdapter = getDataAdapter;
          this.context = context;
  
          name = (TextView) itemView.findViewById(R.id.Main_Text) ;
          num = (TextView) itemView.findViewById(R.id.Second_Text) ;
          img1 = (ImageView) itemView.findViewById(R.id.img1) ;
  
  
      }
  
      @Override
      public void onClick(View v) {
  
          int position = getAdapterPosition();
          listitem_gib getDataAdapter =this.getDataAdapter.get(position);
          Intent intent = new Intent(this.context,Rewaya_info.class);
          intent.putExtra("name",getDataAdapter.getName());
          intent.putExtra("url",getDataAdapter.geturl());
          intent.putExtra("img",getDataAdapter.getimg());
          intent.putExtra("num",getDataAdapter.getnum());
          intent.putExtra("size",getDataAdapter.getsize());
          this.context.startActivity(intent);
  
      }
  }}
  </code></pre>
  
  <h2>ListItem</h2>
  
  <pre><code>public class listitem_gib {
  
  String id;
  String name;
  String url;
  String img;
  String num;
  String size;
  
  
  public String getName() {
  
      return name;
  }
  
  public void setName(String name) {
  
      this.name = name;
  }
  
  public String getid() {
  
      return id;
  }
  
  public void setId(String id1) {
  
      this.id = id1;
  }
  
  
  public String geturl() {
  
      return url;
  }
  
  public void seturl(String url1) {
  
      this.url = url1;
  }
  
  public String getimg() {
  
      return img;
  }
  
  public void setimg(String img1) {
  
      this.img = img1;
  }
  
  public String getnum() {
  
      return num;
  }
  
  public void setnum(String num1) {
  
      this.num = num1;
  }
  public String getsize() {
  
      return size;
  }
  
  public void setsize(String size1) {
  
      this.size = size1;
  }}
  </code></pre>