How can I fetch SQLite data using a Adapter in android? -


good day, want fetch database data using adapter , display in recylcerviewer. don't know how implement it. hoping guide me how accomplish these task

i want replace these data database data don't know how it.

   //i want replace dummy data database data     myadapter adapter = new myadapter(new string[]{"dummy data1", "dummy data2"});  

accntfragment.java

public class accountsfragment extends fragment {      public accountsfragment() {         // required empty public constructor     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);      }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         view rootview = inflater.inflate(r.layout.fragment_accounts, container, false);          recyclerview rv = (recyclerview) rootview.findviewbyid(r.id.rv_recycler_view);         rv.sethasfixedsize(true);          //i want replace dummy data database data         myadapter adapter = new myadapter(new string[]{"dummy data1", "dummy data2"});          rv.setadapter(adapter);          linearlayoutmanager llm = new linearlayoutmanager(getactivity());         rv.setlayoutmanager(llm);          return rootview;     } } 

myadapter.java

public class myadapter extends recyclerview.adapter<myadapter.myviewholder> {     private string[] mdataset;      // provide reference views each data item     // complex data items may need more 1 view per item, ,     // provide access views data item in view holder     public static class myviewholder extends recyclerview.viewholder {          public cardview mcardview;         public textview account_type;         public textview accnt_description;         public textview balance_label;         public textview account_balance;          public myviewholder(view v) {             super(v);              mcardview = (cardview) v.findviewbyid(r.id.card_view);              account_type = (textview) v.findviewbyid(r.id.lblsharecapital);             balance_label = (textview) v.findviewbyid(r.id.lblavailablebalance);              accnt_description = (textview) v.findviewbyid(r.id.sl_desc);             account_balance = (textview) v.findviewbyid(r.id.actual_balance);          }     }      // provide suitable constructor (depends on kind of dataset)     public myadapter(string[] mydataset) {         mdataset = mydataset;     }      // create new views (invoked layout manager)     @override     public myadapter.myviewholder oncreateviewholder(viewgroup parent,                                                      int viewtype) {         // create new view         view v = layoutinflater.from(parent.getcontext())                 .inflate(r.layout.card_item, parent, false);         // set view's size, margins, paddings , layout parameters         myviewholder vh = new myviewholder(v);         return vh;     }      @override     public void onbindviewholder(myviewholder holder, final int position) {         holder.account_type.settext(mdataset[position]);         holder.mcardview.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 string currentvalue = mdataset[position];                 log.d("cardview", "cardview clicked: " + currentvalue);             }         });     }      @override     public int getitemcount() {          return mdataset.length;     } } 

sqlitehandler.java

public void adduser(string br_code, string mem_id, string username, string email, string created_at) {     sqlitedatabase db = this.getwritabledatabase();      contentvalues values = new contentvalues();     values.put(br_code, br_code); // branch code     values.put(mem_id, mem_id); // mem id     values.put(mem_username, username); // username     values.put(mem_email, email); // email     values.put(mem_created_at, created_at); // created @      // inserting row     long id = db.insertorthrow(table_members, null, values);      db.close(); // closing database connection      log.d(tag, "member's info inserted successfully: " + id);     log.d(tag, "br code: " + br_code);     log.d(tag, "member id: " + mem_id);     log.d(tag, "username: " + username);     log.d(tag, "email: " + email);     log.d(tag, "created at: " + created_at);     log.d(tag, "---------------------------------"); } /**  * getting user data database  * */ public hashmap<string, string> getuserdetails() {     hashmap<string, string> user = new hashmap<string, string>();     string selectquery = "select * " + table_members;      sqlitedatabase db = this.getreadabledatabase();     cursor cursor = db.rawquery(selectquery, null);     // move first row     cursor.movetofirst();     if (cursor.getcount() > 0) {         user.put("br_code", cursor.getstring(0));         user.put("mem_id", cursor.getstring(1));         user.put("username", cursor.getstring(2));         user.put("email", cursor.getstring(3));         user.put("created_at", cursor.getstring(4));          log.d(tag, "members's data: " + user.tostring());     }     else{         log.d(tag, "member's data empty");     }     cursor.close();     db.close();     // return user     log.d(tag, "member's info fetch: " + user.tostring());      return user; } /**  * storing user sl details in database  * */ public void addusersldtl(string sl_desc, string tr_date, string  actual_balance, string available_balance){     sqlitedatabase db = this.getwritabledatabase();      contentvalues values = new contentvalues();     values.put(sl_desc, sl_desc); // sl desc     values.put(tr_date, tr_date); // trans date     values.put(actual_balance, actual_balance); // actual balance     values.put(available_balance, available_balance); // availabe balance      // inserting row     long id = db.insertorthrow(table_members_sldtl, null, values);     db.close(); // closing database connection      log.d(tag, "members's sl details successfully: " + id);     log.d(tag, "sl desc: " + sl_desc);     log.d(tag, "transaction date: " + tr_date);     log.d(tag, "actual balance: " + actual_balance);     log.d(tag, "available balance: " + available_balance); } /**  * getting user sl details data database  * */ public hashmap<string, string> getusersldtl() {     hashmap<string, string> sl_summ = new hashmap<string, string>();     string selectquery = "select * " + table_members_sldtl;      sqlitedatabase db = this.getreadabledatabase();     cursor cursor = db.rawquery(selectquery, null);     // move first row     cursor.movetofirst();     if (cursor.getcount() > 0) {         sl_summ.put("sl_desc", cursor.getstring(0));         sl_summ.put("tr_date", cursor.getstring(1));         sl_summ.put("actual_balance", cursor.getstring(2));         sl_summ.put("available_balance", cursor.getstring(3));          log.d(tag, "member's sl details: " + sl_summ.tostring());     }     else{         log.d(tag, "member's sldtl data empty");     }     cursor.close();     db.close();      // return user     log.d(tag, "member's sl details fetch: " + sl_summ.tostring());      return sl_summ; } 

i giving sample code :- if using string[] can replace list , arraylist string[]. writing code using list.

you should fetch list of data first.

public list<hashmap<string, string>> getuserdetails() {      hashmap<string, string> user = new hashmap<>();      list<hashmap<string,string>> userlist = new arraylist<>();      // write content values hashmap      // , add hashmap list      // userlist.add(user);       return userlist;  } 

and make method in adapter notify adapter :-

static void setlist(list<hashmap> list) {      if (list != null && list.size() > 0) {         adapterlist.addall(list);     }     notifydatasetchanged(); }   list<hashmap<string,string>> userdetaillist = getuserdetails(); 

once got data database can call

adapter.setlist(userdetaillist) 

note : database operation should in background thread.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -