[在Studio.xml文件中添加工具栏时,Android Studio RecyclerView不会显示我的数据

来源:爱站网时间:2021-09-16编辑:网友分享
我创建了一个页面,它将在RecyclerView中显示Firebase数据。它可以工作,但我决定在页面顶部放置一个工具栏以具有后退功能。将工具栏放到布局中后...

问题描述


我创建了一个页面,它将在RecyclerView中显示Firebase数据。它可以工作,但我决定在页面顶部放置一个工具栏以具有后退功能。将工具栏放入布局文件后,RecyclerView不再显示任何数据。我认为我的布局XML文件是错误的。预先感谢任何人的帮助。

活动文件

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 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.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();
    }
}

用于观看者的活动文件

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);
    }

}

recyclerview的布局文件

项目视图的布局文件

思路:


[尝试为工具栏和回收者视图添加水平约束。之后,您需要在“相对布局”中指定单项小部件的位置

上一篇:为什么不能打印?

下一篇:Log4j,禁用依赖于环境的附加程序

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载