-3

So, I tried putting a recyclerView in a "popup" dialog using a firebaseRecyclerAdapter. My problem is, that I know for sure the adapter gets filled because I was using the Logcat to tell me when it adds another "user" to the adapter, but it wont show anything in the recyclerview in the dialog.

I'm having this problem for a couple of days and couldn't find an answer yet, glad if you could help me :)

I'm using a main screen which changes fragments, and from a certain fragment I'm calling this specific dialog.

These are my files: UserViewHolder - the class which holds the "sets" for the cardview:

public static class UserViewHolder extends RecyclerView.ViewHolder
    {
        View mView;
        public UserViewHolder(View itemView)
        {
            super(itemView);
            mView=itemView;
        }
        public void setName(String name)
        {
            TextView teacherName=(TextView) mView.findViewById(R.id.txtNameTea);
            teacherName.setText(name);
        }
        public void setEmail(String email)
        {
            TextView txtEmailTea=(TextView) mView.findViewById(R.id.txtEmailTea);
            txtEmailTea.setText(email);
        }
        public void setImage(final Context ctx, Uri imageUri, User cUser)
        {
            final ImageView imgProfileTea=(ImageView) mView.findViewById(R.id.imgProfileTea);

            Picasso.with(ctx).load(cUser.getImageUri()).into(imgProfileTea);
            if(imgProfileTea.getDrawable()==null) {
                StorageReference load = FirebaseStorage.getInstance().getReference().child("usersProfilePic/" + cUser.getImageName());
                load.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {

                        Picasso.with(ctx).load(uri.toString()).into(imgProfileTea);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG);
                    }

                });
            }

        }
    }

Schedules - the fragment which calls the dialog from its toolbar:

package com.example.android.aln4;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.github.sundeepk.compactcalendarview.CompactCalendarView;
import com.github.sundeepk.compactcalendarview.domain.Event;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static com.example.android.aln4.LoginActivity.myUser;
import static com.example.android.aln4.dataBase.mDatabaseReference;
import static com.example.android.aln4.dataBase.mFirebaseDatabase;
import static com.example.android.aln4.dataBase.mStorageRef;
import static com.example.android.aln4.navDrawerMain.firebaseRecyclerAdapter;
import static java.lang.System.in;
import static com.example.android.aln4.navDrawerMain.studentsQuery;

public class Schedules extends Fragment {

    private Toolbar ScheduleToolbar;
    private Button addEvent;
    //private TextView txt;

    private RecyclerView mRecyclerViewStudentEvent;

    private CompactCalendarView compactCalendar;
    private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("MMMM-yyyy", Locale.getDefault());

    private String[] monthName = {"January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December"};


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.addEvent:
                final AlertDialog.Builder mBuilder = new AlertDialog.Builder(getContext());
                View mView = getLayoutInflater().inflate(R.layout.dialog_event_creation, null);
                mRecyclerViewStudentEvent = (RecyclerView) mView.findViewById(R.id.mRecyclerViewStudentEvent);
                mRecyclerViewStudentEvent.setHasFixedSize(true);
                mRecyclerViewStudentEvent.setLayoutManager(new LinearLayoutManager(getContext()));


                setStudentsList();
                mView = getLayoutInflater().inflate(R.layout.dialog_event_creation, null);
                final EditText edtEventTitle = (EditText) mView.findViewById(R.id.edtEventTitle);
                final TextView txtEventStartTime = (TextView) mView.findViewById(R.id.txtEventStartTime);
                final TextView txtEventEndTime = (TextView) mView.findViewById(R.id.txtEventEndTime);
                final EditText edtEventLocation = (EditText) mView.findViewById(R.id.edtEventLocation);
                Button btnOfferEvent = (Button) mView.findViewById(R.id.btnOfferEvent);
                mBuilder.setView(mView);
                final AlertDialog dialog = mBuilder.create();


                btnOfferEvent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Calendar startTime = Calendar.getInstance();
                        Calendar endTime = Calendar.getInstance();
                        int startHour = Integer.parseInt(txtEventStartTime.getText().toString().substring(0, 2));
                        int startMinute = Integer.parseInt(txtEventStartTime.getText().toString().substring(3, 5));
                        startTime.set(Calendar.HOUR_OF_DAY, startHour);
                        startTime.set(Calendar.MINUTE, startMinute);
                        int endHour = Integer.parseInt(txtEventEndTime.getText().toString().substring(0, 2));
                        int endMinute = Integer.parseInt(txtEventEndTime.getText().toString().substring(3, 5));
                        endTime.set(Calendar.HOUR_OF_DAY, endHour);
                        endTime.set(Calendar.MINUTE, endMinute);
                        String mId =/*dataSelected+*/ String.valueOf(startHour) + String.valueOf(startMinute);//+selectedUserID
                        EventCreation newEvent = new EventCreation(mId, startTime, endTime, edtEventTitle.getText().toString(), edtEventLocation.getText().toString(), R.color.colorPrimary);
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setHasOptionsMenu(true);
        ScheduleToolbar = (Toolbar) getView().findViewById(R.id.schedule_toolbar);
        // Setting toolbar as the ActionBar with setSupportActionBar() call
        ((AppCompatActivity) getActivity()).setSupportActionBar(ScheduleToolbar);

        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mDatabaseReference = mFirebaseDatabase.getReference("users");
        mStorageRef = FirebaseStorage.getInstance().getReference();

        Calendar cal = Calendar.getInstance();
        String month = monthName[cal.get(Calendar.MONTH)];
        int year = cal.get(Calendar.YEAR);

        getActivity().setTitle(month + "-" + year);

        compactCalendar = (CompactCalendarView) getView().findViewById(R.id.compactcalendar_view);
        compactCalendar.setUseThreeLetterAbbreviation(true);


        long millis = System.currentTimeMillis() % 1000;
        Event ev1 = new Event(Color.RED, millis, "First try");
        compactCalendar.addEvent(ev1);

        compactCalendar.setListener(new CompactCalendarView.CompactCalendarViewListener() {
            @Override
            public void onDayClick(Date dateClicked) {
                //put events into scroll view adapter

            }

            @Override
            public void onMonthScroll(Date firstDayOfNewMonth) {
                getActivity().setTitle(dateFormatMonth.format(firstDayOfNewMonth));

            }
        });

    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.schedules, container, false);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_main, menu);
    }

    private void setStudentsList() {

        studentsQuery=mDatabaseReference.orderByChild("teacherNum").equalTo(myUser.getTeacherNum());
        firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<User, navDrawerMain.UserViewHolder>(
                User.class,R.layout.card_view_teacher,navDrawerMain.UserViewHolder.class,studentsQuery) {
            @Override
            protected void populateViewHolder(navDrawerMain.UserViewHolder viewHolder, User model, int position) {
                viewHolder.setName(model.getFirstName() + " " + model.getLastName());
                viewHolder.setEmail(model.getEmail());
                viewHolder.setImage(getContext(), Uri.parse(model.getImageUri()), model);
            }
        };

        mRecyclerViewStudentEvent.setAdapter(firebaseRecyclerAdapter);
}


}

The dialog xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="20dp"
            android:src="@mipmap/title"/>
    <EditText
        android:id="@+id/edtEventTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="כותרת"
        android:layout_marginRight="50dp"/>
        <android.support.v7.widget.RecyclerView
            android:layout_marginTop="50dp"
            android:id="@+id/mRecyclerViewStudentEvent"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </android.support.v7.widget.RecyclerView>

    </RelativeLayout>



    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginTop="20dp"
        android:orientation="vertical">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="20dp"
            android:src="@mipmap/clock"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_marginRight="50dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="שעת התחלה"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/txtEventStartTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="50dp"
            android:layout_marginTop="5dp"
            android:text="21:00" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="25dp"
            android:layout_marginRight="50dp"
            android:background="@color/darkgray" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="50dp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="26dp"
            android:text="שעת סיום"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/txtEventEndTime"
            android:layout_width="match_parent"
            android:layout_marginRight="50dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="30dp"
            android:text="21:45" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="50dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:layout_marginTop="20dp"
            android:src="@mipmap/location"/>
        <EditText
            android:id="@+id/edtEventLocation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="מיקום תחילת השיעור"
            android:layout_marginRight="50dp"/>

    </RelativeLayout>

    <Button
        android:id="@+id/btnOfferEvent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:layout_gravity="center_horizontal"
        android:text="הצע שיעור"/>



</LinearLayout>

One more thing is that I know is that I'm already able to bring up users into the recyclerView in other fragments.. Here's the code in another fragment:

package com.example.android.aln4;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;

import static com.example.android.aln4.dataBase.mDatabaseReference;
import static com.example.android.aln4.dataBase.mFirebaseDatabase;
import static com.example.android.aln4.dataBase.mStorageRef;
import static com.example.android.aln4.navDrawerMain.firebaseRecyclerAdapter;
import static com.example.android.aln4.navDrawerMain.teachersQuery;

public class TeachersList extends Fragment {

    private RecyclerView mRecyclerViewTeacher;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getActivity().setTitle("מורים");
        mFirebaseDatabase = FirebaseDatabase.getInstance();
        mDatabaseReference = mFirebaseDatabase.getReference("users");
        mStorageRef = FirebaseStorage.getInstance().getReference();
        mRecyclerViewTeacher=(RecyclerView) getView().findViewById(R.id.mRecyclerViewTeacher);
        mRecyclerViewTeacher.setHasFixedSize(true);
        mRecyclerViewTeacher.setLayoutManager(new LinearLayoutManager(getContext()));
        setTeachersList();


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.teachers_list_frag,container,false);
    }

    private void setTeachersList() {
        teachersQuery=mDatabaseReference.orderByChild("type").equalTo("Teacher");
        firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<User, navDrawerMain.UserViewHolder>(
                User.class,R.layout.card_view_teacher,navDrawerMain.UserViewHolder.class,teachersQuery) {
            @Override
            protected void populateViewHolder(navDrawerMain.UserViewHolder viewHolder, User model, int position) {
                viewHolder.setName(model.getFirstName()+" "+model.getLastName());
                viewHolder.setEmail(model.getEmail());
                viewHolder.setImage(getContext(), Uri.parse(model.getImageUri()),model);
            }
        };
        mRecyclerViewTeacher.setAdapter(firebaseRecyclerAdapter);

    }

}

and its xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecyclerViewTeacher"
        android:layout_marginTop="57dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>




</RelativeLayout>

It's one of my first questions here so I apologize if I missed something :) Any help would be appriciated.

Adi Harel
  • 505
  • 1
  • 5
  • 10

1 Answers1

0

Solved. All I did was regenerating the SHA1 in the firebase settings, and removing the setHasFixedSize line from Schedules activity and it worked just fine.

Adi Harel
  • 505
  • 1
  • 5
  • 10