Remove item from firebase recyclerview - java

public class ChatFragment extends Fragment {
View chatview;
String CurrentUserID;
FirebaseAuth auth;
String time , date ,CurrentDate;
RecyclerView recyclerView;
DatabaseReference ContactRef,UserRef;
FloatingActionButton floatingActionButton;
List<Contacts> contacts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
chatview =inflater.inflate(R.layout.fragment_chat, container, false);
auth= FirebaseAuth.getInstance();
CurrentUserID=auth.getCurrentUser().getUid();
ContactRef= FirebaseDatabase.getInstance().getReference().child("Contacts").child(CurrentUserID);
UserRef= FirebaseDatabase.getInstance().getReference().child("Users");
recyclerView=chatview.findViewById(R.id.chat_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
floatingActionButton=chatview.findViewById(R.id.chat_float_btn);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SendtoFindFriendsActivity();
}
});
return chatview;
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions<Contacts> options=new FirebaseRecyclerOptions.Builder<Contacts>()
.setQuery(ContactRef,Contacts.class)
.build();
FirebaseRecyclerAdapter<Contacts,ChatViewHolder> adapter=new FirebaseRecyclerAdapter<Contacts, ChatViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ChatViewHolder holder, #SuppressLint("RecyclerView") int position, #NonNull Contacts model) {
String userid=getRef(position).getKey();
final String[] image = {""};
final String[] name = {""};
UserRef.child(userid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.hasChild("image"))
{
image[0] =snapshot.child("image").getValue().toString();
GetImage(image[0], holder.profileImage );
}
name[0] =snapshot.child("name").getValue().toString();
holder.lastSeen.setText("Date"+"Time");
holder.userName.setText(name[0]);
holder.userStatus.setText(snapshot.child("status").getValue().toString());
holder.lastSeen.setVisibility(View.VISIBLE);
if (snapshot.child("userState").hasChild("state"))
{
String state=snapshot.child("userState").child("state").getValue().toString();
date=snapshot.child("userState").child("date").getValue().toString();
time=snapshot.child("userState").child("time").getValue().toString();
if (state.equals("online"))
{
holder.lastSeen.setVisibility(View.INVISIBLE);
holder.online.setVisibility(View.VISIBLE);
}
else if (state.equals("offline"))
{
holder.lastSeen.setVisibility(View.VISIBLE);
holder.online.setVisibility(View.INVISIBLE);
Calendar calendar=Calendar.getInstance();
SimpleDateFormat dateFormat=new SimpleDateFormat("MMM dd,yyyy");
CurrentDate=dateFormat.format(calendar.getTime());
if (CurrentDate.equals(date))
{
holder.lastSeen.setText(time.toLowerCase(Locale.ROOT));
}
else
{
holder.lastSeen.setText(date);
}
}
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent chat=new Intent(getActivity(), ChatActivity.class);
chat.putExtra("uid",userid);
chat.putExtra("name",name[0]);
chat.putExtra("image",image[0]);
startActivity(chat);
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
#NonNull
#Override
public ChatViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.user_display_layout,parent,false);
ChatViewHolder viewHolder=new ChatViewHolder(view);
return viewHolder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
public static class ChatViewHolder extends RecyclerView.ViewHolder
{
TextView userName,userStatus,lastSeen;
CircleImageView profileImage;
ImageView online;
public ChatViewHolder(#NonNull View itemView) {
super(itemView);
userName=itemView.findViewById(R.id.user_profile_name);
userStatus=itemView.findViewById(R.id.user_profile_status);
profileImage=itemView.findViewById(R.id.users_profile_image);
userStatus.setVisibility(View.VISIBLE);
lastSeen=itemView.findViewById(R.id.user_profile_lastseen);
lastSeen.setVisibility(View.VISIBLE);
online=itemView.findViewById(R.id.online_icon);
}
}
private void SendtoFindFriendsActivity() {
Intent intent=new Intent(getActivity(), FindFriends.class);
startActivity(intent);
}
private void GetImage(String currentUser, CircleImageView imageView) {
StorageReference storageReference = FirebaseStorage.getInstance().getReference().
child("Profile Images/" + currentUser + ".jpg");
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Glide.with(getContext()).load(uri).into(imageView);
}
});
}
}
can anyone tell me how Can I remove an item from firebase recyclerview .I am new in java app development that's why I am facing problem.
can anyone tell me how Can I remove an item from firebase recyclerview .I am new in java app development that's why I am facing problem.can anyone tell me how Can I remove an item from firebase recyclerview .I am new in java app development that's why I am facing problem.

If I'm understanding the adapter (FirebaseRecyclerAdapter) correctly, it will always display the designated documents in the recyclerView, so in order to remove an item from that list, you'll need to delete it from the database (in your case 'Realtime Database'). Otherwise, anytime you'll remove it from the list localy, the adapter will quicly put it back in.
So in order to remove an item from this list my approach will be:
Assuming we need to delete a single item at a time (and not a batch of items)
figure out how to impliment the desired UI\UX element that will start the delete proccess.
echo the corresponding UID of the item to the viewmodel
delete the item from the database via the viewmodel
echo back to the front the operation result in-order to provide some feedback to the user (a popup displaying: "item removed!", "connection error" and such)
If I'm not mistaken, after the item is removed from the database, the adapter should automatically remove it from the list displayed to the user.
If it dosen't work, or this solution is sub-optimal for your specific endeavour i think you'll have no choice but to make your own adapter.

Related

Realtime Display Value Change form Firebase

I am new to android studio and currently watching tutorials. As of now I have a Shopping cart activity where I can increment, decrement and delete items in cart. The thing is whenever I delete, add or deduct an item, the cart values does not update. I still need to change activities to see the changes. Also the item that I deleted still appears at the bottom of recyclerview items after the dialog appearance unless I change the activity and go back. Please help me how to apply real time update. Below are my class files for the cart activity and adapter class.
CartActivity.java
#Override
protected void onStop() {
if (EventBus.getDefault().hasSubscriberForEvent(MyUpdateCartEvent.class))
EventBus.getDefault().removeStickyEvent(MyUpdateCartEvent.class);
EventBus.getDefault().unregister(this);
super.onStop();
}
#Subscribe(threadMode = ThreadMode.MAIN_ORDERED, sticky = true)
public void onUpdateCart(MyUpdateCartEvent event) {
loadCartFromFirebase();
}
private void loadCartFromFirebase() {
fAuth = FirebaseAuth.getInstance();
if(fAuth.getCurrentUser() != null){
userId = fAuth.getCurrentUser().getUid();
} else {
userId = "UNIQUE_USER_ID";
}
List<CartModel> cartModels = new ArrayList<>();
FirebaseDatabase.getInstance()
.getReference("cart")
.child(userId)
.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
for (DataSnapshot cartSnapshot : snapshot.getChildren()) {
CartModel cartModel = cartSnapshot.getValue(CartModel.class);
cartModel.setKey(cartSnapshot.getKey());
cartModels.add(cartModel);
}
cartLoadListener.onCartLoadSuccess(cartModels);
} else {
cartLoadListener.onCartLoadFailed("Cart Empty");
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
cartLoadListener.onCartLoadFailed(error.getMessage());
}
});
}
private void init() {
ButterKnife.bind(this);
cartLoadListener = this;
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recycler_cart.setLayoutManager(layoutManager);
purchScrnBtn.setOnClickListener(v -> startActivity(new Intent(this, PurchaseActivity.class)));
}
#Override
public void onCartLoadSuccess(List<CartModel> cartModelList) {
double sum = 0;
for (CartModel cartModel : cartModelList) {
sum += cartModel.getTotalPrice();
}
textTotal.setText(new StringBuilder("$").append(sum));
MyCartAdapter adapter = new MyCartAdapter(this, cartModelList);
recycler_cart.setAdapter(adapter);
}
MyCartAdapter.java
package com.example.cart.adapter;
public class CartActivity extends AppCompatActivity implements CartLoadListener {
public MyCartViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new MyCartViewHolder(LayoutInflater.from(context)
.inflate(R.layout.layout_cart_item, parent, false));
}
#Override
public void onBindViewHolder(#NonNull MyCartViewHolder holder, int position) {
holder.minus.setOnClickListener(v -> {
minusCartItem(holder,cartModelList.get(position));
});
holder.delete.setOnClickListener(v -> {
AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle("Delete item")
.setMessage("Do you really want to delete item")
.setNegativeButton("CANCEL", (dialogInterface, i) -> dialogInterface.dismiss())
.setPositiveButton("OK", (dialogInterface2, i) -> {
//Temp remove
notifyItemRemoved(position);
deleteFromFirebase(cartModelList.get(position));
dialogInterface2.dismiss();
}).create();
dialog.show();
});
}
private void deleteFromFirebase(CartModel cartModel) {
FirebaseDatabase.getInstance()
.getReference("cart")
.child(userId)
.child(cartModel.getKey())
.removeValue()
.addOnSuccessListener(aVoid -> EventBus.getDefault().postSticky(new MyUpdateCartEvent()));
}
#SuppressLint("SuspiciousIndentation")
private void minusCartItem(MyCartViewHolder holder, CartModel cartModel) {
if(cartModel.getQuantity() > 1)
cartModel.setQuantity(cartModel.getQuantity()-1);
cartModel.setTotalPrice(cartModel.getQuantity()*Float.parseFloat(cartModel.getPrice()));
//update quantity
holder.txtQuantity.setText(new StringBuilder().append(cartModel.getQuantity()));
updateFirebase(cartModel);
}
public class MyCartViewHolder extends RecyclerView.ViewHolder{
Unbinder unbinder;
public MyCartViewHolder(#NonNull View itemView) {
super(itemView);
unbinder = ButterKnife.bind(this, itemView);
}
}
}
The thing is whenever I delete, add or deduct an item, the cart values does not update.
That's because you load data from Firebase with addListenerForSingleValueEvent, which only loads the data when you call it. If you want to continue to monitor the data for changes, use addValueEventListener instead.

Android: Sending a cardview from one fragment to another

I need some help for a summer project
This is my Events fragment
This is my MyList fragment
I'm using a RecyclerView+Cardview to display the Events. The idea is that the user can click the big plus on the right side of each card, and the card would be displayed in the MyList fragment. I would like to ask if it's possible to transfer a card directly from one fragment to another? Also, both fragments are contained within the same activity, which makes it a little trickier(I haven't found any available solutions).
If that is not possible, another way is to transfer the reference type object contained in the CardView to the MyList fragment. However, this is even less straightforward. This is because the button is inflated in the adapter, but there is no reference type object created here. I have seen many tutorials on using the Parcelable interface, however, I don't know how to implement it here when I'm unable to even create the object in the adapter. The reference object is created in another activity and stored in Firebase before it is read and displayed.
I'm going to attach my EventsAdapter.java and EventsItem.java and EventsFragment.java code below, but please let me know if I should include more code to describe the problem.
Thanks for reading my very long post!!
public class EventsAdapter extends RecyclerView.Adapter<EventsAdapter.EventsViewHolder> implements Filterable {
private ArrayList<EventsItem> mEventsList;
private ArrayList<EventsItem> mEventsListFull;
private EventsAdapter.OnItemClickListener mListener;
private Context mContext;
private DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);
public interface OnItemClickListener {
void onItemClick(int position);
}
//the ViewHolder holds the content of the card
public static class EventsViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public ImageView mAddButton;
public TextView mTextView1;
public TextView mTextView2;
public TextView mTextView3;
public TextView mTextView4;
public TextView mTextView5;
public EventsViewHolder(Context context, View itemView, final EventsAdapter.OnItemClickListener listener) {
super(itemView);
final Context context1 = context;
mImageView = itemView.findViewById(R.id.imageView);
mAddButton = itemView.findViewById(R.id.image_add);
mTextView1 = itemView.findViewById(R.id.title);
mTextView2 = itemView.findViewById(R.id.event_description);
mTextView3 = itemView.findViewById(R.id.date);
mTextView4 = itemView.findViewById(R.id.location);
mTextView5 = itemView.findViewById(R.id.time);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
mAddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str1 = mTextView1.getText().toString();
String str2 = mTextView2.getText().toString();
String str3 = mTextView3.getText().toString();
String str4 = mTextView4.getText().toString();
String str5 = mTextView5.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("title", str1);
bundle.putString("event description", str2);
bundle.putString("date", str3);
bundle.putString("location", str4);
bundle.putString("time", str5);
MylistFragment mlf = new MylistFragment();
mlf.setArguments(bundle);
}
});
}
}
//Constructor for EventsAdapter class. This ArrayList contains the
//complete list of items that we want to add to the View.
public EventsAdapter(Context context, ArrayList<EventsItem> EventsList) {
mEventsList = EventsList;
mContext = context;
mEventsListFull = new ArrayList<>(EventsList); // copy of EventsList for SearchView
}
//inflate the items in a EventsViewHolder
#NonNull
#Override
public EventsAdapter.EventsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_item, parent, false);
EventsAdapter.EventsViewHolder evh = new EventsAdapter.EventsViewHolder(mContext, v, mListener);
return evh;
}
#Override
public void onBindViewHolder(#NonNull EventsAdapter.EventsViewHolder holder, int position) {
EventsItem currentItem = mEventsList.get(position);
holder.mImageView.setImageResource(currentItem.getProfilePicture());
holder.mTextView1.setText(currentItem.getTitle());
holder.mTextView2.setText(currentItem.getDescription());
holder.mTextView3.setText(df.format(currentItem.getDateInfo()));
holder.mTextView4.setText(currentItem.getLocationInfo());
holder.mTextView5.setText(currentItem.getTimeInfo());
}
#Override
public int getItemCount() {
return mEventsList.size();
}
public class EventsItem implements Occasion, Parcelable {
//fields removed for brevity
//constructor removed for brevity
}
public EventsItem() {
}
public EventsItem(Parcel in) {
profilePicture = in.readInt();
timeInfo = in.readString();
hourOfDay = in.readInt();
minute = in.readInt();
locationInfo = in.readString();
title = in.readString();
description = in.readString();
}
public static final Creator<EventsItem> CREATOR = new Creator<EventsItem>() {
#Override
public EventsItem createFromParcel(Parcel in) {
return new EventsItem(in);
}
#Override
public EventsItem[] newArray(int size) {
return new EventsItem[size];
}
};
//getter methods have been removed for brevity
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(profilePicture);
dest.writeString(timeInfo);
dest.writeString(locationInfo);
dest.writeString(title);
dest.writeString(description);
dest.writeString(df.format(dateInfo));
dest.writeInt(hourOfDay);
dest.writeInt(minute);
}
}
public class EventsFragment extends Fragment {
ArrayList<EventsItem> EventsItemList;
FirebaseDatabase mDatabase;
DatabaseReference mDatabaseReference;
ValueEventListener mValueEventListener;
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private EventsAdapter mAdapter;
private View rootView;
public FloatingActionButton floatingActionButton;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_events, container, false);
mDatabase = FirebaseDatabase.getInstance();
mDatabaseReference = mDatabase.getReference().child("Events");
createEventsList();
buildRecyclerView();
floatingActionButton = rootView.findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), EventsAdder.class);
startActivity(intent);
}
});
mValueEventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
EventsItemList.add(snapshot.getValue(EventsItem.class));
}
EventsAdapter eventsAdapter = new EventsAdapter(getActivity(), EventsItemList);
mRecyclerView.setAdapter(eventsAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
};
mDatabaseReference.addValueEventListener(mValueEventListener);
setHasOptionsMenu(true);
Toolbar toolbar = rootView.findViewById(R.id.events_toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
return rootView;
}
public void createEventsList() {
EventsItemList = new ArrayList<>();
}
public void buildRecyclerView() {
mRecyclerView = rootView.findViewById(R.id.recyclerview);
mLayoutManager = new LinearLayoutManager(getContext());
mAdapter = new EventsAdapter(getActivity(), EventsItemList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
}
}
If you would like to see the same CardView within the MyListFragment, you could have the MyListFragment contain a RecyclerView, and reuse the same EventsAdapter and EventsViewHolder. The only difference is that rather than populating the adapter with all the children of the "Events" from your database, you would only populate it with the single Event that you want.
Also, since you have made your Event class implement parcelable, you do not need to manually create the bundle when clicking the plus button.
I am assuming you have a single Activity, and you simply want to replace the EventsFragment with the MyListFragment. Checkout the docs for replacing one fragment with another.
Step 1:
Extend your onItemClickListener to look like:
public interface OnItemClickListener {
void onItemClick(int position);
void onPlusButtonClick(int position);
}
and adjust the code in your EventsViewHolder constructor to look like this when the plus button is clicked:
mAddButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
// no need to manually create the bundle here
// you already have all the information you need
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onPlusButtonClick(position);
}
}
}
});
Step 2:
Implement our new method onPlusButtonClick. As per our discussion in the comments, it seems you do not implement this interface anywhere. You can implement it inside the constructor to your EventsAdapter:
public EventsAdapter(Context context, ArrayList<EventsItem> EventsList) {
mEventsList = EventsList;
mContext = context;
mEventsListFull = new ArrayList<>(EventsList); // copy of EventsList for SearchView
mListener = new OnItemClickListener() {
#Override
public void onItemClick() {
// handle clicking the entire view holder
// NOTE: inside your EventsViewHolder, it looks like you call this method on the entire itemView. This could 'swallow' the click on the plus button. You may need to adjust your code to handle this.
}
#Override
public void onPlusButtonClick(int position) {
MyListFragment myListFragment = new MyListFragment();
Event event = mEventsList.get(position);
Bundle bundle = new Bundle();
bundle.putExtra("event", event); // this will work due to implementing parcelable
myListFragment.setArguments(bundle);
// use mContext since im assuming we areinside adapter
// if in an Activity, no need to use context to get the fragment manager
FragmentTransaction transaction = mContext.getSupportFragmentManager().beginTransaction();
// Replace the EventsFragment with the MyListFragment
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, myListFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}
Step 3:
Inside your MyListFragments onCreateView() method:
#Override
public View onCreateView (
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState
) {
Bundle bundle = getArguments();
Event event = bundle.getExtra("event"); // again, will work due to implementing parcelable
// from here you should bind to a recycler view, and you can even reuse your adapter like so:
List<EventsItem> singleEventList = new List<EventsItem>();
singleEventList.add(event);
EventsAdapter adapter = new EventsAdapter(getActivity(), singleEventList);
// be sure to inflate and return your view here...
}
and you should be good to go!
I have left out bits of code here and there for simplicity.. but I hope this is understandable.
As a side note.. in your firebase database listener, it is bad practice to create a new EventsAdapter every single time your data is updated. Instead, you should update the data in the adapter with the new values. Do this by creating a public method inside the adapter such as replaceEvents(List<EventsItem> newEvents), and inside, replace mEventsList with the new events, then call notifyDataSetChanged().

Shuffle items from Firebase Database

How to shuffle items from Firebase Database?
Let's say my DB contains 4 items [1,2,3,4]. In my RecyclerView, I want to display shuffled, something like [2,3,1,4]. How can I do that?
This is the code in my Fragment. So it works retrieving data but I need to shuffle it before displaying.
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
#Override
public View onCreateView(#NotNull LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_store, viewGroup, false);
setHasOptionsMenu(false);
mRecyclerView = rootView.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Store");
}
return rootView;
}
For retrieving data from Firebase Database I am using the following code:
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions<Store2> options =
new FirebaseRecyclerOptions.Builder<Store2>()
.setQuery(mRef, Store2.class)
.build();
FirebaseRecyclerAdapter<Store2, StoreHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Store2, StoreHolder>(options) {
#NonNull
#Override
public StoreHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.store_gridview, parent, false);
return new StoreHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull final StoreHolder holder, int position, #NonNull final Store2 model) {
holder.setDetails(getContext(), model.getImage(), model.getName(), model.getPrice());
}
};
firebaseRecyclerAdapter.startListening();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
There are no option available for shuffling data in firebase.
But you can do it locally .
Pass your list in this.
Collections.shuffle(arrayList);
When you are using the following reference:
mRef = mFirebaseDatabase.getReference("Store");
It means that you are trying to display all children that exist under Store node. If under this node you store children that are added using the push() method, the default ordering is chronological, since every pushed id contains a time component. If you want to change this behaviour, you need to get all the items and shuffle them manually.
As #DougStevenson mentioned in his comment, you have to use the same reference, get all items, add them to a list, shuffle them and then use a ListAdapter for that.
Please also note, that when you pass a DocumentReference or a Query object to the setQuery() method, where is no further mechanism that can help you shuffle your items. Unfortunately, you'll lose the benefits of the Firebase-UI library but you'll get your work done.
As #DougStevenson and #frankenstein mentioned, I added all items into a list, then I was able the shuffle it using Collections.shuffle(arrayList);
I couldn't do it using FirebaseRecyclerAdapter, and I needed a custom Adapter for my RecyclerView.ViewHolder.
What is the downside of not using FirebaseRecyclerAdapter? Or is it any? Maybe someone with experience can answer this question.
My fragment:
private View rootView;
private RecyclerView recyclerView;
private StoreAdapter storeAdapter;
private ArrayList<Store> arrayStore;
#Override
public View onCreateView(#NotNull LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_store, viewGroup, false);
setHasOptionsMenu(false);
recyclerView = rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Data");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
arrayStore = new ArrayList<>();
for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
Store store = snapshot.getValue(Store.class);
arrayStore.add(store);
}
storeAdapter = new StoreAdapter(getActivity() ,arrayStore);
recyclerView.setAdapter(storeAdapter);
Collections.shuffle(arrayStore, new Random(3));
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
return rootView;
}
My adapter:
private final Context context;
private ArrayList<Store> arrayStore;
public StoreAdapter(Context context, ArrayList<Store> arrayStore) {
this.context = context;
this.arrayStore = arrayStore;
}
#NonNull
#Override
public StoreAdapter.GameViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new StoreAdapter.GameViewHolder(LayoutInflater.from(context).inflate(
R.layout.store_gridview, viewGroup, false));
}
#Override
public void onBindViewHolder(#NonNull final StoreAdapter.GameViewHolder viewHolder, final int position) {
setColor(viewHolder);
viewHolder.itemName.setText(arrayStore.get(position).getName());
Glide.with(viewHolder.view.getContext())
.load(arrayStore.get(position).getImage())
.thumbnail(0.75f)
.into(viewHolder.itemImage);
viewHolder.view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendData(viewHolder, position);
}
});
}
#Override
public int getItemCount() {
return arrayStore.size();
}
class GameViewHolder extends RecyclerView.ViewHolder {
private final View view;
private final ImageView itemImage, itemRarity;
private final TextView itemName;
GameViewHolder(#NonNull View itemView) {
super(itemView);
this.view = itemView;
itemRarity = view.findViewById(R.id.itemRarity);
itemImage = view.findViewById(R.id.itemImage);
itemName = view.findViewById(R.id.itemName);
}
}
private void sendData(#NonNull final StoreAdapter.GameViewHolder viewHolder, int position) {
if (viewHolder.getAdapterPosition() != RecyclerView.NO_POSITION) {
Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra("name", arrayStore.get(position).getName());
intent.putExtra("rarity", arrayStore.get(position).getRarity());
intent.putExtra("item", arrayStore.get(position).getImage());
intent.putExtra("price", arrayStore.get(position).getPrice());
intent.putExtra("key", "Store");
context.startActivity(intent);
}
}
private void setColor(#NonNull final StoreAdapter.GameViewHolder viewHolder) {
String rarity = arrayStore.get(viewHolder.getAdapterPosition()).getRarity();
if (rarity.contains("legendary")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_legendary_gradient);
} else if (rarity.contains("classic")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_classic_gradient);
} else if (rarity.contains("epic")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_epic_gradient);
} else if (rarity.contains("elite")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_elite_gradient);
} else if (rarity.contains("rare")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_rare_gradient);
} else if (rarity.contains("special")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_special_gradient);
}
}
My bean class:
private String image, name, price, rarity;
public Store() {
}
public String getImage() {
return image;
}
public String getName() {
return name;
}
public String getPrice() {
return price;
}
public String getRarity() {
return rarity;
}

Android - Firebase - Send Users to Chat Room

Aim
Allowing the Users to access their selected Group Chat. Once the user clicks on the Group Chat name, they will be entered into that Group Chat.
Database Tree
As shown in the Database Tree, the Currently Signed In user will be shown a list of Group Chat names that have been created.
I have an Admin Account to create these Group Chats for the users.
The Android, Asia, and Europe group chats that are seen within the Database ARE NOT fixed variables. They are names. A newly Group Chat name could be "Earth".
Therefore there is no way of calling it by a variable other than calling it by the Node itself.
Screenshot of Application
List of Group Chats 2. Entering a Group Chat
Flow of Activities
GroupFragment ---> Chat Activity
GroupFragment <--- Chat Activity
Flow of Application
User--->LoginActivity--->UserActivity--->GroupFrag--->GroupChatActivity
At the (GroupFrag--->GroupChatActivity) The user must select a Group Chat name within the GroupFrag to enter the GroupChatActivity
The user must select a Group Chat name within the GroupFrag to enter the GroupChatActivity
Description
The users will be able to select a Group Chat name (from GroupFragment) and the app will bring the user into the Group Chat itself (into Chat Activity). The User will be able to go back to the GroupFragment and select another desired Group.
(Group Chat names are NOT FIXED -- They're not a node that can be called from)
Problem
I am unable to select the Group Chat names after it was prompt within the Fragment, which will then bring me to the Group Chat.
Group Fragment
#Override
public void onStart() {
super.onStart();
class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.MyHolder> {
ArrayList<String> list;
public GroupAdapter(ArrayList<String> list) {
this.list = list;
}
#Override
public GroupAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_groups, parent, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.setText(list.get(position));
}
#Override
public int getItemCount() {
return list.size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView nameTextView;
public MyHolder(View itemView) {
super(itemView);
nameTextView = itemView.findViewById(R.id.groupChatNameTxt);
}
public void setText(String groupName) {
nameTextView.setText(groupName);
}
}
}
jGroupsDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> groupChatNames = new ArrayList<>();
for (DataSnapshot child : dataSnapshot.getChildren()) {
groupChatNames.add(child.getKey());
}
GroupAdapter adapter = new GroupAdapter(groupChatNames);
jGroupChatList.setAdapter(adapter);
//I'm not sure where to write a code for userID
//final String usersID = getRef(position).getKey();
// When the user clicks on one of the group chat names, he/she will be sent to the Chat Activity
jGroupChatList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentUserProfile = new Intent(getActivity(), ChatActivity.class);
intentUserProfile.putExtra("groupChatName",groupName);
intentUserProfile.putExtra("neighbourhood", neighbourhood);
intentUserProfile.putExtra("usersName", usersName);
intentUserProfile.putExtra("usersID", usersID);
startActivity(intentUserProfile);
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Chat Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
jGroupChatName = getIntent().getExtras().get("groupChatName").toString();
jUserID = getIntent().getExtras().get("usersID").toString();
jUserName = getIntent().getExtras().get("usersName").toString();
jUserNeighbourhood = getIntent().getExtras().get("neighbourhood").toString();
jChatRoot = FirebaseDatabase.getInstance().getReference().child(jGroupChatName);
jChatToolbar = (Toolbar) findViewById(R.id.allUSersToolBar);
setSupportActionBar(jChatToolbar);
getSupportActionBar().setTitle(jGroupChatName); // Show the name of the selected group name
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Additional Comments
Online Tutorial
I have watched an Android Firebase Group Chat tutorial on youtube. The link is https://www.youtube.com/watch?v=wVCz1a3ogqk. But it does not provide some the features/ functions whic I am trying to implement.
Linked Question
Android - Firebase - Prompting Group Chat Names
Future Implementations
For future implementations, I would like to send and retrieve the messages and prompt it into the group chat for the users to view like a real group chat. But of course, I will leave that for another Question.
You can update your adapter and viewholder implementation as follows:
public class Adapter extends RecyclerView.Adapter<Adapter.MyHolder> {
Context context;
ArrayList<String> list;
public Adapter(Context context, ArrayList<String> list) {
this.context = context;
this.list = list;
}
#Override
public Adapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_groups, parent, false);
MyHolder holder = new MyHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.setText(list.get(position));
}
#Override
public int getItemCount() {
return list.size();
}
class MyHolder extends RecyclerView.ViewHolder {
TextView nameTextView;
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
String groupName = list.get(getAdapterPosition());
Intent intentUserProfile = new Intent(context, MainActivity.class);
intentUserProfile.putExtra("groupChatName", groupName);
// If fixed, you should pass these values to adapter's constructor
// intentUserProfile.putExtra("neighbourhood", neighbourhood);
// intentUserProfile.putExtra("usersName", usersName);
// intentUserProfile.putExtra("usersID", usersID);
context.startActivity(intentUserProfile);
}
};
public MyHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(onClickListener);
nameTextView = (TextView) itemView.findViewById(R.id.groupChatNameTxt);
}
public void setText(String groupName) {
nameTextView.setText(groupName);
}
}
}
You also have to update this line in your GroupFragment:
GroupAdapter adapter = new GroupAdapter(getActivity(), groupChatNames);
This is another solution that you can implement inside your fragment so that you can put extras in intent (actually modified from your former question):
#Override
public void onStart() {
super.onStart();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference groupChatsRef = rootRef.child("Group Chats");
FirebaseRecyclerAdapter<String, GroupChatViewHolder> chatAdapter = new FirebaseRecyclerAdapter<String, GroupChatViewHolder>(
String.class,
R.layout.layout_groups,
GroupChatViewHolder.class,
groupChatsRef) {
protected void populateViewHolder(GroupChatViewHolder viewHolder, String model, int position) {
final String groupChatName = this.getRef(position).getKey();
viewHolder.setName(groupChatName);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intentUserProfile = new Intent(getActivity(), ChatActivity.class);
intentUserProfile.putExtra("groupChatName", groupChatName);
intentUserProfile.putExtra("neighbourhood", neighbourhood);
intentUserProfile.putExtra("usersName", usersName);
intentUserProfile.putExtra("usersID", usersID);
startActivity(intentUserProfile);
}
});
}
};
jGroupChatList.setAdapter(chatAdapter);
}
Note that it handles your string-string group chat entries in your DB as key-value pairs.
public class group_name_list_adapter extends RecyclerView.Adapter<group_name_list_adapter.ViewHolder> {
private List< group_name_list> listItems;
private Context context;
OnItemClickListener onItemClickListener;
public group_name_list_adapter(List<group_name_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.group_name_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
group_name_list listItem = listItems.get(position);
holder.txtTitle.setText(listItem.getTxtTitle());
holder.txtTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onGroupNameClick(position);
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtTitle;
public ViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener{
void onGroupNameClick(int position);
}
}
public class group_name_list {
private String txtTitle;
public group_name_list(String txtTitle) {
this.txtTitle = txtTitle;
}
public String getTxtTitle() {
return txtTitle;
}
}
public class ChatActivity implements group_name_list_adapter.OnItemClickListener
private RecyclerView recyclerGroupName;
private group_name_list_adapter groupNameAdapter;
private List<group_name_list> group_name_List;
private List<String> groupNameKeyList; //This is optional – this is used if you wanted the group chats to have the same name instead of overwriting the groupchat when creating.
Inside your Firebase call:
group_name_List.removeAll(group_name_List t);
groupNameKeyList.removeAll(groupNameKeyList);
//Depending on your firebase reference. This could just be dataSnapshot.getChildren()
for (DataSnapshot child : dataSnapshot.child("Group Chats").getChildren()){
if (!child.getKey().equals(null)){
groupNameKeyList.add(child.getKey().toString()); //Again this is optional
}
group_name_list newGroupList = child.getValue();
);
groupNameList.add(newGroupList);
}
recyclerGroupName.setAdapter(groupNameAdapter);
gLayoutAttribute = new GridLayoutManager(getActivity(), 1);
recyclerGroupName = (RecyclerView) rootView.findViewById(R.id.recyclerGroupName);
recyclerGroupName.setHasFixedSize(true);
recyclerGroupName.setLayoutManager(new LinearLayoutManager(this.getContext()));
recyclerGroupName.setLayoutManager(gLayoutAttribute);
#Override
public void onAttributeClick(int position) {
Intent intentUserProfile = new Intent(getActivity(), ChatActivity.class);
intentUserProfile.putExtra("groupChatName",groupName);
intentUserProfile.putExtra("neighbourhood", neighbourhood);
intentUserProfile.putExtra("usersName", usersName);
intentUserProfile.putExtra("usersID", usersID);
intent.putExtra("name", groupList.get(position).toString());
//intent.putExtra("name", groupListKeyList.get(position).toString()); this is your optional key
startActivity(intentUserProfile);
}
Try according to this
Suppose you've created 5 user's in FirebaseDatabasewith different UID's
In this step you have to get all user's from Firebase and display it in RecyclerView
In Recyclerview's adapter class in onBindViewHolder' you have to do like add and remove` users from list which you generated at the time creating group.
In this step you've to search firebaseDatabase user's Uid which is currently logged in and if your UID is matched found in any Group then you need to get the Group-name .
Happy to help you

Trouble updating GridView by getting Data from Firebase's Database

I just started learning android studio for about a month. Sorry if I have posted this in wrong format or asked the wrong question. I have a GridView at my OverviewPage which has a custom adapter that extends ArrayAdapter. I have a GridItem class that contains the objects that I wish to display in the each grid. What I would like to do is to update my GridView to have a new grid every time a new child is created in the Databse.
This is my GridItem class.
package com.example.android.testlogin;
import java.io.Serializable;
public class GridItem implements Serializable {
int mImageView;
String mTitleHere;
String mPriceHere;
public GridItem(int ImageView, String titleHere, String priceHere) {
mImageView = ImageView;
mTitleHere = titleHere;
mPriceHere = priceHere;
}
public int getmImageView() {
return mImageView;
}
public void setmImageView(int ImageView) {
mImageView = ImageView;
}
public String getmTitleHere() {
return mTitleHere;
}
public void setmTitleHere(String TitleHere) {
mTitleHere = TitleHere;
}
public String getmPriceHere() {
return mPriceHere;
}
public void setmPriceHere(String PriceHere) {
mPriceHere = PriceHere;
}
}
This is my custom Adapter.
package com.example.android.testlogin;
public class GridAdapter extends ArrayAdapter<GridItem> {
public GridAdapter(Activity context, ArrayList<GridItem> packageName){
super(context ,0, packageName);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
}
GridItem currentItem = getItem(position);
ImageView putPic = (ImageView) listItemView.findViewById(R.id.imageHere);
TextView putTitle = (TextView) listItemView.findViewById(R.id.titleHere);
TextView putPrice = (TextView)listItemView.findViewById(R.id.per_price);
//Set Text for TextView and ImageResources for ImageView.
putPic.setImageResource(currentItem.getmImageView());
putTitle.setText(String.valueOf(currentItem.getmTitleHere()));
putPrice.setText(String.valueOf("$: " + currentItem.getmPriceHere()));
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
This is my OverviewPage which acts as the main page when user login. Users should be able to see all the grid created.
package com.example.android.testlogin;
public class OverviewPage extends AppCompatActivity {
private GridView lvPackage;
private GridAdapter adapter;
private FirebaseAuth mAuth1;
private FirebaseAuth.AuthStateListener mAuthListener1;
private DatabaseReference mDatabaseReference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview_page);
lvPackage = (GridView) findViewById(R.id.grid);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("comPostsCopy");
mAuth1 = FirebaseAuth.getInstance();
mAuthListener1 = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
startActivity(new Intent(OverviewPage.this , MainActivity.class));
}
}
};
//Add sample data for grid;
final ArrayList<GridItem> elements = new ArrayList<GridItem>();
elements.add(new GridItem(R.mipmap.ic_launcher_round, "Hello there", "120"));
elements.add(new GridItem(R.mipmap.ic_launcher_round, "Bare Bears", "50"));
elements.add(new GridItem(R.mipmap.ic_launcher_round, "Not Fun", "50"));
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Loop Through Children
for(DataSnapshot ds : dataSnapshot.getChildren());
*//Read Data from Firebase Database and get required data to be shown on new grid.*
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Initialize Adapter
adapter = new GridAdapter(this, elements );
lvPackage.setAdapter(adapter);
lvPackage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do something
Toast.makeText(OverviewPage.this, "Not yet", Toast.LENGTH_SHORT).show();
}
});
}
#Override
protected void onStart() {
super.onStart();
mAuth1.addAuthStateListener(mAuthListener1);
}
}
This is my Firebase Database
Firebase Database
I would only like to get for example the child key "title" and "pricePerGuest" shown on my new grid.
I know I would have to loop through each child in my comPostsCopy child. However, I have no idea how to get some paticular data from the added child in the addValueEventListener method.

Categories

Resources