java - Complete android cardView with db helper and recycleView -
i pretty new whole android development looking create simple android app making use of sql database, cardviews , recycleview. app have far works trying add button detailedactivity allow me remove entry database.
the desired workflow be:
- get data
- select cardview
- click new button (will delete)
- go cardview, remove entry on button has been pressed , populate cardview.
recycleradapter.java
package com.example.prabhu.databasedemo; import android.content.context; import android.content.intent; import android.os.bundle; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.textview; import android.widget.toast; import java.util.arraylist; import java.util.list; public class recycleradapter extends recyclerview.adapter<recycleradapter.viewholder> { static list<databasemodel> dblist; static context context; recycleradapter(context context, list<databasemodel> dblist ){ this.dblist = new arraylist<databasemodel>(); this.context = context; this.dblist = dblist; } @override public recycleradapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view itemlayoutview = layoutinflater.from(parent.getcontext()).inflate( r.layout.item_row, null); // create viewholder viewholder viewholder = new viewholder(itemlayoutview); return viewholder; } @override public void onbindviewholder(recycleradapter.viewholder holder, int position) { holder.name.settext(dblist.get(position).getname()); holder.email.settext(dblist.get(position).getemail()); } @override public int getitemcount() { return dblist.size(); } public static class viewholder extends recyclerview.viewholder implements view.onclicklistener { public textview name,email; public viewholder(view itemlayoutview) { super(itemlayoutview); name = (textview) itemlayoutview .findviewbyid(r.id.rvname); email = (textview)itemlayoutview.findviewbyid(r.id.rvemail); itemlayoutview.setonclicklistener(this); } @override public void onclick(view v) { intent intent = new intent(context,detailsactivity.class); bundle extras = new bundle(); extras.putint("position",getadapterposition()); intent.putextras(extras); /* int i=getadapterposition(); intent.putextra("position", getadapterposition());*/ context.startactivity(intent); toast.maketext(recycleradapter.context, "you have clicked row " + getadapterposition(), toast.length_short).show(); } } }
databasehelper.java
package com.example.prabhu.databasedemo; import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; import android.widget.toast; import java.util.arraylist; import java.util.list; public class databasehelpher extends sqliteopenhelper { private static final string database_name="student"; private static final int database_version = 1; private static final string student_table = "stureg"; private static final string stu_table = "create table "+student_table +"(name text,email text primary key,roll text,address text,branch text)"; context context; public databasehelpher(context context) { super(context, database_name, null, database_version); this.context = context; } @override public void oncreate(sqlitedatabase db) { db.execsql(stu_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + student_table); // create tables again oncreate(db); } /* insert database*/ public void insertintodb(string name,string email,string roll,string address,string branch){ log.d("insert", "before insert"); // 1. reference writable db sqlitedatabase db = this.getwritabledatabase(); // 2. create contentvalues add key "column"/value contentvalues values = new contentvalues(); values.put("name", name); values.put("email", email); values.put("roll", roll); values.put("address", address); values.put("branch", branch); // 3. insert db.insert(student_table, null, values); // 4. close db.close(); toast.maketext(context, "insert value", toast.length_short); log.i("insert db", "after insert"); } /* retrive data database */ public list<databasemodel> getdatafromdb(){ list<databasemodel> modellist = new arraylist<databasemodel>(); string query = "select * "+student_table; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(query,null); if (cursor.movetofirst()){ { databasemodel model = new databasemodel(); model.setname(cursor.getstring(0)); model.setemail(cursor.getstring(1)); model.setroll(cursor.getstring(2)); model.setaddress(cursor.getstring(3)); model.setbranch(cursor.getstring(4)); modellist.add(model); }while (cursor.movetonext()); } log.d("student data", modellist.tostring()); return modellist; } /*delete row database*/ public void deletearow(string email){ sqlitedatabase db= this.getwritabledatabase(); db.delete(student_table, "email" + " = ?", new string[] { email }); db.close(); } }
databasemodel.java
package com.example.prabhu.databasedemo; public class databasemodel { private string name; private string roll; private string address; private string branch; private string email; public string getname() { return name; } public void setname(string name) { this.name = name; } public string getroll() { return roll; } public void setroll(string roll) { this.roll = roll; } public string getaddress() { return address; } public void setaddress(string address) { this.address = address; } public string getbranch() { return branch; } public void setbranch(string branch) { this.branch = branch; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } }
mainactivity.java
package com.example.prabhu.databasedemo; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.toolbar; 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 java.util.list; public class mainactivity extends appcompatactivity { edittext etname,etroll,etaddress,etbranch,etemail; button btnsubmit,btngetdata; databasehelpher helpher; list<databasemodel> dblist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); dblist= new arraylist<databasemodel>(); etname = (edittext)findviewbyid(r.id.etname); etroll = (edittext)findviewbyid(r.id.etroll); etaddress =(edittext)findviewbyid(r.id.etaddress); etbranch = (edittext)findviewbyid(r.id.etbranch); etemail = (edittext)findviewbyid(r.id.etemail); btnsubmit =(button)findviewbyid(r.id.btnsubmit); btngetdata =(button)findviewbyid(r.id.btngetdata); btngetdata.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivity(new intent(mainactivity.this, secondactivity.class)); // startactivity(new intent(mainactivity.this, detailsactivity.class)); } }); btnsubmit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string name=etname.gettext().tostring(); string email=etemail.gettext().tostring(); string roll=etroll.gettext().tostring(); string address=etaddress.gettext().tostring(); string branch=etbranch.gettext().tostring(); if(name.equals("") || email.equals("") || roll.equals("") ||address.equals("")||branch.equals("")){ toast.maketext(mainactivity.this,"please fill fields",toast.length_short).show(); }else { helpher = new databasehelpher(mainactivity.this); helpher.insertintodb(name, email, roll, address, branch); } etname.settext(""); etroll.settext(""); etaddress.settext(""); etbranch.settext(""); etemail.settext(""); toast.maketext(mainactivity.this, "insert value", toast.length_short); } }); } }
secondactivity.java
package com.example.prabhu.databasedemo; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import java.util.arraylist; import java.util.list; public class secondactivity extends appcompatactivity { databasehelpher helpher; list<databasemodel> dblist; recyclerview mrecyclerview; private recyclerview.adapter madapter; private recyclerview.layoutmanager mlayoutmanager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); helpher = new databasehelpher(this); dblist= new arraylist<databasemodel>(); dblist = helpher.getdatafromdb(); mrecyclerview = (recyclerview)findviewbyid(r.id.recycleview); mrecyclerview.sethasfixedsize(true); // use linear layout manager mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager); // specify adapter (see next example) madapter = new recycleradapter(this,dblist); mrecyclerview.setadapter(madapter); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_second, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: finish(); return true; } return super.onoptionsitemselected(item); } }
detailedactivity.java
package com.example.prabhu.databasedemo; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import android.widget.textview; import android.widget.toast; import java.util.arraylist; import java.util.list; public class detailsactivity extends appcompatactivity { databasehelpher helpher; list<databasemodel> dblist; int position; textview tvname,tvemail,tvroll,tvaddress,tvbranch; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_details); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); intent intent = getintent(); bundle bundle = intent.getextras(); // 5. status value bundle position = bundle.getint("position"); tvname =(textview)findviewbyid(r.id.name); tvemail =(textview)findviewbyid(r.id.email); tvroll =(textview)findviewbyid(r.id.roll); tvaddress =(textview)findviewbyid(r.id.address); tvbranch =(textview)findviewbyid(r.id.branch); helpher = new databasehelpher(this); dblist= new arraylist<databasemodel>(); dblist = helpher.getdatafromdb(); if(dblist.size()>0){ string name= dblist.get(position).getname(); string email=dblist.get(position).getemail(); string roll=dblist.get(position).getroll(); string address=dblist.get(position).getaddress(); string branch=dblist.get(position).getbranch(); tvname.settext(name); tvemail.settext(email); tvroll.settext(roll); tvaddress.settext(address); tvbranch.settext(branch); } toast.maketext(detailsactivity.this, dblist.tostring(), toast.length_short); } public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_details, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: finish(); return true; } return super.onoptionsitemselected(item); } }
i had added button (findviewbyid) in detailedavtivity.java onclicklistener. show toast, however, when button on detailed view clicked, nothing happens - no toast.
so really, questions are:
- where have define button , implement onclicklistener?
- how make button delete entry? happy use email email textview (tvemail) have go , refresh cards well.
thanks in advance
after time found answer. doing correct messed toast command.
basically needs done is
delbutton = (button) findviewbyid(r.id.button); delbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(getapplicationcontext(), tvemail.gettext() + " deleted", toast.length_short).show(); startactivity(new intent(detailsactivity.this, mainactivity.class)); helpher.deletearow((string) tvemail.gettext()); } });
Comments
Post a Comment