How do I attach RecyclerView adapter from sqlite database

irwan dwiyanto picture irwan dwiyanto · Mar 25, 2017 · Viewed 7.7k times · Source

I've tried to find a tutorial how to make recycleview with sqlite but I am a little less understood his steps. is there who can help me to solve the following problem. create a data adapter recycleview of sqlite.

RecyclerViewHolder.java

package com.my.app;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class RecyclerViewHolder extends RecyclerView.ViewHolder {
    TextView tv1;
    ImageView imageView;

    public RecyclerViewHolder(View itemView) {
        super(itemView);
        tv1 =(TextView)itemView.findViewById(R.id.data_kategori);
        imageView=(ImageView)itemView.findViewById(R.id.list_kategori);
    }
}

RecycleAdapter.java

package com.my.app;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;


public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.myViewHolder> {
    public ArrayList<String> myValues;


    public RecycleAdapter (ArrayList<String> myValues){
        this.myValues = myValues;
    }

    @Override
    public RecycleAdapter.myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View listitem =LayoutInflater.from(parent.getContext()).inflate(R.layout.list_kategori, parent, false);
        return new myViewHolder(listitem);
    }

    @Override
    public void onBindViewHolder(RecycleAdapter.myViewHolder holder, int position) {
        holder.data_nm.setText(myValues.get(position));
    }

    @Override
    public int getItemCount() {
        return myValues.size();
    }

    public  static class  myViewHolder extends RecyclerView.ViewHolder {
        private TextView data_nm;
        public myViewHolder (View itemView) {
            super(itemView);
            data_nm =(TextView)itemView.findViewById(R.id.data_kategori);
        }
    }
}

DbHelper.java

package com.my.app;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {

        // Database Information
        static final String DB_NAME = "SavePassword";

        // database version
        static final int DB_VERSION = 1;

        // Table Name
        public static final String kategori_tb = "tb_kategori";

        // Table columns
        public static final String kategori_id = "_id";
        public static final String kategori_nm = "nm";

        // Creating table query
        private static final String data_kategori = "create table " + kategori_tb + "(" + kategori_id
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + kategori_nm + " TEXT NOT NULL);";

        public DbHelper(Context context) {
                super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
                db.execSQL(data_kategori);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                db.execSQL("DROP TABLE IF EXISTS " + data_kategori);
                onCreate(db);
        }

}


**DbManager.java**

    package com.my.app;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;

    public class DbManager {

        private DbHelper dbHelper;

        private Context context;

        private SQLiteDatabase database;

        public DbManager(Context c) {
            context = c;
        }

        public DbManager open() throws SQLException {
            dbHelper = new DbHelper(context);
            database = dbHelper.getWritableDatabase();
            return this;
        }

        public void close() {
            dbHelper.close();
        }

        public Cursor fetch_data() {
            String[] columns = new String[] { DbHelper.kategori_id, DbHelper.kategori_nm };
            Cursor cursor = database.query(DbHelper.kategori_tb, columns, null, null, null, null, null);
            if (cursor != null) {
                cursor.moveToFirst();
            }
            return cursor;
        }

        public void insert_data(String nm_kategori) {
            ContentValues value = new ContentValues();
            value.put(DbHelper.kategori_nm, nm_kategori);
            database.insert(DbHelper.kategori_tb, null, value);
        }
    }

MainActivity.java

package com.savepassword.app;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

import static com.savepassword.app.R.id.nama_kategori;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    RecyclerView recyclerView;

    private DbManager dbManager;
    private Button crk;
    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ArrayList<String> myValues = new ArrayList<>();

        //adapter = new SimpleCursorAdapter(this, R.layout.view_akun, cursor, from, to, 0);

        myValues.add("kitkat");
        myValues.add("lolipop");
        myValues.add("nougat");

        RecycleAdapter adapter = new RecycleAdapter(myValues);
        RecyclerView myView = (RecyclerView)findViewById(R.id.rc_kategori);
        myView.setHasFixedSize(true);
        myView.setAdapter(adapter);
        myView.setLayoutManager(new GridLayoutManager(this, 2));
        myView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
        myView.setItemAnimator(new DefaultItemAnimator());

    }

}    

Answer

Techierj picture Techierj · Mar 25, 2017

You are almost done with it, In Main Activity Just Fetch Data From DB and Store that inside list. You have already created Adapter just bind that array list with your RecyclerView, it will work.

E.g.

ArrayList<String> myDataList = fetchDataFromDB() // will return Data from DB
RecycleAdapter adapter = new RecycleAdapter(myDataList);