private void getPosts() { Firebase database = new Firebase("https://vivid-heat-3338.firebaseio.com/"); Query query; if (spinnerdisplaydisplay.equals("recent")) { query = database.orderByChild("timestamp").limitToLast(55); } else { query = database.orderByChild("votes").limitToFirst(55); } // Attach an listener to read the data at our posts reference query.addListenerForSingleValueEvent( new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { aList.clear(); for (DataSnapshot postSnapshot : snapshot.child("posts").getChildren()) { final Post post = postSnapshot.getValue(Post.class); aList.add( 0, new PostElement( post.getContent(), (String) snapshot .child("users") .child(post.getUser_id()) .child("profilePic") .getValue(), (String) snapshot .child("users") .child(post.getUser_id()) .child("userName") .getValue(), post.getTimestamp(), String.valueOf(post.getVotes()), post.getPost_id())); adapter.notifyDataSetChanged(); } } @Override public void onCancelled(FirebaseError firebaseError) { System.out.println("The read failed: " + firebaseError.getMessage()); } }); }
@Override protected void onStart() { super.onStart(); final ListView mainList = (ListView) findViewById(R.id.main_list); listAdapter = new FriendFoodBoxItemListAdapter( usersRef.orderByChild("expirationDate"), this, R.layout.food_friend_view, userName); mainList.setAdapter(listAdapter); }
@Override public Comment[] getCommentsOnPost(String postId) { Query query = commentsFirebaseRef.orderByChild("postId").equalTo(postId); return executeCommentQuerySynchronously(query); }
@Override public Comment[] getCommentsByUser(String authorId) { Query query = commentsFirebaseRef.orderByChild("authorId").equalTo(authorId); return executeCommentQuerySynchronously(query); }
@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_active_list_details); /* Get the push ID from the extra passed by ShoppingListFragment */ Intent intent = this.getIntent(); mListId = intent.getStringExtra(Constants.KEY_LIST_ID); if (mListId == null) { /* No point in continuing without a valid ID. */ finish(); return; } /** Create Firebase references */ mCurrentListRef = new Firebase(Constants.FIREBASE_URL_USER_LISTS).child(mEncodedEmail).child(mListId); mCurrentUserRef = new Firebase(Constants.FIREBASE_URL_USERS).child(mEncodedEmail); mSharedWithRef = new Firebase(Constants.FIREBASE_URL_LISTS_SHARED_WITH).child(mListId); Firebase listItemsRef = new Firebase(Constants.FIREBASE_URL_SHOPPING_LIST_ITEMS).child(mListId); /** Link layout elements from XML and setup the toolbar */ initializeScreen(); /** Setup the adapter */ mActiveListItemAdapter = new ActiveListItemAdapter( this, ShoppingListItem.class, R.layout.single_active_list_item, listItemsRef.orderByChild(Constants.FIREBASE_PROPERTY_BOUGHT_BY), mListId, mEncodedEmail); /* Create ActiveListItemAdapter and set to listView */ mListView.setAdapter(mActiveListItemAdapter); /** * Add ValueEventListeners to Firebase references to control get data and control behavior and * visibility of elements */ /* Save the most up-to-date version of current user in mCurrentUser */ mCurrentUserRefListener = mCurrentUserRef.addValueEventListener( new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { User currentUser = dataSnapshot.getValue(User.class); if (currentUser != null) mCurrentUser = currentUser; else finish(); } @Override public void onCancelled(FirebaseError firebaseError) { Log.e( LOG_TAG, getString(R.string.log_error_the_read_failed) + firebaseError.getMessage()); } }); final Activity thisActivity = this; /** * Save the most recent version of current shopping list into mShoppingList instance variable an * update the UI to match the current list. */ mCurrentListRefListener = mCurrentListRef.addValueEventListener( new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { /** * Saving the most recent version of current shopping list into mShoppingList if * present finish() the activity if the list is null (list was removed or unshared * by it's owner while current user is in the list details activity) */ ShoppingList shoppingList = snapshot.getValue(ShoppingList.class); if (shoppingList == null) { finish(); /** * Make sure to call return, otherwise the rest of the method will execute, even * after calling finish. */ return; } mShoppingList = shoppingList; /** * Pass the shopping list to the adapter if it is not null. We do this here because * mShoppingList is null when first created. */ mActiveListItemAdapter.setShoppingList(mShoppingList); /* Check if the current user is owner */ mCurrentUserIsOwner = Utils.checkIfOwner(shoppingList, mEncodedEmail); /* Calling invalidateOptionsMenu causes onCreateOptionsMenu to be called */ invalidateOptionsMenu(); /* Set title appropriately. */ setTitle(shoppingList.getListName()); HashMap<String, User> usersShopping = mShoppingList.getUsersShopping(); if (usersShopping != null && usersShopping.size() != 0 && usersShopping.containsKey(mEncodedEmail)) { mShopping = true; mButtonShopping.setText(getString(R.string.button_stop_shopping)); mButtonShopping.setBackgroundColor( ContextCompat.getColor(ActiveListDetailsActivity.this, R.color.dark_grey)); } else { mButtonShopping.setText(getString(R.string.button_start_shopping)); mButtonShopping.setBackgroundColor( ContextCompat.getColor(ActiveListDetailsActivity.this, R.color.primary_dark)); mShopping = false; } setWhosShoppingText(mShoppingList.getUsersShopping()); } @Override public void onCancelled(FirebaseError firebaseError) { Log.e( LOG_TAG, getString(R.string.log_error_the_read_failed) + firebaseError.getMessage()); } }); mSharedWithListener = mSharedWithRef.addValueEventListener( new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { mSharedWithUsers = new HashMap<String, User>(); for (DataSnapshot currentUser : dataSnapshot.getChildren()) { mSharedWithUsers.put(currentUser.getKey(), currentUser.getValue(User.class)); } mActiveListItemAdapter.setSharedWithUsers(mSharedWithUsers); } @Override public void onCancelled(FirebaseError firebaseError) { Log.e( LOG_TAG, getString(R.string.log_error_the_read_failed) + firebaseError.getMessage()); } }); /** Set up click listeners for interaction. */ /* Show edit list item name dialog on listView item long click event */ mListView.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { /* Check that the view is not the empty footer item */ if (view.getId() != R.id.list_view_footer_empty) { ShoppingListItem shoppingListItem = mActiveListItemAdapter.getItem(position); if (shoppingListItem != null) { /* If the person is the owner and not shopping and the item is not bought, then they can edit it. */ if (shoppingListItem.getOwner().equals(mEncodedEmail) && !mShopping && !shoppingListItem.isBought()) { String itemName = shoppingListItem.getItemName(); String itemId = mActiveListItemAdapter.getRef(position).getKey(); showEditListItemNameDialog(itemName, itemId); return true; } } } return false; } }); /* Perform buy/return action on listView item click event if current user is shopping. */ mListView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { /* Check that the view is not the empty footer item */ if (view.getId() != R.id.list_view_footer_empty) { final ShoppingListItem selectedListItem = mActiveListItemAdapter.getItem(position); String itemId = mActiveListItemAdapter.getRef(position).getKey(); if (selectedListItem != null) { /* If current user is shopping */ if (mShopping) { /* Create map and fill it in with deep path multi write operations list */ HashMap<String, Object> updatedItemBoughtData = new HashMap<String, Object>(); /* Buy selected item if it is NOT already bought */ if (!selectedListItem.isBought()) { updatedItemBoughtData.put(Constants.FIREBASE_PROPERTY_BOUGHT, true); updatedItemBoughtData.put(Constants.FIREBASE_PROPERTY_BOUGHT_BY, mEncodedEmail); } else { /* Return selected item only if it was bought by current user */ if (selectedListItem.getBoughtBy().equals(mEncodedEmail)) { updatedItemBoughtData.put(Constants.FIREBASE_PROPERTY_BOUGHT, false); updatedItemBoughtData.put(Constants.FIREBASE_PROPERTY_BOUGHT_BY, null); } } /* Do update */ Firebase firebaseItemLocation = new Firebase(Constants.FIREBASE_URL_SHOPPING_LIST_ITEMS) .child(mListId) .child(itemId); firebaseItemLocation.updateChildren( updatedItemBoughtData, new Firebase.CompletionListener() { @Override public void onComplete(FirebaseError firebaseError, Firebase firebase) { if (firebaseError != null) { Log.d( LOG_TAG, getString(R.string.log_error_updating_data) + firebaseError.getMessage()); } } }); } } } } }); }
// Obtiene e inicializa las vistas. private void initVistas(View v) { lblNuevoAlumno = (TextView) v.findViewById(R.id.lblNuevoAlumno); lblNuevoAlumno.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { listener.onAgregarAlumno(); } }); RecyclerView lstAlumnos = (RecyclerView) v.findViewById(R.id.lstAlumnos); lstAlumnos.setHasFixedSize(true); Firebase ref = new Firebase(App.getUidAlumnosUrl()); mAdaptador = new AlumnosAdapter(ref.orderByChild("nombre")); mAdaptador.setOnItemClickListener(this); mAdaptador.setOnItemLongClickListener(this); mObservador = new RecyclerView.AdapterDataObserver() { @Override public void onItemRangeInserted(int positionStart, int itemCount) { super.onItemRangeInserted(positionStart, itemCount); lblNuevoAlumno.setVisibility(mAdaptador.isEmpty() ? View.VISIBLE : View.INVISIBLE); } @Override public void onItemRangeRemoved(int positionStart, int itemCount) { super.onItemRangeRemoved(positionStart, itemCount); lblNuevoAlumno.setVisibility(mAdaptador.isEmpty() ? View.VISIBLE : View.INVISIBLE); } @Override public void onChanged() { super.onChanged(); lblNuevoAlumno.setVisibility(mAdaptador.isEmpty() ? View.VISIBLE : View.INVISIBLE); } }; mAdaptador.registerAdapterDataObserver(mObservador); lstAlumnos.setAdapter(mAdaptador); mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); lstAlumnos.setLayoutManager(mLayoutManager); lstAlumnos.addItemDecoration( new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL)); lstAlumnos.setItemAnimator(new DefaultItemAnimator()); // Drag & drop y Swipe to dismiss. ItemTouchHelper itemTouchHelper = new ItemTouchHelper( new ItemTouchHelper.SimpleCallback( ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.RIGHT) { @Override public boolean onMove( RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { // Se elimina el elemento. eliminarAlumno(viewHolder.getAdapterPosition()); } }); itemTouchHelper.attachToRecyclerView(lstAlumnos); // Hide / show FAB on scrolling. mHidingScrollListener = new HidingScrollListener() { @Override public void onHide() { listener.onHideFAB(); } @Override public void onShow() { listener.onShowFAB(); } }; lstAlumnos.addOnScrollListener(mHidingScrollListener); }
public void loadTemplatesFromDB(ChildEventListener listener) { db = DBconn.getInstance().getFbConnection().child("Templates"); Query queryRef = db.orderByChild("name"); queryRef.addChildEventListener(listener); }