android - Database with repeating records -


i have sqlite database in app.when have internet connection,data saved , when haven't internet connection,i loaded data database , show in listview.but in listview have many records,i think when have internet created records how many times,how start app.but how can delete records,create new,and dont create same records every time when start app? mainlist:

public class mainlist extends listfragment{     sqlhelper dbhelper;     listview mainlist;     progressbar progbar;     private static string url = "https://fierce-citadel-4259.herokuapp.com/hamsters";     private static final string title = "title";     private static final string description = "description";     private static final string image = "image";     arraylist<hashmap<string,string>> jsonlist1 = new arraylist<hashmap<string, string>>();     arraylist<hashmap<string,string>> bdlist = new arraylist<hashmap<string, string>>();     public view oncreateview(layoutinflater inflater,viewgroup container,bundle savedinstancestate) {         view v = inflater.inflate(r.layout.list_fragme, null);         mainlist = (listview)v.findviewbyid(android.r.id.list);     progbar = (progressbar) v.findviewbyid(r.id.progressbar);         return v;     }      public void onactivitycreated(bundle savedinstancestate){         super.onactivitycreated(savedinstancestate);         if (isnetworkconnected()==true) {             new progresstask().execute();         }         else{             displaysavedlv();         }     }      @override     public void onlistitemclick(listview l, view view, int position, long id) {         super.onlistitemclick(l, view, position, id);         string title = jsonlist1.get(position).get("title");         string description= jsonlist1.get(position).get("description");         string image = jsonlist1.get(position).get("image");           mydetailfragment detailfragment = new mydetailfragment();         bundle bundle = new bundle();         if(isnetworkconnected()==true) {             bundle.putstring("title", title);             bundle.putstring("description", description);             bundle.putstring("image", image);         }         else         {             string dbtitle= bdlist.get(position).get("title");             string dbdescription = bdlist.get(position).get("description");             string dvimage = bdlist.get(position).get("image");             bundle.putstring("title",dbtitle);             bundle.putstring("description",dbdescription);             bundle.putstring("image",dvimage);         }         detailfragment.setarguments(bundle);         fragmenttransaction fragmenttransaction = getactivity().getfragmentmanager().begintransaction();         fragmenttransaction.replace(r.id.fragmentcont,detailfragment);         fragmenttransaction.addtobackstack(null);         fragmenttransaction.commit();     }      private class progresstask extends asynctask<string,void,boolean> {         private progressdialog dialog;         private activity activity;         private mainactivity context;         private string[] params;          public progresstask(mainactivity activity) throws sqlexception {             this.activity = getactivity();             context = activity;             dialog = new progressdialog(getactivity().getapplicationcontext());          }          public progresstask()  {             dialog = new progressdialog(getactivity().getapplicationcontext());             try {                 dbhelper = new sqlhelper(getactivity().getapplicationcontext());             } catch (sqlexception e) {                 e.printstacktrace();             }         }          @override         protected boolean doinbackground(string... params) {             this.params = params;             jsonparser jparser = new jsonparser();             jsonarray json = jparser.getjsonfromurl(url);             for(int =0;i<json.length();i++) {                 try {                     jsonobject c = json.getjsonobject(i);                     string vtitle = c.getstring(title);                     string vdescription = c.getstring(description);                     string vimage = c.getstring(image);                     dbhelper.open();                     dbhelper.createentry(vtitle, vimage, vdescription);                     dbhelper.close();                      hashmap<string, string> map = new hashmap<>();                     map.put(title, vtitle);                     map.put(description, vdescription);                     map.put(image, vimage);                      jsonlist1.add(map);                  } catch (jsonexception e) {                     e.printstacktrace();                 } catch (sqlexception e) {                     e.printstacktrace();                 }             }             return null;         }           protected void onpreexecute(){             progbar.setvisibility(view.visible);         }         protected void onpostexecute(final boolean success){              if (isnetworkconnected()==true) {                 progbar.setvisibility(view.gone);                 customlistadapter adapter = new customlistadapter(getactivity(), jsonlist1, r.layout.list_item, new string[]{title, description}, new int[]{r.id.title, r.id.description});                 mainlist.setadapter(adapter);             }             else {                 displaysavedlv();             }         }     }      private void displaysavedlv() {         try {             dbhelper = new sqlhelper(getactivity().getapplicationcontext());         } catch (sqlexception e) {             e.printstacktrace();         }         bdlist = dbhelper.getalldata();          customlistadapter adapter1 = new customlistadapter(getactivity(), bdlist, r.id.list_item, new string[]{title, description}, new int[]{r.id.title, r.id.description});         mainlist.setadapter(adapter1);     }     private boolean isnetworkconnected() {         connectivitymanager cm = (connectivitymanager)getactivity().getsystemservice(context.connectivity_service);         networkinfo ni = cm.getactivenetworkinfo();         if (ni == null) {             // there no active networks.             return false;         } else             return true;     } } 

sqlhelper class:

public class sqlhelper {     string title;     string description;     string image;     private static final string text_type = " text";     private static final string comma_sep = ",";     public static final string key_rowid = "_id";     public static final string key_title = "title";     public static final string key_description = "description";     public static final string key_image = "image";     private sqlhelper mdb;     private static final string database_name = "dbcategory";     private static final string database_table = "categorytable";     private static final int database_version = 1;     private static final string sql_create_entries =             "create table " + database_table + " (" +                     key_rowid + " integer primary key," +                     key_title + text_type + comma_sep +                     key_description + text_type + comma_sep + key_image + text_type +                      " )";     private dbhelper ourhelper;     private final context ourcontext;     private sqlitedatabase ourdatabase;      public sqlhelper(context c) throws sqlexception {         ourcontext = c;         try {             open();         } catch (sqlexception e) {             e.printstacktrace();         }     }      public sqlhelper open() throws sqlexception{         ourhelper = new dbhelper(ourcontext);         ourdatabase = ourhelper.getwritabledatabase();         return this;     }      public void close(){         ourhelper.close();     }      private static class dbhelper extends sqliteopenhelper {          public dbhelper(context context) {             super(context, database_name, null, database_version);             // todo auto-generated constructor stub         }          @override         public void oncreate(sqlitedatabase db) {             db.execsql("create table " + database_table + " (" +                             key_rowid + " integer primary key autoincrement," +                             key_title + " text not null," + key_description + " text not null," + key_image + " text not null );"             );         }          @override         public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {             db.execsql("drop table if exists " + database_table);             oncreate(db);         }      }      public long createentry(string title, string description,string image) {         contentvalues cv = new contentvalues();         cv.put(key_title, title);         cv.put(key_description,description);         cv.put(key_image, image);          return ourdatabase.insert(database_table, null, cv);     }      public arraylist<hashmap<string, string>> getalldata()     {         arraylist<hashmap<string, string>> array_list = new arraylist<hashmap<string, string>>();          //hp = new hashmap();         sqlitedatabase db = this.ourhelper.getreadabledatabase();         cursor res =  db.rawquery( "select * categorytable", null );         res.movetofirst();          while(res.isafterlast() == false){              hashmap<string,string>  hashmap = new hashmap<string, string>();             hashmap.put("title", res.getstring(res.getcolumnindex(key_title)));             hashmap.put("description", res.getstring(res.getcolumnindex(key_image)));             hashmap.put("image", res.getstring(res.getcolumnindex(key_description)));                 array_list.add(hashmap);             res.movetonext();         }         return array_list;     } } 

so here understand you, every time when have connection put data in database show them later if there no connection after while database contains many data, want show last data retrieved , answer before put new data in database make sure delete in database creating drop table or delete method query .. go.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -