java - android SQLite, can't view data anymore in my listView -
yesterday made nice listview see database, today tried add on line database , got error message saying "database leaked. blabla close database". after not add anymore line database.
but restarted android studio , add new line (and no error anymore) ! can't see news lines add. see line added before error (or maybe can see 1 added caused me error). checked database chrome , see everything, added news lines !
i don't know up, checked history several class didn't make bad change (i think). how can see database again ? use contentprovider access database.
here fragment class list view (i use 2 button inside elements list view , textview) :
public class viewcardeditor extends fragment implements loadermanager.loadercallbacks<cursor> { public static final string authority = "com.example.jean.cartememoire.cardcontentprovider"; public string[] from; public final int[] = {r.id.idlist, r.id.themelist, r.id.questionlist, r.id.reponselist, r.id.difficultlist}; stockcard stock; viewgroup container; listview listview; mysimplecursoradapter adapter; private arraylist<string> data = new arraylist<string>(); @override public view oncreateview(layoutinflater inflater, viewgroup c, bundle savedinstancestate) { container = c; view view = inflater.inflate(r.layout.fragment_view_card_editor, container, false); stock = stockcard.getinstance(container.getcontext()); = new string[]{stock._id, stock.theme, stock.question, stock.reponse, stock.difficulte}; // inflate layout fragment if (container != null) { container.removeallviews(); } databaseview(view); return view; } public loader<cursor> oncreateloader(int id, bundle args) { uri.builder builder = new uri.builder(); uri uri = builder.scheme("content").authority(authority) .appendpath(stock.stock_table).build(); return new cursorloader(container.getcontext(), uri, from, null, null, null); } public void onloadfinished(loader<cursor> loader, cursor data) { adapter.swapcursor(data); } public void onloaderreset(loader<cursor> loader) { adapter.swapcursor(null); } public void databaseview(view view) { arraylist<string> list; cursor cursor = stock.getdata(); listview = (listview) view.findviewbyid(r.id.listview); adapter = new mysimplecursoradapter(container.getcontext(), r.layout.card_stock, null, from, to,0); listview.setadapter(adapter); loadermanager manager = getloadermanager(); manager.initloader(0, null, this); } public void deleteonecard(int id) { uri.builder builder = new uri.builder(); builder.scheme("content").authority(authority).appendpath(stock.stock_table); contenturis.appendid(builder, id); uri uri = builder.build(); contentresolver resolver = container.getcontext().getcontentresolver(); resolver.delete(uri, null, null); } private class mysimplecursoradapter extends simplecursoradapter { viewholder vh; public mysimplecursoradapter(context context, int layout, cursor c, string[] from, int[] to, int flags) { super(context, layout, c, from, to, flags); } public view newview(context _context, cursor _cursor, viewgroup parent) { layoutinflater inflater = (layoutinflater) _context.getsystemservice(_context.layout_inflater_service); view view = inflater.inflate(r.layout.card_stock, parent, false); vh = new viewholder(); vh.idlist = (textview) view.findviewbyid(r.id.idlist); vh.themelist = (textview) view.findviewbyid(r.id.themelist); vh.questionlist = (textview) view.findviewbyid(r.id.questionlist); vh.reponselist = (textview) view.findviewbyid(r.id.reponselist); vh.difficultelist = (textview) view.findviewbyid(r.id.difficultlist); vh.supprimer = (button) view.findviewbyid(r.id.buttondelete); vh.modifier = (button) view.findviewbyid(r.id.buttonmodifier); view.settag(vh); return view; } public void bindview(view view, context context, cursor cursor) { vh.idlist.settext(cursor.getstring(cursor.getcolumnindex(stock._id))); vh.themelist.settext(cursor.getstring(cursor.getcolumnindex(stock.theme))); vh.questionlist.settext(cursor.getstring(cursor.getcolumnindex(stock.question))); vh.reponselist.settext(cursor.getstring(cursor.getcolumnindex(stock.reponse))); vh.difficultelist.settext(cursor.getstring(cursor.getcolumnindex(stock.difficulte))); vh.supprimer.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { view parentview = (view)v.getparent(); textview idlist = (textview) parentview.findviewbyid(r.id.idlist); /*textview themelist = (textview) parentview.findviewbyid(r.id.themelist); textview questionlist = (textview) parentview.findviewbyid(r.id.questionlist); textview reponselist = (textview) parentview.findviewbyid(r.id.reponselist); textview difficultelist = (textview) parentview.findviewbyid(r.id.difficultlist);*/ int id = integer.parseint(idlist.gettext().tostring()); deleteonecard(id); toast.maketext(container.getcontext(), "suppression de "+id, toast.length_short).show(); } }); vh.modifier.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { view parentview = (view)v.getparent(); textview idlist = (textview) parentview.findviewbyid(r.id.idlist); textview themelist = (textview) parentview.findviewbyid(r.id.themelist); themelist.setfocusable(true); themelist.requestfocus(); /*textview questionlist = (textview) parentview.findviewbyid(r.id.questionlist); textview reponselist = (textview) parentview.findviewbyid(r.id.reponselist); textview difficultelist = (textview) parentview.findviewbyid(r.id.difficultlist);*/ int id = integer.parseint(idlist.gettext().tostring()); toast.maketext(container.getcontext(), "bouton modifier pour "+id, toast.length_short).show(); } }); } } public class viewholder { button supprimer, modifier; textview idlist, themelist, questionlist, reponselist, difficultelist; } }
here class adding news lines :
public class addcardeditor extends fragment { edittext edittheme, editquest, editrep; ratingbar ratingbar; button buttoncreatecard; cardcontentprovider stock; viewgroup container; @override public view oncreateview(layoutinflater inflater, viewgroup c, bundle savedinstancestate) { view view; view = inflater.inflate(r.layout.fragment_add_card_editor, container, false); // inflate layout fragment container = c; if (container != null) { container.removeallviews(); } edittheme =(edittext) view.findviewbyid(r.id.edittheme); editquest =(edittext) view.findviewbyid(r.id.editquest); editrep =(edittext) view.findviewbyid(r.id.editrep); ratingbar =(ratingbar) view.findviewbyid(r.id.ratingbar); buttoncreatecard =(button) view.findviewbyid(r.id.buttoncreatecard); stock = new cardcontentprovider(); adddata(container); return view; } public void adddata(final viewgroup container) { buttoncreatecard.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { try { contentvalues cv = new contentvalues(); cardcontentprovider cp = new cardcontentprovider(); cv.put(stock.theme, edittheme.gettext().tostring()); cv.put(stock.question, editquest.gettext().tostring()); cv.put(stock.reponse, editrep.gettext().tostring()); cv.put(stock.difficulte, ratingbar.getrating()); contentresolver resolver = container.getcontext().getcontentresolver(); uri.builder builder = new uri.builder(); system.out.println("adddata debug"); builder.scheme("content").authority(stock.authority).appendpath(stock.stock_table); system.out.println("adddata debug2"); uri uri = builder.build(); uri = resolver.insert(uri,cv); long id = contenturis.parseid(uri); system.out.println("adddata debug3 : "+id); toast.maketext(container.getcontext(), "carte créé avec succès !", toast.length_short).show(); } catch (exception e) { toast.maketext(container.getcontext(), "erreur, echec de création de la carte", toast.length_short).show(); } } }); } }
then layout listview :
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/relativelist" android:descendantfocusability="blocksdescendants"> <textview android:text="@string/difficult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview8" android:layout_below="@+id/textview7" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_margintop="29dp" /> <textview android:text="@string/r_ponse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview7" android:layout_margintop="20dp" android:layout_below="@+id/textview6" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/idlist" android:layout_alignparenttop="true" android:layout_alignleft="@+id/themelist" android:layout_alignstart="@+id/themelist" tools:ignore="hardcodedtext" /> <textview android:text="question :" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview6" android:layout_margintop="14dp" android:layout_below="@+id/textview4" android:layout_alignparentleft="true" android:layout_alignparentstart="true" tools:ignore="hardcodedtext" /> <textview android:text="@string/id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview3" android:layout_alignparenttop="true" android:layout_alignright="@+id/textview4" android:layout_alignend="@+id/textview4" /> <textview android:text="thème :" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview4" android:layout_below="@+id/textview3" android:layout_alignright="@+id/textview6" android:layout_alignend="@+id/textview6" tools:ignore="hardcodedtext" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textview6" android:id="@+id/questionlist" android:layout_torightof="@+id/themelist" android:layout_toendof="@+id/themelist" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textview8" android:layout_alignleft="@+id/questionlist" android:layout_alignstart="@+id/questionlist" android:id="@+id/reponselist" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/textview8" android:layout_alignright="@+id/reponselist" android:layout_alignend="@+id/reponselist" android:id="@+id/difficultlist" android:layout_toendof="@+id/reponselist" android:layout_torightof="@+id/reponselist" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/themelist" android:layout_marginleft="33dp" android:layout_marginstart="33dp" tools:ignore="hardcodedtext" android:layout_below="@+id/idlist" android:layout_torightof="@+id/textview8" android:layout_toendof="@+id/textview8" /> <button android:text="@string/supprimerb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:id="@+id/buttondelete"/> <button android:text="@string/modifierb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/buttonmodifier" android:layout_aligntop="@+id/questionlist" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_alignleft="@+id/buttondelete" android:layout_alignstart="@+id/buttondelete" /> </relativelayout>
and contentprovider class:
public class cardcontentprovider extends contentprovider { private stockcard stock; public static string authority = "com.example.jean.cartememoire.cardcontentprovider"; private static string path ="cartes_table"; public static final string _id = "_id"; public static final string theme = "theme"; public static final string question = "question"; public static final string reponse = "reponse"; public static final string difficulte = "difficulte"; //# = un chiffre public static final string stock_table = "cartes_table"; private static final int id_stock_table = 1; private static final int id_theme = 2; private static final int id_question = 3; private static final int id_reponse = 4; private static final int id_difficulte = 5; private static final urimatcher matcher = new urimatcher(urimatcher.no_match); static { matcher.adduri(authority, stock_table, id_stock_table); matcher.adduri(authority, stock_table+"/*", id_stock_table); } @override public boolean oncreate() { stock = stockcard.getinstance(getcontext()); return true; } @nullable @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitedatabase db = stock.getreadabledatabase(); int code = matcher.match(uri); long id; cursor cursor; switch (code) { case id_stock_table: cursor = db.query(stock_table, projection, selection, selectionargs, null, null, sortorder); break; default: log.d("uri provider =", uri.tostring()); throw new unsupportedoperationexception("pas encore implémenté"); } return cursor; } @nullable @override public string gettype(uri uri) { return null; } @nullable @override public uri insert(uri uri, contentvalues values) { sqlitedatabase db = stock.getwritabledatabase(); int code = matcher.match(uri); long id; uri.builder builder = new uri.builder(); switch(code) { case id_stock_table: system.out.println(values.tostring()); id = db.insert(stock_table, null, values); builder.appendpath(stock_table); break; default: throw new unsupportedoperationexception("pas encore implémenté"); } builder.authority(authority); builder = contenturis.appendid(builder, id); return builder.build(); } @override public int delete(uri uri, string selection, string[] selectionargs) { sqlitedatabase db = stock.getwritabledatabase(); int code = matcher.match(uri); int i; switch(code) { case id_stock_table: long id = contenturis.parseid(uri); = db.delete(stock_table, "_id=" + id, null); break; default: throw new unsupportedoperationexception("pas encore implémenté"); } return 0; } @override public int update(uri uri, contentvalues values, string selection, string[] selectionargs) { return 0; } }
thank help.
edit : tried adding cursor close didn't change anything... have button delete 1 line in database and... if delete 1 line, don't see line anymore in listview can see line added , not see before... think can display 3 lines in listview, feel problem in listview itself. i'm checking doc listview see miss...
edit 2 : design problem, had few item show because not scroll through item. here new fragment layout contain listview :
<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="layout.viewcardeditor" android:background="@android:color/darker_gray"> <listview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listview" /> </framelayout>
before, layout_height set 500dp. listview little, can scroll text view inside listview bugged, content display content other textview , change again when scrolling. content textview scrolling me in other textview.
edit 3 : vote delete question because didn't had more error, have new problem listview, has limited scroll :i can see 3 items , top of 4 item, can scroll little bit...
you'll need notify cursors
content has changed using contentresolvers.notifychange(...) method. you'll want in custom contentprovider when update data. call allows cursors register change notifications refresh data.
Comments
Post a Comment