2

How can I can i feed a looping data from my local database into recycler view in android.

I have three classes,

ItemData.java

public class NatureItem {
    private String title;
    private int imageUrl;
    public NatureItem(String title,int imageUrl){
    this.title = title;
    this.imageUrl = imageUrl;
    }
    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }
    public int getImageUrl() {
    return imageUrl;
    }
    public void setImageUrl(int imageUrl) {
    this.imageUrl = imageUrl;
    }
}



CardAdapter.java

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

     private NatureItem[] itemsData;

     public CardAdapter(NatureItem[] itemsData) {
     this.itemsData = itemsData;
     }

    // Create new views (invoked by the layout manager)
    @Override
    public CardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.list_cardview_activity, null);

        // create ViewHolder

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        // - get data from your itemsData at this position
        // - replace the contents of the view with that itemsData
        viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
        viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());


    }

    // inner class to hold a reference to each item of RecyclerView
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView txtViewTitle;
        public ImageView imgViewIcon;

        public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.blog_content);
            imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.imageView);
        }
    }


    // Return the size of your itemsData (invoked by the layout manager)
    @Override
    public int getItemCount() {
         return itemsData.length; 
    }
}



MainActivity.java

     NatureItem itemsData[] ={ 
                new NatureItem("imageName",R.drawable.image2),
                new NatureItem("imageName2",R.drawable.image2)              
        };

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);       
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    mAdapter = new CardAdapter(itemsData);
    recyclerView.setAdapter(mAdapter);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

With the above codes I am able to populate my recycleview with the information in the array above(itemsData). Now, I want to be able to populate my recycleview with data from my local sql database using the database handler below;

public class DatabaseHandler extends SQLiteOpenHelper {


    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_FIRSTNAME + " TEXT,"
                + KEY_LASTNAME + " TEXT,"
                + KEY_EMAIL + " TEXT UNIQUE,"
                + KEY_UID + " TEXT,"
                + KEY_CREATED_AT + " TEXT" + ")";
        db.execSQL(CREATE_LOGIN_TABLE);
    }



    /**
     * Getting user data from database
     * */
    public Cursor  getUserDetails(){
        HashMap<String,String> user = new HashMap<String,String>();
             String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

             SQLiteDatabase db = this.getReadableDatabase();
             Cursor cursor = db.rawQuery(selectQuery, null);
            if (cursor!=null){
                 user.put("fname", cursor.getString(1));
                 user.put("lname", cursor.getString(2));
                 user.put("email", cursor.getString(3));
                 user.put("uid", cursor.getString(4));
                 user.put("created_at", cursor.getString(5));
                cursor.moveToNext();
            }
            return cursor;
    }


    /**
     * Getting user status
     * return true if rows are there in table
     * */
    public int getRowCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int rowCount = cursor.getCount();
        db.close();
        cursor.close();

        // return row count
        return rowCount;
    }


}

Please how can I populate my recyclerview with the data from the database handler. I would be grateful if somebody could help. Thanks in advance

Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
NewBIe
  • 299
  • 6
  • 17
  • you can use android loader manager, android docs example start: https://developer.android.com/training/load-data-background/setup-loader.html – SacreDeveloper Sep 20 '15 at 08:48

1 Answers1

1

I would recommend on using Android Loader Manager which are perfect for this purpose and also android based.

A nice example to start with can be found in here:

Android Docs Link

External link

EDIT: In this POST you can find what you looking for. Combine it with loader manager in your activity / fragment and you are ready to go. Remember to call adapter.switchCursor() with the new cursor from OnLoadFinished

Hope this will help you.

Community
  • 1
  • 1
SacreDeveloper
  • 1,253
  • 1
  • 9
  • 16
  • 1
    Thanks for your responds. I know about using loader manager, but my problem is how do I use the recycler view to display items from the database. I want to know its implementation. I would be grateful if there are any links or tutorials to solving my problem. Thanks – NewBIe Sep 20 '15 at 09:43
  • please how and where do i reference my database handler into the recycleview adapter. thanks – NewBIe Sep 20 '15 at 15:40
  • 1
    Hi, did you get the cursoradapter and loadermanager to work to populate your recyclerview? If so, can you post your working example? – AJW Jan 02 '16 at 23:09