Reading Firebase Data And Populating Recyclerview
I'm trying to read the data from Firebase and display it in a recyclerview with a card layout. I have a seperate activity for writing to Firebase and a fragment that actually displ
Solution 1:
- In Your activity
public class ApprovedMeetingsActivity extends AppCompatActivity {
DatabaseReference rootRef, pendingMeetingsRef;
ListView pendingMeetingslist;
List<Meeting> meetings = new ArrayList<>();
MeetingsAdapter meetingsAdapter;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pending_meetings);
pendingMeetingslist = findViewById(R.id.list);
rootRef = FirebaseDatabase.getInstance().getReference();
pendingMeetingsRef = rootRef.child("approved_meetings");
loadListFromFirebase();
}
private void loadListFromFirebase() {
pendingMeetingsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
meetings.add(postSnapshot.getValue(Meeting.class));
meetingsAdapter = new MeetingsAdapter(ApprovedMeetingsActivity.this, meetings, "startmeet");
pendingMeetingslist.setAdapter(meetingsAdapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
- Getters and setters
public class Meeting {
String id;
String user;
String training;
String trainer;
String location;
String trainee;
public Meeting() {
//Empty Constructor For Firebase
}
public void setallmeetings(String id, String user, String training, String trainer, String location)
{
this.id = id;
this.user = user; //Parameterized for Program-Inhouse objects.
this.training = training;
this.trainer = trainer;
this.location = location;
}
//Getters and Setters
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getUser()
{
return user;
}
public void setUser(String user)
{
this.user = user;
}
public String getTraining()
{
return training;
}
public void setTraining(String training)
{
this.training = training;
}
public String getTrainer()
{
return trainer;
}
public void setTrainer(String trainer)
{
this.trainer = trainer;
}
public String getLocation()
{
return location;
}
public void setLocation(String location)
{
this.location = location;
}
public String getTrainee()
{
return trainee;
}
public void setTrainee(String trainee)
{
this.trainee = trainee;
}
- Then finally in adapter set the data
public class MeetingsAdapter extends BaseAdapter {
private List<Meeting> listData;
private LayoutInflater layoutInflater;
Context context;
String text;
List<String> ids = new ArrayList<>();
DatabaseReference rootRef, pendingMeetingsRef, canceledMeetingsRef;
SendMsgViewModel sendMsgViewModel;
CompositeSubscription subscription = new CompositeSubscription();
Float lat , lng ;
Location currentLocation;
private SimpleLocation location;
public MeetingsAdapter(Context context, List<Meeting> listData, String text) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
this.context = context;
this.text = text;
}
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.item_meeting_list, null);
holder = new ViewHolder();
holder.meeting = convertView.findViewById(R.id.training_txt);
holder.trainer = convertView.findViewById(R.id.trainer_txt);
holder.location = convertView.findViewById(R.id.location_txt);
holder.cancelMeetBtn = convertView.findViewById(R.id.cancel_meet_btn);
holder.startMeetBtn = convertView.findViewById(R.id.start_meet_btn);
if(text.equals("hidebtn")){
holder.cancelMeetBtn.setVisibility(View.GONE);
holder.startMeetBtn.setVisibility(View.GONE);
}
else if(text.equals("startmeet")){
holder.cancelMeetBtn.setVisibility(View.GONE);
holder.startMeetBtn.setVisibility(View.VISIBLE);
}
else {
holder.cancelMeetBtn.setVisibility(View.VISIBLE);
holder.startMeetBtn.setVisibility(View.GONE);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
sendMsgViewModel = new SendMsgViewModel(new SendMsgInteractorImpl(), AndroidSchedulers.mainThread());
location = new SimpleLocation(context);
final PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
Toast.makeText(context, "Permission Granted", Toast.LENGTH_SHORT).show();
NotifyData notifydata = new NotifyData(String.valueOf(location.getLatitude()),
String.valueOf(location.getLongitude()));
subscription.add(sendMsgViewModel.sendMsg("key=AIzaSyCJT3Osi3pHf3K6nr_LRs1Xx0m0M5fID04",
new Message("cDjrAZSBTP4:APA91bG1hbHafiCuLRW_yy1OLbySkTXd0BPIvT5jA9xzZn4dW-4Cy0WRoqGKM9E6GSMumCd63Uu6xiksPYB1EmcOjn6v_jlZWsZ41I2z6laQtYlu7j57vimQVYOVXBNe1d8eBIqIZAfi",
notifydata,""))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Message>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Message message) {
}
}));
}
@Override
public void onPermissionDenied(ArrayList<String> deniedPermissions) {
Toast.makeText(context, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
}
};
rootRef = FirebaseDatabase.getInstance().getReference();
pendingMeetingsRef = rootRef.child("requested_meetings");
canceledMeetingsRef = rootRef.child("canceled_meetings");
for(int i=0; i < listData.size(); i++) {
ids.add(listData.get(position).getId());
}
holder.meeting.setText(listData.get(position).getTraining());
holder.trainer.setText(listData.get(position).getTrainer());
holder.location.setText(listData.get(position).getLocation());
holder.cancelMeetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MaterialDialog.Builder(context)
.title("Cancel Meeting")
.backgroundColor(context.getResources().getColor(R.color.white))
.titleColor(context.getResources().getColor(R.color.black))
.contentColor(context.getResources().getColor(R.color.textColor))
.content("Are you sure you want to cancel this Meeting ?")
.positiveText("Yes")
.negativeText("No")
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
}
})
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
Meeting meet = new Meeting();
String id = canceledMeetingsRef.push().getKey();
meet.setId(id);
meet.setUser(listData.get(position).getUser());
meet.setTraining(listData.get(position).getTraining());
meet.setTrainer(listData.get(position).getTrainer());
meet.setLocation(listData.get(position).getLocation());
canceledMeetingsRef.child(id).setValue(meet);
listData.remove(position);
pendingMeetingsRef.child(ids.get(position)).removeValue();
ids.remove(position);
notifyDataSetChanged();
Toast.makeText(context,"Meeting successfully cancelled", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
holder.startMeetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!location.hasLocationEnabled()) {
// ask the user to enable location access
SimpleLocation.openSettings(context);
return;
}
new MaterialDialog.Builder(context)
.title("Start Meeting")
.backgroundColor(context.getResources().getColor(R.color.white))
.titleColor(context.getResources().getColor(R.color.black))
.contentColor(context.getResources().getColor(R.color.textColor))
.content("Are you sure you want to start this Meeting ?")
.positiveText("Yes")
.negativeText("No")
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
}
})
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
location.beginUpdates();
TedPermission.with(context)
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
.check();
}
})
.show();
}
});
return convertView;
}
static class ViewHolder {
TextView meeting, trainer, location;
ImageView cancelMeetBtn;
LinearLayout startMeetBtn;
}
void updateLocation(Location location) {
currentLocation = location;
lat = (float) currentLocation.getLatitude();
lng = (float) currentLocation.getLongitude();
}
}
Post a Comment for "Reading Firebase Data And Populating Recyclerview"