How to Exclude item from FirebaseRecyclerAdapter or Firebase Reference Android - android

I'm working on Activity to show all the others app users except my user and i'm using Java android with Firebase .
But i cant find anyway to exclude item from DatabaseReference or from FirebaseRecyclerAdapter .
My Activity look like this
My Database look like this :
My code inside this Activity looks like below :
#Override
public void onStart() {
super.onStart();
database = FirebaseDatabase.getInstance();
myRef = database.getReference().child("users");
FirebaseRecyclerAdapter<Users,RecyclerViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, RecyclerViewHolder>(Users.class , R.layout.users_single_layout ,RecyclerViewHolder.class,myRef ) {
#Override
protected void populateViewHolder(RecyclerViewHolder viewHolder, Users model, int position) {
viewHolder.setDisolayName(model.getDisplay_name());
viewHolder.setStatus(model.getStatus());
viewHolder.setImage(model.getImager_thumb(), getContext());
frinds_relative.setVisibility(View.INVISIBLE);
final String users_id = getRef(position).getKey();
viewHolder.mview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent target = new Intent(getActivity() , user_profile.class);
target.putExtra("profile_id" , users_id) ;
startActivity(target);
}
});
}
};
users_recycler.setAdapter(firebaseRecyclerAdapter);
}
I will be thankful if you have the solution .

I faced something similar once and how i did it was..
First thing is to give the parent layout and id in the users_single_layout xml file;
Reference the parent layout in the viewholder;
Then hide all the content of file by :
android:visibility="gone"
When That is done, You set and if statement in your FirebaseRecyclerAdapter:
Check the Code Below:
users_recycler = new FirebaseRecyclerAdapter<Users,RecyclerViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, RecyclerViewHolder>(Users.class , R.layout.users_single_layout ,RecyclerViewHolder.class,myRef ) {
#Override
protected void populateViewHolder(RecyclerViewHolder viewHolder, Users model, int position) {
if (adapter.getRef(position).getKey.equals(currentUid){
viewholder.thelayout.setVisibility(View.GONE);
} else {
viewHolder.setDisolayName(model.getDisplay_name());
viewHolder.setStatus(model.getStatus());
viewHolder.setImage(model.getImager_thumb(), getContext());
frinds_relative.setVisibility(View.INVISIBLE);
final String users_id = getRef(position).getKey();
viewHolder.mview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent target = new Intent(getActivity() , user_profile.class);
target.putExtra("profile_id" , users_id) ;
startActivity(target);
}
});
}
}
};
users_recycler.setAdapter(firebaseRecyclerAdapter);

The FirebaseUI FirebaseRecyclerAdapter is a direct representation of the data in the underlying Query or exists at a location in a Firebase database. To exclude an item from an adapter, you have to exclude it first from the Firebase location or ensure it doesn't match that particular query anymore.
To achieve this, you can use the following line of code:
yourAdapter.getRef(position).removeValue();

Related

Retrieve value of key from firebase database to listview

In my firebase database the date is the key value (node) which has its own children. How to retrieve the date into firebase adapter and show it in the listview? I am using the below code but the list is blank.
databaseReference = FirebaseDatabase.getInstance().getReference().child("Lectures").child(post_key);
FirebaseListOptions<String> options = new FirebaseListOptions.Builder<String>()
.setLayout(R.layout.subject_list_heading)
.setQuery(databaseReference, String.class)
.build();
adapter = new FirebaseListAdapter<String>(options) {
#Override
protected void populateView(View v, String model, int position) {
TextView subject_name = v.findViewById(R.id.tv_sub_heading);
subject_name.setText(model);
}
};
list_of_date.setAdapter(adapter);
This is the JSON object:
{
  "-LHSdSCzD_tZhzqvSOw_" : {
    "17-7-2018" : {
      "Absent" : "0",
      "Present" : "1",
      "Total_Lectures" : "1"
    }
I want to retrieve the date in the listview.
Any help is appreciated.
To solve this, please add the following lines of code in the onStart() and onStop() methods.
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if(adapter != null) {
adapter.stopListening();
}
}
So because you are using a listener, you need to start listen for changes. If you are interested, this is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a RecyclerView using FirebaseRecyclerAdapter.

I want to dynamically take inputs from a couple of views. How do I do that?

So, I have an activity in my app where I need to take in the number of tickets for different ticket classes that are retrieved from the backend. The number of ticket classes is also variable. How do I take the user input in this case? Each time the countdown_btn or countup_btn is pressed, I need to update an array that holds the number of tickets the user has chosen. How do I do this when the number of ticket classes itself is dynamic?
If the button 'pledge' is clicked, I want to take the respective inputs from each of the views here and somehow communicate it to the next activity using intent.
My app's code:
public class RewardsAndPledgeActivity extends AppCompatActivity {
DatabaseReference mRewardsRef;
RecyclerView rewards_list;
String Artcall_id;
String reward_id[];
Integer counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_rewards);
/* -------- Obtain the Event ID of the item that the user has selected on the RecyclerView ------*/
Intent intent = getIntent();
Artcall_id = intent.getStringExtra("Artcall_id");
/* ----------------------------------------------------------------------------------------------*/
mRewardsRef = FirebaseDatabase.getInstance().getReference().child("Rewards").child(Artcall_id);
rewards_list = (RecyclerView) findViewById(R.id.reward_list);
rewards_list.setHasFixedSize(true);
rewards_list.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Reward_List, RewardsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Reward_List, RewardsViewHolder>(
Reward_List.class,
R.layout.single_reward_item_layout,
RewardsAndPledgeActivity.RewardsViewHolder.class,
mRewardsRef
) {
#Override
protected void populateViewHolder(final RewardsViewHolder viewHolder, Reward_List model, int position) {
final String reward_id = getRef(position).getKey();
viewHolder.setReward_ticket_amount_txt(model.getReward_ticket_amount_txt());
viewHolder.setReward_ticket_amount_class_name(model.getReward_ticket_amount_class_name());
viewHolder.setReward_ticket_class_desc(model.getReward_ticket_class_desc());
viewHolder.countdown_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer current_ticket_count = Integer.parseInt(viewHolder.ticket_counter.getText().toString());
if(current_ticket_count >0 ) {
viewHolder.ticket_counter.setText(String.valueOf(current_ticket_count - 1));
}
}
});
viewHolder.countup_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer current_ticket_count = Integer.parseInt(viewHolder.ticket_counter.getText().toString());
viewHolder.ticket_counter.setText(String.valueOf(current_ticket_count + 1));
}
});
}
};
rewards_list.setAdapter(firebaseRecyclerAdapter);
}
}
My database:
Image of the activity:

Android fetching values from Firebase Database

Hello so I've been strugging for awhile now. I want to consult someone about my code. I want to apply
a coloring function to my app.
When the person presses the button when it's at its:
GREEN state it updates the value on the database to BEING HOUSEKEPT
YELLOW state when pressed sends READY FOR INSPECTION RoomStatus
RED state when pressed sends READY FOR HOUSEKEEPING RoomStatus
It was working earlier but when I tried to restrict the users that users who are Housekeepers can't access the RED STATE which are for House Keepers, I inserted somewhere in my code where I want to implement it.
I'm going over loops here can somebody tell me where I did wrong?
Here's my code:
Button room1;
private DatabaseReference mFirebaseDatabase, mFirebaseDatabase1, mFirebaseDatabase1room;
private FirebaseDatabase mFirebaseInstance;
private DatabaseReference referenceroom1;
private String roomStat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navi_to_scan2);
mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase1 = mFirebaseInstance.getReference("Rooms");
mFirebaseDatabase = mFirebaseInstance.getReference("Users");
mFirebaseDatabase1room = mFirebaseInstance.getReference("Rooms").child("Room1");
referenceroom1 = mFirebaseDatabase1.child("Room1").child("RoomStatus");
room1 = (Button) findViewById(R.id.rm1Btn);
mFirebaseDatabase1.child("Room1").addValueEventListener(new ValueEventListener() { //attach listener
#Override
public void onDataChange(DataSnapshot dataSnapshot) { //something changed!
for (DataSnapshot locationSnapshot : dataSnapshot.getChildren()) {
String location = locationSnapshot.getValue().toString();
if (location.equals("READY FOR HOUSEKEEPING")) {
room1.setBackgroundColor(Color.GREEN);
roomStat = "Green";
} else if (location.equals("BEING HOUSEKEPT")) {
room1.setBackgroundColor(Color.YELLOW);
roomStat = "Yellow";
} else {
room1.setBackgroundColor(Color.RED);
roomStat = "Red";
}
if (roomStat.equals("Green")) {
room1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String message = "BEING HOUSEKEPT";
DatabaseReference reference = mFirebaseDatabase1.child("Room1").child("RoomStatus");
reference.setValue(message);
Intent next1 = new Intent(getApplicationContext(), ReaderActivity3.class);
startActivity(next1);
}
});
} else if (roomStat.equals("Yellow")) {
room1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String message = "READY FOR INSPECTION";
DatabaseReference reference = mFirebaseDatabase1.child("Room1").child("RoomStatus");
reference.setValue(message);
Intent next1 = new Intent(getApplicationContext(), ReaderActivity2.class);
startActivity(next1);
}
});
} else {
room1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String message = "READY FOR HOUSEKEEPING";
DatabaseReference reference = mFirebaseDatabase1.child("Room1").child("RoomStatus");
reference.setValue(message);
Intent next1 = new Intent(getApplicationContext(), ReaderActivity.class);
startActivity(next1);
//I ALSO WANT TO PUT A CONDITION HERE FETCHING userType from Structure
// mFirebaseDatabase = mFirebaseInstance.getReference("Users").child("userKey");
//but doing this would mean that I would have to put a listener inside, would that be okay? I tried when this
//was working at first but it didin't and now the whole thing is not working
}
});
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) { //update UI here if error occurred.
}
});
}
Here's my structure:
Can you help me identify what's the problem with how i implemented this?
I see nothing wrong, try debugging. Android is buggy nowadays, especially when you're connecting to a cloud database. In the left side of the run button the bug shaped one. Debug.
Or maybe you could try to show your location which is your string, to check if you pulled the right info from your database. You can use logs. A guide can be found here
https://developer.android.com/studio/debug/am-logcat.html

android firebase reads all the database

i am trying to read a specific child in Firebase which i named Tags. the problem is, i just can't put the object from tags (dados.getValue) into a ArrayList to later populate in my ListView.
I know is simple, sorry about that, ut i am new here in android
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taglist);
tags = new ArrayList<>();
tagList = (ListView) findViewById(R.id.tagsList);
tagsRefs = FirebaseConfig.getFireBase();
tagsRefs.child("tags").child("categorias");
tagsRefs.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dados : dataSnapshot.child("tags").getChildren()) {
System.out.println("tag EXTRAIDA NO taglist " + dados.getValue());
dados.getValue(); //HOW CAN I PUT THIS INTO AN ARRAY TO LATER ADD IN MY ArrayList tags??
String tagS = dados.getValue(String.class);
tags.addAll(tagS);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
tagAdapter = new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_2,
android.R.id.text1,
tags);
tagList.setAdapter(tagAdapter);
tagList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedTag = (String) tagList.getItemAtPosition(position);
setSelectedTag(selectedTag);
}
});
}
here is my database:
h
This is happening because onDataChange() method is called asynchronously. This means that the statement that adds tags to your list is executed before onDataChange() method has been called. That's why your list is empty outside that method. So in order to use that lists, you need to use it inside the onDataChange().
For other approach, please visit this post and this post.
Hope it helps.

How to detect null value or no child values present in FirebaseRecyclerAdapter

I have used Firebase RecyclerViewAdapter extensively in my app but since the below code doesn't fire populateViewHolder without any data to load in the first place, I can't show the load complete message for the initial load
RecyclerView recycler = (RecyclerView) findViewById(R.id.messages_recycler);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(Chat.class, android.R.layout.two_line_list_item, ChatHolder.class, mRef) {
#Override
public void populateViewHolder(ChatHolder chatMessageViewHolder, Chat chatMessage, int position) {
chatMessageViewHolder.setName(chatMessage.getName());
chatMessageViewHolder.setText(chatMessage.getText());
}
};
recycler.setAdapter(mAdapter);
What you're describing is covered by this feature: Handle empty state for recycleview. You can handle this by implementing onDataChanged() in your adapter. A snippet from the sample app of FirebaseUI:
#Override
protected void onDataChanged() {
// if there are no chat messages, show a view that invites the user to add a message
mEmptyListView.setVisibility(
mRecyclerViewAdapter.getItemCount() == 0 ? View.VISIBLE : View.INVISIBLE
);
}
if(chatMessage.getName().equals("")){ //null value }else{
//execute code }
Override onDataChanged() and check in it
mAdapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(Chat.class, android.R.layout.two_line_list_item, ChatHolder.class, mRef) {
#Override
public void populateViewHolder(ChatHolder chatMessageViewHolder, Chat chatMessage, int position) {
chatMessageViewHolder.setName(chatMessage.getName());
chatMessageViewHolder.setText(chatMessage.getText());
}
#Override
protected void onDataChanged() {
// do your tests here
}
};

Categories

Resources