Search in sources :

Example 81 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project priend by TakoJ.

the class PostListFragment method onActivityCreated.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Set up Layout Manager, reverse layout
    mManager = new LinearLayoutManager(getActivity());
    mManager.setReverseLayout(true);
    mManager.setStackFromEnd(true);
    mRecycler.setLayoutManager(mManager);
    // Set up FirebaseRecyclerAdapter with the Query
    Query postsQuery = getQuery(mDatabase);
    mAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.item_post, PostViewHolder.class, postsQuery) {

        @Override
        protected void populateViewHolder(final PostViewHolder viewHolder, final Post model, final int position) {
            final DatabaseReference postRef = getRef(position);
            // Set click listener for the whole post view
            final String postKey = postRef.getKey();
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // Launch PostDetailActivity
                    Intent intent = new Intent(getActivity(), PostDetailActivity.class);
                    intent.putExtra(PostDetailActivity.EXTRA_POST_KEY, postKey);
                    startActivity(intent);
                }
            });
            // Determine if the current user has liked this post and set UI accordingly
            if (model.stars.containsKey(getUid())) {
                viewHolder.starView.setImageResource(R.mipmap.ic_toggle_star_24);
            } else {
                viewHolder.starView.setImageResource(R.mipmap.ic_toggle_star_outline_24);
            }
            // Bind Post to ViewHolder, setting OnClickListener for the star button
            viewHolder.bindToPost(model, new View.OnClickListener() {

                @Override
                public void onClick(View starView) {
                    // Need to write to both places the post is stored
                    DatabaseReference globalPostRef = mDatabase.child("posts").child(postRef.getKey());
                    DatabaseReference userPostRef = mDatabase.child("user-posts").child(model.uid).child(postRef.getKey());
                    // Run two transactions
                    onStarClicked(globalPostRef);
                    onStarClicked(userPostRef);
                }
            });
        }
    };
    mRecycler.setAdapter(mAdapter);
}
Also used : PostViewHolder(com.example.management.viewholder.PostViewHolder) Query(com.google.firebase.database.Query) DatabaseReference(com.google.firebase.database.DatabaseReference) Post(com.example.management.models.Post) Intent(android.content.Intent) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View)

Example 82 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project NPSmiles by bmcglynn1.

the class NewsFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View viewer = inflater.inflate(R.layout.activity_news_feed, container, false);
    View popupLayout = inflater.inflate(R.layout.popup_layout_logged_in, null);
    final ListView list = (ListView) viewer.findViewById(R.id.list);
    final ArrayList<NewsMessage> newsList = new ArrayList<>();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Reference");
    mAuth = FirebaseAuth.getInstance();
    if (mAuth.getCurrentUser() != null) {
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PopupMenu popup = new PopupMenu(parent.getContext(), view);
                popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
                final TextView title = (TextView) view.findViewById(R.id.title);
                final TextView message = (TextView) view.findViewById(R.id.information);
                if (mAuth.getCurrentUser() != null) {
                    popup.show();
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch(item.getItemId()) {
                                case R.id.EditMenu:
                                    // refresh view if needed
                                    break;
                                case R.id.DeleteMenu:
                                    // delete from firebase
                                    // refresh view
                                    FirebaseDatabase database = FirebaseDatabase.getInstance();
                                    DatabaseReference myRef = database.getReference("Reference");
                                    Query titleQuery = myRef.child(title.getText().toString());
                                    titleQuery.addListenerForSingleValueEvent(new ValueEventListener() {

                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {
                                            for (DataSnapshot titleSnapshot : dataSnapshot.getChildren()) {
                                                dataSnapshot.getRef().removeValue();
                                            }
                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {
                                        }
                                    });
                                    break;
                            }
                            return true;
                        }
                    });
                }
            }
        });
    }
    myRef.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            newsList.clear();
            for (DataSnapshot newsItem : dataSnapshot.getChildren()) {
                NewsMessage newsMessage = new NewsMessage();
                for (DataSnapshot item : newsItem.getChildren()) {
                    if (item.getKey().equals("title")) {
                        newsMessage.setTitle((String) item.getValue());
                    } else if (item.getKey().equals("message")) {
                        newsMessage.setMessage((String) item.getValue());
                    } else {
                        newsMessage.setDate((String) item.getValue());
                    }
                }
                newsList.add(newsMessage);
            }
            Collections.sort(newsList);
            CustomAdapter adapter = new CustomAdapter(context, newsList);
            list.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    return viewer;
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) Query(com.google.firebase.database.Query) DatabaseReference(com.google.firebase.database.DatabaseReference) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ValueEventListener(com.google.firebase.database.ValueEventListener) PopupMenu(android.widget.PopupMenu)

Example 83 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project NPSmiles by bmcglynn1.

the class CreatePost method addPost.

public void addPost() {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("Reference");
    DatabaseReference childRef = myRef.child(newsMessage.getTitle());
    childRef.setValue(newsMessage);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 84 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project iNGAGE by davis123123.

the class ChatActivity method onUpvoteClick.

//modify unblock functions here
@Override
public void onUpvoteClick(int p) {
    //Log.d("vote" , "up : ");
    //get correct chat msg with ith key from chatmessage helper
    ChatMessageHelper chatMessageHelper = (ChatMessageHelper) chatAdapter.getItem(p);
    String chat_key = chatMessageHelper.getMessageID();
    DatabaseReference message_root = root.child(chat_key);
    //get upvote data
    DatabaseReference upvote_count = message_root.child("upvotes");
    upvote_count.runTransaction(new Transaction.Handler() {

        @Override
        public Transaction.Result doTransaction(MutableData currentData) {
            Log.d("Data", String.valueOf(currentData));
            if (currentData.getValue() == null) {
                currentData.setValue(1);
            } else {
                currentData.setValue((Long) currentData.getValue() + 1);
            }
            //we can also abort by calling Transaction.abort()
            return Transaction.success(currentData);
        }

        //TODO:Error handle here
        @Override
        public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
        }
    });
}
Also used : ChatMessageHelper(ingage.ingage20.helpers.ChatMessageHelper) DatabaseError(com.google.firebase.database.DatabaseError) Transaction(com.google.firebase.database.Transaction) DatabaseReference(com.google.firebase.database.DatabaseReference) MutableData(com.google.firebase.database.MutableData) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 85 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project MadMax by deviz92.

the class FirebaseUtils method addComment.

/*
        END EVENT
     */
/*
        COMMENT
     */
public String addComment(Comment comment) {
    Log.d(TAG, "addComment");
    final String ID = databaseReference.child("comments").push().getKey();
    DatabaseReference commentReference = databaseReference.child("comments").child(ID);
    DatabaseReference expenseReference = databaseReference.child("expenses").child(comment.getExpenseID());
    commentReference.setValue(comment);
    expenseReference.child("comments").child(ID).setValue(true);
    return ID;
}
Also used : DatabaseReference(com.google.firebase.database.DatabaseReference)

Aggregations

DatabaseReference (com.google.firebase.database.DatabaseReference)157 DatabaseError (com.google.firebase.database.DatabaseError)95 DataSnapshot (com.google.firebase.database.DataSnapshot)87 ValueEventListener (com.google.firebase.database.ValueEventListener)61 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)50 HashMap (java.util.HashMap)24 View (android.view.View)18 MutableData (com.google.firebase.database.MutableData)17 Transaction (com.google.firebase.database.Transaction)17 Intent (android.content.Intent)16 FirebaseUser (com.google.firebase.auth.FirebaseUser)13 ArrayList (java.util.ArrayList)12 TextView (android.widget.TextView)11 Query (com.google.firebase.database.Query)11 Map (java.util.Map)11 ProgressDialog (android.app.ProgressDialog)10 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)10 ChildEventListener (com.google.firebase.database.ChildEventListener)9 SharedPreferences (android.content.SharedPreferences)8 ChatMessageHelper (ingage.ingage20.helpers.ChatMessageHelper)8