android - can i retrieve sqlite data depending on fragments name? -
hey have 3 fragment (3 tabs ) want column 1 display on text view tab 1 , column 2 on tab 2 column 3 on tab 3 database possible ??? of course fragments name tab 1 2 , 3.. ..!
public class databasehelper extends sqliteopenhelper {
public static final string database_name = "big.db"; public static final string table_name = "images"; public static final string name = "name"; public static final string place = "place"; **public static final integer flag = 1;** public databasehelper(context context) { super(context, database_name, null, 1); sqlitedatabase db = this.getwritabledatabase(); } @override public void oncreate(sqlitedatabase db) { string create_images_table = "create table images ( " + "id integer primary key autoincrement, " + **"flag integer, " +** "name text, " + "place text )"; db.execsql(create_images_table); } @override public void onupgrade(sqlitedatabase db, int i, int i1) { db.execsql("drop table if exists images"); this.oncreate(db); } public void insertentry(string name, string place) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(name, name); contentvalues.put(place, place); **contentvalues.put(flag,1);** db.insert(table_name, null, contentvalues); db.close(); } public void insertentry(string name, string place,integer flag) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(name, name); **contentvalues.put(flag,flag)** contentvalues.put(place, place); **contentvalues.put(flag,2)** db.insert(table_name, null, contentvalues); db.close(); } public dbresponse getdatafortab1() { dbresponse obj = new dbresponse(); sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery("select * " + table_name + "where"+flag+**"=1 , null);** if (cursor.getcount() > 0) { cursor.movetonext(); obj.name = cursor.getstring(cursor.getcolumnindex(name)); obj.place = cursor.getstring(cursor.getcolumnindex(place)); } cursor.close(); return obj; }
}
and fragments class every 1 has same code :
public class tab1 extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.tab1, container, false); imageview imageview = (imageview) v.findviewbyid(r.id.img1); string photopath = environment.getexternalstoragedirectory() + "/download/image1.jpg"; bitmap bitmap1 = bitmapfactory.decodefile(photopath); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; final bitmap b = bitmapfactory.decodefile(photopath, options); databasehelper db = new databasehelper(getactivity()); dbresponse response = db.getdata(); textview textview = (textview)v.findviewbyid(r.id.txt3); textview.settext(response.name+" - "+response.place); imageview.setimagebitmap(b); return v;
layout1.java
public class layout1 extends appcompatactivity implements tablayout.ontabselectedlistener { tablayout tablayout; viewpager viewpager; imageview imageview1, imageview2, imageview3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_layout1); tablayout = (tablayout) findviewbyid(r.id.tablayout1); tablayout.addtab(tablayout.newtab().settext("tab1")); tablayout.addtab(tablayout.newtab().settext("tab2")); tablayout.addtab(tablayout.newtab().settext("tab3")); tablayout.settabgravity(tablayout.gravity_fill); viewpager = (viewpager) findviewbyid(r.id.viewpager1); pager adapter = new pager(getsupportfragmentmanager(), tablayout.gettabcount()); viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); viewpager.setadapter(adapter); tablayout.setontabselectedlistener(this); viewpager.getcurrentitem(); viewpager.setoffscreenpagelimit(3); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab1); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int position = tablayout.getselectedtabposition(); switch (position) { case 0: file file = environment.getexternalstoragepublicdirectory(environment.directory_downloads); string picturename = "image1" + ".jpg"; // imagename(picturename); file imagefile = new file(file, picturename); uri pictureuri = uri.fromfile(imagefile); intent = new intent(android.provider.mediastore.action_image_capture); i.putextra(mediastore.extra_output, pictureuri); startactivityforresult(i, 0); break; case 1: file file2 = environment.getexternalstoragepublicdirectory(environment.directory_downloads); string picturename2 = "image2" + ".jpg"; file imagefile2 = new file(file2, picturename2); uri pictureuri2 = uri.fromfile(imagefile2); intent ii = new intent(android.provider.mediastore.action_image_capture); ii.putextra(mediastore.extra_output, pictureuri2); startactivityforresult(ii, 1); break; case 2: file file3 = environment.getexternalstoragepublicdirectory(environment.directory_downloads); string picturename3 = "image3" + ".jpg"; file imagefile3 = new file(file3, picturename3); uri pictureuri3 = uri.fromfile(imagefile3); intent iii = new intent(android.provider.mediastore.action_image_capture); iii.putextra(mediastore.extra_output, pictureuri3); startactivityforresult(iii, 2); break; } } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { imageview1 = (imageview) findviewbyid(r.id.img1); imageview2 = (imageview) findviewbyid(r.id.img2); imageview3 = (imageview) findviewbyid(r.id.img3); if (requestcode == 0 && resultcode == result_ok) { string photopath = environment.getexternalstoragedirectory() + "/download/image1.jpg"; galleryaddpic(photopath); bitmap bitmap1 = bitmapfactory.decodefile(photopath); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; final bitmap b = bitmapfactory.decodefile(photopath, options); imageview1.setimagebitmap(b); imageview1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { imageview1.builddrawingcache(); bitmap image = imageview1.getdrawingcache(); bundle extras = new bundle(); intent o = new intent(layout1.this, information.class); extras.putparcelable("bitmap", image); o.putextras(extras); startactivity(o); } }); } else if (requestcode == 1 && resultcode == result_ok) { string photopath = environment.getexternalstoragedirectory() + "/download/image2.jpg"; galleryaddpic(photopath); bitmap bitmap2 = bitmapfactory.decodefile(photopath); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; final bitmap b2 = bitmapfactory.decodefile(photopath, options); imageview2.setimagebitmap(b2); imageview2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { imageview2.builddrawingcache(); bitmap image = imageview2.getdrawingcache(); bundle extras = new bundle(); intent o = new intent(layout1.this, information.class); extras.putparcelable("bitmap", image); o.putextras(extras); startactivity(o); } }); } else if (requestcode == 2 && resultcode == result_ok) { string photopath = environment.getexternalstoragedirectory() + "/download/image3.jpg"; galleryaddpic(photopath); bitmap bitmap3 = bitmapfactory.decodefile(photopath); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; final bitmap b3 = bitmapfactory.decodefile(photopath, options); imageview3.setimagebitmap(b3); imageview3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { imageview3.builddrawingcache(); bitmap image = imageview3.getdrawingcache(); bundle extras = new bundle(); intent o = new intent(layout1.this, information.class); extras.putparcelable("bitmap", image); o.putextras(extras); startactivity(o); } }); } } private void galleryaddpic(string photopath) { intent mediascanintent = new intent(intent.action_media_scanner_scan_file); file f = new file(photopath); uri contenturi = uri.fromfile(f); mediascanintent.setdata(contenturi); this.sendbroadcast(mediascanintent); } @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabreselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); }
}
information.java
public class information extends appcompatactivity { edittext text1,text2 ; button btn; databasehelper databasehelper; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_information); text1 = (edittext) findviewbyid(r.id.txt1); text2 = (edittext) findviewbyid(r.id.txt2); imageview oo= (imageview)findviewbyid(r.id.imageview99); btn = (button) findviewbyid(r.id.btn1); log.d("click", "123 info saved"); adddata(); databasehelper = new databasehelper(this); bundle extras = getintent().getextras(); bitmap bmp = (bitmap) extras.getparcelable("bitmap"); oo.setimagebitmap(bmp); } public void adddata(){ btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { databasehelper.insertentry(text1.gettext().tostring(),text2.gettext().tostring()); toast.maketext(getapplicationcontext(),"doneeeee", toast.length_long).show(); intent b = new intent(information.this,layout1.class); startactivity(b); } }); }
log cat:
09-13 15:35:42.550 22723-22723/com.example.mike.bigone12 e/sqlitelog: (1) no such column: flag 09-13 15:35:42.550 22723-22723/com.example.mike.bigone12 d/androidruntime: shutting down vm 09-13 15:35:42.550 22723-22723/com.example.mike.bigone12 w/dalvikvm: threadid=1: thread exiting uncaught exception (group=0x41ab0270) 09-13 15:35:42.560 22723-22723/com.example.mike.bigone12 e/androidruntime: fatal exception: main process: com.example.mike.bigone12, pid: 22723 android.database.sqlite.sqliteexception: no such column: flag (code 1): , while compiling: select * images flag =1 @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:889) @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:500) @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:37) @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:44) @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1314) @ android.database.sqlite.sqlitedatabase.rawquery(sqlitedatabase.java:1253) @ com.example.mike.bigone12.databasehelper.getdatafortab1(databasehelper.java:70) @ com.example.mike.bigone12.tab1.oncreateview(tab1.java:33) @ android.support.v4.app.fragment.performcreateview(fragment.java:2074) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1286) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:758) @ android.support.v4.app.fragmentmanagerimpl.execsingleaction(fragmentmanager.java:1632) @ android.support.v4.app.backstackrecord.commitnowallowingstateloss(backstackrecord.java:637) @ android.support.v4.app.fragmentstatepageradapter.finishupdate(fragmentstatepageradapter.java:166) @ android.support.v4.view.viewpager.populate(viewpager.java:1235) @ android.support.v4.view.viewpager.populate(viewpager.java:1083) @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1609) @ android.view.view.measure(view.java:16518) @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:719) @ android.widget.relativelayout.onmeasure(relativelayout.java:455) @ android.view.view.measure(view.java:16518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5125) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ android.support.v7.widget.contentframelayout.onmeasure(contentframelayout.java:135) @ android.view.view.measure(view.java:16518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5125) @ android.support.v7.widget.actionbaroverlaylayout.onmeasure(actionbaroverlaylayout.java:391) @ android.view.view.measure(view.java:16518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5125) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ android.view.view.measure(view.java:16518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5125) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1404) @ android.widget.linearlayout.measurevertical(linearlayout.java:695) @ android.widget.linearlayout.onmeasure(linearlayout.java:588) @ android.view.view.measure(view.java:16518) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5125) @ android.widget.framelayout.onmeasure(framelayout.java:310) @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2410) @ android.view.view.measure(view.java:16518) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1921) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1114) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1296) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1001) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:5680) @ android.view.choreographer$callbackrecord.run(choreographer.java:761) @ android.view.choreographer.docallbacks(choreographer.java:574) @ android.view.choreographer.doframe(choreographer.java:544) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:747) @
i'm getting same info every tab want every tab have own info!
create field addition field in table called flag integer datatype while inserting :
in activity:
public class layout1 extends appcompatactivity implements tablayout.ontabselectedlistener { tablayout tablayout; viewpager viewpager; imageview imageview1, imageview2, imageview3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_layout1); tablayout = (tablayout) findviewbyid(r.id.tablayout1); tablayout.addtab(tablayout.newtab().settext("tab1")); tablayout.addtab(tablayout.newtab().settext("tab2")); tablayout.addtab(tablayout.newtab().settext("tab3")); tablayout.settabgravity(tablayout.gravity_fill); viewpager = (viewpager) findviewbyid(r.id.viewpager1); pager adapter = new pager(getsupportfragmentmanager(), tablayout.gettabcount()); viewpager.addonpagechangelistener(new tablayout.tablayoutonpagechangelistener(tablayout)); viewpager.setadapter(adapter); tablayout.setontabselectedlistener(this); viewpager.getcurrentitem(); viewpager.setoffscreenpagelimit(3); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab1); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int position = tablayout.getselectedtabposition(); switch (position) { case 0: file file = environment.getexternalstoragepublicdirectory(environment.directory_downloads); string picturename = "image1" + ".jpg"; // imagename(picturename); file imagefile = new file(file, picturename); uri pictureuri = uri.fromfile(imagefile); intent = new intent(android.provider.mediastore.action_image_capture); i.putextra(mediastore.extra_output, pictureuri); startactivityforresult(i, 0); break; case 1: file file2 = environment.getexternalstoragepublicdirectory(environment.directory_downloads); string picturename2 = "image2" + ".jpg"; file imagefile2 = new file(file2, picturename2); uri pictureuri2 = uri.fromfile(imagefile2); intent ii = new intent(android.provider.mediastore.action_image_capture); ii.putextra(mediastore.extra_output, pictureuri2); startactivityforresult(ii, 1); break; case 2: file file3 = environment.getexternalstoragepublicdirectory(environment.directory_downloads); string picturename3 = "image3" + ".jpg"; file imagefile3 = new file(file3, picturename3); uri pictureuri3 = uri.fromfile(imagefile3); intent iii = new intent(android.provider.mediastore.action_image_capture); iii.putextra(mediastore.extra_output, pictureuri3); startactivityforresult(iii, 2); break; } } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { imageview1 = (imageview) findviewbyid(r.id.img1); imageview2 = (imageview) findviewbyid(r.id.img2); imageview3 = (imageview) findviewbyid(r.id.img3); if (requestcode == 0 && resultcode == result_ok) { string photopath = environment.getexternalstoragedirectory() + "/download/image1.jpg"; galleryaddpic(photopath); bitmap bitmap1 = bitmapfactory.decodefile(photopath); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; final bitmap b = bitmapfactory.decodefile(photopath, options); imageview1.setimagebitmap(b); imageview1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { imageview1.builddrawingcache(); bitmap image = imageview1.getdrawingcache(); bundle extras = new bundle(); intent o = new intent(layout1.this, information.class); extras.putparcelable("bitmap", image); extras.putint("flag",1); o.putextras(extras); startactivity(o); } }); } else if (requestcode == 1 && resultcode == result_ok) { string photopath = environment.getexternalstoragedirectory() + "/download/image2.jpg"; galleryaddpic(photopath); bitmap bitmap2 = bitmapfactory.decodefile(photopath); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; final bitmap b2 = bitmapfactory.decodefile(photopath, options); imageview2.setimagebitmap(b2); imageview2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { imageview2.builddrawingcache(); bitmap image = imageview2.getdrawingcache(); bundle extras = new bundle(); intent o = new intent(layout1.this, information.class); extras.putparcelable("bitmap", image); extras.putint("flag",2); o.putextras(extras); startactivity(o); } }); } else if (requestcode == 2 && resultcode == result_ok) { string photopath = environment.getexternalstoragedirectory() + "/download/image3.jpg"; galleryaddpic(photopath); bitmap bitmap3 = bitmapfactory.decodefile(photopath); bitmapfactory.options options = new bitmapfactory.options(); options.insamplesize = 8; final bitmap b3 = bitmapfactory.decodefile(photopath, options); imageview3.setimagebitmap(b3); imageview3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { imageview3.builddrawingcache(); bitmap image = imageview3.getdrawingcache(); bundle extras = new bundle(); intent o = new intent(layout1.this, information.class); extras.putparcelable("bitmap", image); extras.putint("flag",3); o.putextras(extras); startactivity(o); } }); } } private void galleryaddpic(string photopath) { intent mediascanintent = new intent(intent.action_media_scanner_scan_file); file f = new file(photopath); uri contenturi = uri.fromfile(f); mediascanintent.setdata(contenturi); this.sendbroadcast(mediascanintent); } @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabreselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); }
}
in information activity:
public class information extends appcompatactivity { edittext text1,text2 ; button btn; integer flag; databasehelper databasehelper; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_information); text1 = (edittext) findviewbyid(r.id.txt1); text2 = (edittext) findviewbyid(r.id.txt2); imageview oo= (imageview)findviewbyid(r.id.imageview99); btn = (button) findviewbyid(r.id.btn1); log.d("click", "123 info saved"); databasehelper = new databasehelper(this); bundle extras = getintent().getextras(); bitmap bmp = (bitmap) extras.getparcelable("bitmap"); flag=extras.getint("flag"); oo.setimagebitmap(bmp)' btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { databasehelper.insertentry(text1.gettext().tostring(),text2.gettext().tostring(),flag); toast.maketext(getapplicationcontext(),"doneeeee", toast.length_long).show(); intent b = new intent(information.this,layout1.class); startactivity(b); } }); }
like third tab put 3 while inserting while calling method first tab can query this:
public class databasehelper extends sqliteopenhelper { public static final string database_name = "big.db"; public static final string table_name = "images"; public static final string name = "name"; public static final string place = "place"; public static final string flag ="flag"; public databasehelper(context context) { super(context, database_name, null, 1); sqlitedatabase db = this.getwritabledatabase(); } @override public void oncreate(sqlitedatabase db) { string create_images_table = "create table images ( " + "id integer primary key autoincrement, " + "flag integer, " + "name text, " + "place text )"; db.execsql(create_images_table); } @override public void onupgrade(sqlitedatabase db, int i, int i1) { db.execsql("drop table if exists images"); this.oncreate(db); } public void insertentry(string name, string place,integer flag) { sqlitedatabase db = this.getwritabledatabase(); contentvalues contentvalues = new contentvalues(); contentvalues.put(name, name); contentvalues.put(flag,flag) contentvalues.put(place, place); db.insert(table_name, null, contentvalues); db.close(); } public dbresponse getdatafortab1() { dbresponse obj = new dbresponse(); sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery("select * " + table_name + " " + flag + " =" + 1 + "",null); if (cursor.getcount() > 0) { cursor.movetonext(); obj.name = cursor.getstring(cursor.getcolumnindex(name)); obj.place = cursor.getstring(cursor.getcolumnindex(place)); } cursor.close(); return obj; } //in 2nd tab public dbresponse getdatafortab3() { dbresponse obj = new dbresponse(); sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery("select * " + table_name + "where"+flag+"=2 , null); if (cursor.getcount() > 0) { cursor.movetonext(); obj.name = cursor.getstring(cursor.getcolumnindex(name)); obj.place = cursor.getstring(cursor.getcolumnindex(place)); } cursor.close(); return obj; } in 3rd tab: public dbresponse getdatafortab3() { dbresponse obj = new dbresponse(); sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery("select * " + table_name + "where"+flag+"=3, null); if (cursor.getcount() > 0) { cursor.movetonext(); obj.name = cursor.getstring(cursor.getcolumnindex(name)); obj.place = cursor.getstring(cursor.getcolumnindex(place)); } cursor.close(); return obj; }
your dbhelper class:
like can call method in fragment can set textview anydoubt feel free ask
Comments
Post a Comment