java - How to create new instance dynamically? -


i want store car details sharedpreference. , there more 1 group of data. create new sharedpreference instance when car number not exist.

so instance's name can't same previous one. have no idea create new instance of sharedpreference dynamically.

@override public void onclick(view view) {     switch (view.getid()){         case r.id.search_btn:             responsetext.settext("");             mlist = new arraylist<sharedpreferences>();             string isnum = textnum.gettext().tostring();             iterator iterator = mlist.iterator();              while (iterator.hasnext()){                 if((iterator.next().tostring()).equals(isnum)){                     log.i("information","exist!");                     break;                  }else {                     sharedpreferences pref = getsharedpreferences("data", mode_private);                       mlist.add(pref);                     sharedpreferences.editor editor = pref.edit();                     editor.putstring("type", texttype.gettext().tostring());                     editor.putstring("num", textnum.gettext().tostring());                     editor.putstring("car", textcar.gettext().tostring());                     editor.commit();                     break;                 }             }             break;      } }    

edit : trying store details of car (vehicle type, plate number, vehicle identification number) sharedpreference. want store list. want add details if not exist in sharedpreference.

you need pojo class hold details of car.

private class car {     public string num;     public string car;      // getters , setters     public void setnum(string num){         this.num = num;     }      public string getnum(){         return num;     }       public void setcar(string car){         this.car = car;     }      public string getcar(){         return car;     }  } 

you can use gson library convert list of cars json , store in sharedpreferences.
onclick method,

@override public void onclick(view view) {     switch (view.getid()){         case r.id.search_btn:             responsetext.settext("");             string isnum = textnum.gettext().tostring();              // create instance of sharedpreferences             sharedpreferences pref = getsharedpreferences("data", mode_private);             string value = pref.getstring("cardata", "");             type type = new typetoken<arraylist<car>>() {}.gettype()             gson gson = new gson();             arraylist<car> cars = gson.fromjson(value, type);              // linear search             boolean flag = false;             (car car : cars) {                 if(car.num.equals(isnum)){                     flag = true;                     break;                 }             }              // not found. add new car             if(!flag){                 cars.add(new car(textcar.gettext().tostring(), textnum.gettext().tostring()));                 sharedpreferences.editor editor = pref.edit();                 editor.putstring("cardata", gson.tojson(cars));                 editor.apply();             }      } } 

note : large number of cars, sharedpreferences can huge. switch database if case.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -