android - Update ViewPager Based on Dynamic Data in Firebase -


i creating polling app in realtime using firebase.

every day, adding new polls firebase category have under directory polls > dates > respective date > poll 1, poll 2, poll 3. want update viewpager display polls current day.

each poll have image, question, answer choices, total vote count, , several other components.

basically, want viewpager display each poll particular day. manually update data in firebase, , hoping firebase listeners in client/java code can update data dynamically. polls each unique instance of homepollsfragment, , having difficulty changing individual fragments display in viewpager particular day.

i trying handle changes in getitemposition() override method of fragmentstatepageradapter, , trying override "view comments" textview in included screenshot (i update else later, doing "view comments" simplicity). "view comments" state "test comments poll 1" first poll, , "test comments poll 2" second poll.

below mainactivity, create viewpager, , homepollsfragment:

mainactivity

public class mainactivity extends appcompatactivity implements     homepollsfragment.onfragmentinteractionlistener {  private toolbar toolbar; private fragmentmanager fm; private static final string firebase_url = "https://fan-polls.firebaseio.com/"; private firebase mbaseref; private firebase muserref; private firebase mpollsref; private static final string poll_frag_tag = "poll_frag"; private static final string prefs_frag_tag = "prefs_frag"; private static final string request_poll_frag_rag = "request_poll_frag"; private viewpager mpager; private fragmentstatepageradapter mpageradapter; private int mpollcountforcurrentday; private dateformat mdateformat; private date mdate; private string mcurrentdatestring; private string mpollquestion;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_home);     toolbar = (toolbar) findviewbyid(r.id.tool_bar);     setsupportactionbar(toolbar);     createnavdrawer();      //write firebase     mbaseref = new firebase(firebase_url);     muserref = mbaseref.child("users");     mpollsref = mbaseref.child("polls");      //set viewpager current day     mpager = (viewpager) findviewbyid(r.id.home_polls_viewpager);     mpageradapter = new screenslidepageradapter(getsupportfragmentmanager());     mpager.setadapter(mpageradapter);      //terminate loading activity has no value after sending user appropriate screen     loadingactivity.loadingactivity.finish();      //get current date apply viewpager     mdateformat = new simpledateformat("dd-mm-yyyy");     mdate = new date();     mcurrentdatestring = mdateformat.format(mdate);     }  @override protected void onstart() {     super.onstart();     mpollsref.addvalueeventlistener(new valueeventlistener() {         //testing methodology of adding children         @override         public void ondatachange(datasnapshot datasnapshot) {             mpollcountforcurrentday = (int) datasnapshot.child(mcurrentdatestring).getchildrencount();             mpageradapter.notifydatasetchanged();             log.i("tag", "there " + string.valueof(mpollcountforcurrentday) + " children in today's poll count.");          }          @override         public void oncancelled(firebaseerror firebaseerror) {          }     }); }  //close firebase eventlistener @override protected void onstop() {     super.onstop(); }  @override public void onfragmentinteraction(uri uri) {  }  private class screenslidepageradapter extends fragmentstatepageradapter {     public screenslidepageradapter(fragmentmanager fm) {         super(fm);     }      @override     public fragment getitem(int position) {         return new homepollsfragment();     }      @override     public int getcount() {         return mpollcountforcurrentday;     }      @override     public int getitemposition(object object) {         final object mobject = object;         mpollsref.addvalueeventlistener(new valueeventlistener() {             @override             public void ondatachange(datasnapshot datasnapshot) {                 (int = 1; <= mpollcountforcurrentday; i++) {                     homepollsfragment frag = (homepollsfragment) mobject;                     frag.mnumberofpollchildren.settext((string) datasnapshot.child(mcurrentdatestring).child(string.valueof(i)).child("poll_question").getvalue());                 }             }             @override             public void oncancelled(firebaseerror firebaseerror) {             }         });     return super.getitemposition(object);   }  }   @override public void onbackpressed() {     super.onbackpressed();     super.onbackpressed();     if (mpager.getcurrentitem() == 0) {         // if user looking @ first step, allow system handle         // button. calls finish() on activity , pops stack.         super.onbackpressed();     } else {         // otherwise, select previous step.         mpager.setcurrentitem(mpager.getcurrentitem() - 1);     }   } } 

homepollsfragment, trying create new instance of different question, answer, , other info each day (the viewpager should dynamically update each day polls on given day):

public class homepollsfragment extends fragment { // todo: rename parameter arguments, choose names match // fragment initialization parameters, e.g. arg_item_number private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2";  // todo: rename , change types of parameters private string mparam1; private string mparam2; public textview mnumberofpollchildren; private firebase mbaseref; private firebase mpollsref; private static final string firebase_url = "https://fan-polls.firebaseio.com/";  //all date items; dynamic private int mpollcountforcurrentday; private dateformat mdateformat; private date mdate; private string mcurrentdatestring;   private onfragmentinteractionlistener mlistener;  public homepollsfragment() {     // required empty public constructor }  /**  * use factory method create new instance of  * fragment using provided parameters.  *  * @param param1 parameter 1.  * @param param2 parameter 2.  * @return new instance of fragment homepollsfragment.  */ // todo: rename , change types , number of parameters public static homepollsfragment newinstance(string param1, string param2) {     homepollsfragment fragment = new homepollsfragment();     bundle args = new bundle();     args.putstring(arg_param1, param1);     args.putstring(arg_param2, param2);     fragment.setarguments(args);     return fragment; }  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     mbaseref = new firebase(firebase_url);     mpollsref = mbaseref.child("polls");     //get current date apply viewpager     mdateformat = new simpledateformat("dd-mm-yyyy");     mdate = new date();     mcurrentdatestring = mdateformat.format(mdate);      if (getarguments() != null) {         mparam1 = getarguments().getstring(arg_param1);         mparam2 = getarguments().getstring(arg_param2);     } }  @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     // inflate layout fragment     viewgroup rootview = (viewgroup) inflater.inflate(r.layout.fragment_poll, container, false);     mnumberofpollchildren = (textview) rootview.findviewbyid(r.id.comments_textview);     return rootview; }  @override public void onstart() {     super.onstart();     mpollsref.addvalueeventlistener(new valueeventlistener() {         @override         public void ondatachange(datasnapshot datasnapshot) {             mpollcountforcurrentday = (int) datasnapshot.child(mcurrentdatestring).getchildrencount();             log.i("tag", "there " + string.valueof(mpollcountforcurrentday) + " children in today's pollllll count.");         }          @override         public void oncancelled(firebaseerror firebaseerror) {          }     }); }  //close event listener onstop @override public void onstop() {     super.onstop(); }  // todo: rename method, update argument , hook method ui event public void onbuttonpressed(uri uri) {     if (mlistener != null) {         mlistener.onfragmentinteraction(uri);     } }  @override public void onattach(context context) {     super.onattach(context);     if (context instanceof onfragmentinteractionlistener) {         mlistener = (onfragmentinteractionlistener) context;     } else {         throw new runtimeexception(context.tostring()                 + " must implement onfragmentinteractionlistener");     } }  @override public void ondetach() {     super.ondetach();     mlistener = null; }  /**  * interface must implemented activities contain  * fragment allow interaction in fragment communicated  * activity , potentially other fragments contained in  * activity.  * <p/>  * see android training lesson <a href=  * "http://developer.android.com/training/basics/fragments/communicating.html"  * >communicating other fragments</a> more information.  */ public interface onfragmentinteractionlistener {     // todo: update argument type , name     void onfragmentinteraction(uri uri);  } } 

example of poll design view comments textview:

enter image description here


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -