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);
}
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;
}
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);
}
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) {
}
});
}
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;
}
Aggregations