android - Reload fragment/activity (or recyclerview) from adaptater -


i have simple app fragment , activity hosting recyclerview, vith data fom database, have delete function, can't refresh recycler view, tried method because longclick cardview on adapter, can't make work

this adapter:

public class loyaltycardrvadapter extends recyclerview.adapter<loyaltycardrvadapter.cardviewholder> {  ...  public loyaltycardrvadapter(context context, arraylist<loyaltycardobject> cards) {     this.context = context;     this.cards = cards; }  public void setdata(arraylist<loyaltycardobject> cards) {     this.cards = cards; }  @override public int getitemcount() {     return cards.size(); }  @override public void onbindviewholder(cardviewholder holder, int position) {     loyaltycardobject card = cards.get(position);      holder.marque.settext(card.getmarque());     ... }  @override public cardviewholder oncreateviewholder(viewgroup parent, int viewtype) {     view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.loyaltycard_row,             parent, false);     return new cardviewholder(view, context, cards); }   /**  * cette classe initialise chaque cardview avec l'object correspondant  */ public static class cardviewholder extends recyclerview.viewholder implements view.onclicklistener, view.onlongclicklistener {     cardview cardview;     textview mail, marque, code;     arraylist<loyaltycardobject> cards = new arraylist<>();     context context;     alertdialog.builder alertdialogbuilder;     requestqueue requestqueue;     imageview logo;      public cardviewholder(view view, context context, arraylist<loyaltycardobject> cards) {         ...     }      @override     public void onclick(view v) { //gère le clique sur une cardview          int position = getadapterposition();         loyaltycardobject card = this.cards.get(position);         toast.maketext(context, "codebarre pour la carte " + card.getmarque(), toast.length_short).show();         intent intentcode = new intent(this.context.getapplicationcontext(), loyaltycardshowcodeactivity.class);         intentcode.putextra("code", card.getcode());         this.context.startactivity(intentcode);     }      @override     public boolean onlongclick(view v) {          int position = getadapterposition();         final loyaltycardobject card = this.cards.get(position);         requestqueue = volley.newrequestqueue(context);           alertdialogbuilder                 .settitle("effacer")                 .setmessage("voulez-vous vraiment effacer cette carte ?")                 .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                         dialog.cancel();                          stringrequest request = new stringrequest(request.method.post, url,                                 new response.listener<string>() {                                     @override                                     public void onresponse(string response) {                                         toast.maketext(context, "carte n°" + card.getcode() + " chez " + card.getmarque() + " effacé", toast.length_short).show();                                     }                                 }, new response.errorlistener() {                             @override                             public void onerrorresponse(volleyerror error) {                                 log.i("myapp", "onerrorresponse()");                             }                         }) { // paramètres de la requete                             @override                             protected map<string, string> getparams() throws authfailureerror {                                 map<string, string> parameters = new hashmap<string, string>();                                 parameters.put("mail", card.getmail());                                 parameters.put("code", card.getcode());                                 return parameters;                             }                         };                         requestqueue.add(request);                     }                 })                 .setnegativebutton(android.r.string.no, new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                         dialog.cancel();                     }                 })                 .seticon(r.drawable.ic_error_outline_black_24dp)                 .show();         return false;     } } 

}

and fragment in exemple:

public class loyaltycardlistfragment extends android.support.v4.app.fragment {  loyaltycardrvadapter adapter; progressdialog dialog; textview addcards; usersharedpreferences usersharedpreferences; arraylist<loyaltycardobject> cards = new arraylist<loyaltycardobject>(); string url = serverurl.card_get; alertdialog.builder builder;  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {      view rootview = inflater.inflate(r.layout.loyaltycard_list_fragment, container, false);     recyclerview recyclerview = (recyclerview) rootview.findviewbyid(r.id.rv_cards);     recyclerview.sethasfixedsize(true);     adapter = new loyaltycardrvadapter(getactivity(), cards);     linearlayoutmanager llm = new linearlayoutmanager(getactivity());     llm.setorientation(linearlayoutmanager.vertical);     recyclerview.sethasfixedsize(true);     recyclerview.setlayoutmanager(llm);     recyclerview.setadapter(adapter);      addcards = (textview) rootview.findviewbyid(r.id.addcards);      adapter.setdata(cards);      dialog = new progressdialog(getactivity());     dialog.setmessage("chargement");     dialog.setcancelable(true);     showdialog();      addcards.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             builder = new alertdialog.builder(getactivity());             builder.settitle("ajout de carte");             builder.setmessage("comment ajouter ?");             builder.setpositivebutton("scanner", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int which) {                     dialog.dismiss();                     log.i("alert", "yes");                     intent intent = new intent(getactivity(), loyaltycardaddwithscanneractivity.class);                     startactivity(intent);                 }             });             builder.setnegativebutton("manuel", new dialoginterface.onclicklistener() {                 @override                 public void onclick(dialoginterface dialog, int which) {                     dialog.dismiss();                     log.i("alert", "no");                     startactivity(new intent(getactivity(), loyaltycardaddactivity.class));                 }             });             alertdialog alertdialog = builder.create();             alertdialog.show();         }     });      getandinitdata();      return rootview; }  public void getandinitdata() { //recupère les cartes de l'utilisateur et les affiches dans un recycler view sous forme de cardview      usersharedpreferences = new usersharedpreferences(getactivity());     final userobject user = usersharedpreferences.getloggedinuser();      jsonarrayrequest jsonarrayrequest = new jsonarrayrequest(request.method.get, url + user.mail,             new response.listener<jsonarray>() {                 @override                 public void onresponse(jsonarray response) {                     try {                         (int = 0; < response.length(); i++) {                             jsonobject object = response.getjsonobject(i);                             loyaltycardobject card = new loyaltycardobject();                             card.setmail(object.getstring("mail"));                             card.setmarque(object.getstring("marque"));                             card.setcode(object.getstring("code"));                              cards.add(card);                         }                         hidedialog();                         adapter.notifydatasetchanged();                     } catch (jsonexception e) {                         e.printstacktrace();                     }                 }             }, new response.errorlistener() {         @override         public void onerrorresponse(volleyerror error) {             view view = getview();             linearlayout ll = (linearlayout) view.findviewbyid(r.id.cardlist);             log.d("error!!!", error.tostring());             hidedialog();             snackbar snackbar = snackbar  //si l'utilisateur n'a pas de carte de fidelite, affiche un snackbar qui propose d'ajouter une carte                     .make(ll, "aucune carte", snackbar.length_long)                     .setaction("ajouter", new view.onclicklistener() {                         @override                         public void onclick(view view) {                             builder = new alertdialog.builder(getactivity());                             builder.settitle("ajout de carte");                             builder.setmessage("comment ajouter ?");                             builder.setpositivebutton("scanner", new dialoginterface.onclicklistener() {                                 @override                                 public void onclick(dialoginterface dialog, int which) {                                     dialog.dismiss();                                     log.i("alert", "yes");                                     intent intent = new intent(getactivity(), loyaltycardaddwithscanneractivity.class);                                     startactivity(intent);                                 }                             });                             builder.setnegativebutton("manuel", new dialoginterface.onclicklistener() {                                 @override                                 public void onclick(dialoginterface dialog, int which) {                                     dialog.dismiss();                                     log.i("alert", "no");                                     startactivity(new intent(getactivity(), loyaltycardaddactivity.class));                                 }                             });                             alertdialog alertdialog = builder.create();                             alertdialog.show();                         }                     });             snackbar.show();         }     });     appcontroller.getinstance().getrequestqueue().add(jsonarrayrequest); }  public void showdialog() {     if (!dialog.isshowing())         dialog.show(); }  public void hidedialog() {     if (dialog.isshowing())         dialog.hide(); }  @override public void ondestroyview() {     super.ondestroyview();     dialog.hide();     dialog.dismiss(); } 

}

there method can call when data contains adapter has changed. example:

  1. create listview, recyclerview, ...
  2. create adapter.
  3. set adapter listview, ....
  4. do work (change data, delete it, whatever...)

now, if want refresh actual info in view linked adapter, call method:

adapter.notifydatasetchanged(); 

that update info of view correct , newest info.

hope helps!


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -