1

I created a page where it will show the firebase data in a RecyclerView. It work but I decided to put a toolbar at the top of the page to have a back function. After I put the toolbar in my layout file, the RecyclerView is not showing any data anymore. I think that my layout XML file is wrong. Thanks in advance for anyone helping.

Actity file

package com.example.attendanceappvqyfyp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.example.attendanceappvqyfyp.Interface.itemClickListener;
import com.example.attendanceappvqyfyp.Common.Common;
import com.example.attendanceappvqyfyp.Model.LecturerClass;
import com.example.attendanceappvqyfyp.Model.News;
import com.example.attendanceappvqyfyp.Model.Timetable;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.example.attendanceappvqyfyp.Common.Common;

public class LecturerClassList extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    FirebaseDatabase database;
    DatabaseReference lecturerclass;
    DatabaseReference news;

    FirebaseRecyclerAdapter<LecturerClass,LecturerClassViewHolder> adapter;

    TextView newscourse, newsclass;
    EditText newsdescription;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lecturer_class_list);

        //Firebase
        database = FirebaseDatabase.getInstance();
        lecturerclass = database.getReference("Class");
        news = database.getReference("News");

        recyclerView = (RecyclerView)findViewById(R.id.recycler_lecturerclass);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        //Toolbar code
        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("Class List");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);

        loadLecturerClass(Common.currentLecturer.getName());
    }

    private void loadLecturerClass(String Lecturer) {
        adapter = new FirebaseRecyclerAdapter<LecturerClass, LecturerClassViewHolder>(LecturerClass.class, R.layout.lecturerclass_item, LecturerClassViewHolder.class, lecturerclass.orderByChild("lecturer").equalTo(Lecturer)) {
            @Override
            protected void populateViewHolder(LecturerClassViewHolder lecturerClassViewHolder, LecturerClass lecturerClass, int i) {
                lecturerClassViewHolder.lclasstitle.setText("Class :" + adapter.getRef(i).getKey());
                lecturerClassViewHolder.ldate.setText("Day :" + lecturerClass.getDay());
                lecturerClassViewHolder.ltime.setText("Time :" + lecturerClass.getTime());
                lecturerClassViewHolder.lclassroom.setText("Classroom :" + lecturerClass.getClassroom());
                lecturerClassViewHolder.lcourse.setText(lecturerClass.getCourse());

                lecturerClassViewHolder.setItemClickListener(new itemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        //code later
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle().equals(Common.News))
        {
            showNewsDialog(adapter.getRef(item.getOrder()).getKey(),adapter.getItem(item.getOrder()));
        }
        return super.onContextItemSelected(item);
    }

    private void showNewsDialog(final String course, final LecturerClass item) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(LecturerClassList.this);
        alertDialog.setTitle("Make Announcement");
        alertDialog.setMessage("Make announcement about this class.");

        LayoutInflater inflater = this.getLayoutInflater();
        View lecturer_make_news = inflater.inflate(R.layout.activity_lecturer_make_news, null);

        newscourse = lecturer_make_news.findViewById(R.id.NewsCourse);
        newsclass = lecturer_make_news.findViewById(R.id.NewsClass);
        newsdescription = lecturer_make_news.findViewById(R.id.NewsDescription);

        newscourse.setText("Course: "+item.getCourse());
        newsclass.setText("Subject: " + course);

        alertDialog.setView(lecturer_make_news);

        alertDialog.setPositiveButton("Post", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                if(newsdescription.getText().toString().isEmpty()){
                    Toast.makeText(LecturerClassList.this, "Post cancel, description cannot be empty.", Toast.LENGTH_SHORT).show();
                }
                else {
                    News lnews = new News(
                            item.getCourse(),
                            course,
                            newsdescription.getText().toString()
                    );

                    news.child(String.valueOf(System.currentTimeMillis())).setValue(lnews);
                    Toast.makeText(LecturerClassList.this, "Announcement post successfully.", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });

        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();;
            }
        });
        alertDialog.show();
    }
}

Activity file for viewholder

package com.example.attendanceappvqyfyp;

import android.view.ContextMenu;
import android.view.View;
import android.widget.TextView;

import com.example.attendanceappvqyfyp.Common.Common;
import androidx.recyclerview.widget.RecyclerView;
import com.example.attendanceappvqyfyp.Interface.itemClickListener;

public class LecturerClassViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnCreateContextMenuListener {

    public TextView lclasstitle, ldate, ltime, lclassroom, lcourse;

    private itemClickListener itemClickListener;

    public LecturerClassViewHolder(View itemView) {
        super(itemView);

        lclasstitle = (TextView)itemView.findViewById(R.id.lclasstitle);
        ldate = (TextView)itemView.findViewById(R.id.ldate);
        ltime = (TextView)itemView.findViewById(R.id.ltime);
        lclassroom = (TextView)itemView.findViewById(R.id.lclassroom);
        lcourse = (TextView)itemView.findViewById(R.id.lcourse);

        itemView.setOnCreateContextMenuListener(this);
        itemView.setOnClickListener(this);

    }

    public void setItemClickListener(itemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onClick(View view) {
        itemClickListener.onClick(view, getAdapterPosition(), false);
    }


    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select");

        menu.add(0,0,getAdapterPosition(), Common.News);
    }

}

Layout file for recyclerview

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LecturerClassList">

    <android.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:theme="@style/ThemeOverlay.AppCompat.Dark" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_lecturerclass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layout_constraintTop_toBottomOf="@+id/toolbar" />

</androidx.constraintlayout.widget.ConstraintLayout>

Layout file for itemview

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    app:cardElevation="4dp"
    android:layout_marginBottom="8dp"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/lclasstitle"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:gravity="center"
            android:text=""
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/ldate"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignBottom="@id/lclasstitle"
            android:layout_marginBottom="-40dp"
            android:gravity="center"
            android:text=""
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/ltime"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignBottom="@id/ldate"
            android:layout_marginBottom="-40dp"
            android:gravity="center"
            android:text=""
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/lclassroom"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:text=""
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/lcourse"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:text=""
            android:visibility="invisible"
            android:textColor="@android:color/black"
            android:textSize="20sp" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

Thanks to ZeePee for giving this link Display Back Arrow on Toolbar, although the toolbar didnt work for me, i change it back to actionbar and use the code from the link and it work.

Community
  • 1
  • 1
stanley
  • 13
  • 3

1 Answers1

0

Try adding horizontal constraints for Toolbar and Recycler View. After that you need to specify where do you want your single item widgets in Relative layout

zulu_papa
  • 415
  • 6
  • 12
  • i try adding horizontal constraint for Toolbar and Recycler View already have the constraitn but is not working. Im not sure what you mean by the single item widgets in relative layout. – stanley Feb 25 '20 at 11:16
  • Items which fill the recycler. Check how relative layout works, you dont need all those margins. Dont use layout_alignBottom. Use layoutbelow, layoutabove, toleftof and toright of. If you set all to alignparentbottom to true, they will overlap. Check how you align your elements in itemview. You dont need vertical orientation too. You can set toolbar height, add that value to dimensions and then set the margin top for recycler to the same value – zulu_papa Feb 25 '20 at 13:45
  • I change my itemview and remove all those margin and use layoutbelow for those textview. I remove vertical orientation as well. As for setting the margin for recycler view, the toolbar and recyclerview are inside a constraint layout and therefore setting margin top for recycler view is not working. Thanks @ZeePee for helping out. I saw that some people use coordinator layout for recycler view with toolbar, is that the reason why mine not working maybe? – stanley Feb 25 '20 at 14:33
  • Not sure about that, you might use frame or even easier linear as well with vertical orientation – zulu_papa Feb 25 '20 at 14:34
  • Is it a problem for you to put the project on github. I am not an expert, but it seems i might help you and i would be glad to help you out. Seems i might learn something from your code too :) – zulu_papa Feb 25 '20 at 16:44
  • Sorry i dun think i can upload to github, my project is link to firebase, and by using my projcet, anyone can change the firebase data. Every code that is related to this problem, i have uploaded here. Really sorry – stanley Feb 25 '20 at 17:34
  • You can make private repository and invite only me to collaborate on the project. I'm willing to help you. Otherwise try to use debugger, or put bunch of log statements. What does your logcat say on the problem? Just for code organization check your Activity class. Put your toolbar code above recyclerview calls. Check this answer too: https://stackoverflow.com/questions/26651602/display-back-arrow-on-toolbar – zulu_papa Feb 26 '20 at 04:02
  • No problem, i am glad that you have fixed it ;). Good luck – zulu_papa Mar 04 '20 at 09:43