Created
June 11, 2017 14:23
-
-
Save hassanabidpk/fd507bc3de498d1321e2e2872aa70de7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PastEventFragment extends Fragment { | |
private static final String TAG = PastEventFragment.class.getSimpleName(); | |
public static final String MARATHON_EVENT_DATABASE = "event"; | |
private DatabaseReference mDatabase; | |
private List<Event> mEventList; | |
private Query mEventsQuery; | |
private Context mContext; | |
public PastEventFragment() { | |
} | |
// TODO: Customize parameter initialization | |
@SuppressWarnings("unused") | |
public static PastEventFragment newInstance() { | |
PastEventFragment fragment = new PastEventFragment(); | |
Bundle args = new Bundle(); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setHasOptionsMenu(true); | |
mDatabase = FirebaseDatabase.getInstance().getReference(MARATHON_EVENT_DATABASE); | |
mEventsQuery = mDatabase.child(DateHelper.getCurrentYear()).orderByChild("date").startAt(DateHelper.getCurrentDate()); | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
final View view = inflater.inflate(R.layout.fragment_past_event_list, container, false); | |
if(savedInstanceState == null) { | |
addFirebaseEventListener(); | |
} | |
return view; | |
} | |
private void addFirebaseEventListener() { | |
mEventsQuery.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
// This method is called once with the initial value and again | |
// whenever data at this location is updated. | |
mEventList = new ArrayList<Event>(); | |
if(dataSnapshot.exists()) { | |
for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) { | |
Event event = eventSnapshot.getValue(Event.class); | |
mEventList.add(event); | |
; | |
} | |
// Set Adapter | |
} | |
} | |
@Override | |
public void onCancelled(DatabaseError error) { | |
Log.w(TAG, "Failed to read value.", error.toException()); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment