Recycler View未在卡片视图中显示数据

时间:2018-04-09 05:22:30

标签: firebase firebase-realtime-database android-recyclerview android-cardview

我创建了一个应用程序,其中我想使用卡片视图在回收站视图中显示来自firebase的数据。我的数据正在从firebase中检索并且正在播放,直到达到回收器选项但不在卡片视图中显示数据。这是我的代码。请帮我解决这个问题,以便在回收商视图中显示数据......

package com.example.dpka.intern;

import android.graphics.Color;
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.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;


/**
 * A simple {@link Fragment} subclass.
 */
public class frag2ofAttendanceNew extends Fragment {

    String mPinFromFrag1ofAttendance;
    String yearf1;
    String monthf1;


    private RecyclerView mRecyclerView;
    DatabaseReference myRef;
    DatabaseReference holidaysRef;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.d("abcd","oncreate view reached");
        final View v= inflater.inflate(R.layout.fragment_frag2of_attendance_new, container, false);
        Log.d("abcd","oncreate view reached and layout inflated");
        mRecyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);
        return v;
    }

    @Override
    public void onStart(){
        super.onStart();
        Log.d("abcd","onstart reached");
        Bundle b = getArguments();
        if(b!=null){
            mPinFromFrag1ofAttendance = b.getString("mPinToFrag2ofAttendance");
            Log.d("abcd","Receiving from frag1ofAttendance");
            yearf1 = b.getString("year");
            monthf1 = b.getString("month");
        }
        Log.d("abcd",mPinFromFrag1ofAttendance);
Log.d("abcd",yearf1);
Log.d("abcd",monthf1);
        /*myRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://esscurrent-77906.firebaseio.com/Attendance_Records/123456/2018/3");*/


        myRef = FirebaseDatabase.getInstance().getReference().child("Attendance_Records").child(mPinFromFrag1ofAttendance).child(yearf1).child(monthf1);


        /*myRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.child("year").exists()){
                    Log.d("abcd","datasnapshot year exists");

                    String tryyear = dataSnapshot.child("year").getValue(String.class);
                    Log.d("abcd",tryyear);

                    *//*Attendance_Records value = dataSnapshot.getValue(Attendance_Records.class);
                    String a = value.getYear();
                    TextView abc = (TextView)getView().findViewById(R.id.tryyear);
                    abc.setText(a);
                    String c = abc.getText().toString();
                    Log.d("abcd",c);

//                    Log.d("abcd",a);
                    String b = value.getEntryDate();
                    TextView bcd = (TextView) getView().findViewById(R.id.tryentrydate);
                    bcd.setText(b);*//*
  //                  Log.d("abcd",b);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
*/

        Log.d("abcd","DB reference reached");
        //mRecyclerView.setHasFixedSize(true);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(layoutManager);
        //mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        Log.d("abcd","setlayoutmanager");

        FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<Attendance_Records>().setQuery(myRef,Attendance_Records.class).build();
        Log.d("abcd","recycleroptions reached");

        FirebaseRecyclerAdapter<Attendance_Records,AttendanceViewHolder> adapter =
                new FirebaseRecyclerAdapter<Attendance_Records, AttendanceViewHolder>
                        (options) {
            //Log.d("abcd","recycleradapter reached");

             @NonNull
             @Override
             public AttendanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                 Log.d("abcd","oncreateviewholder reached");
                 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card,parent,false);
                 Log.d("abcd","cardview inflated");
                 return new AttendanceViewHolder(view);
             }

            @Override
            protected void onBindViewHolder(AttendanceViewHolder holder, int position, Attendance_Records model) {

                holidaysRef=FirebaseDatabase.getInstance().getReference().child("Holidays");
                final String userdate = model.getDate();
                holidaysRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(final DataSnapshot dataSnapshot) {
                        if(dataSnapshot.child(userdate).exists()){
                            Log.d("abcd","date matched");
                            //String holidaytype = holidaysRef.child(userdate).toString();
                            final DatabaseReference tilldateRef = holidaysRef.child(userdate);
                            tilldateRef.addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot1) {
                                    if(dataSnapshot1.child("G").exists()){
                                        Log.d("abcd","its G");
                                        CardView cv = getView().findViewById(R.id.card_view);
                                        Log.d("abcd","cardview found");
                                        cv.setCardBackgroundColor(Color.CYAN);
                                        Log.d("abcd","changed cv color");
                                    }
                                    else if(dataSnapshot1.child("R").exists()){
                                        Log.d("abcd","its R");
                                        CardView cv = getView().findViewById(R.id.card_view);
                                        Log.d("abcd","cardview found");
                                        cv.setCardBackgroundColor(Color.MAGENTA);
                                        Log.d("abcd","changed cv color");
                                    }



                                    else{
                                        Log.d("abcd","Neither G nor R");
                                    }
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });
                            /*if(tilldateRef.child("G")){
                                Log.d("abcd","its G");
                                CardView cv = getView().findViewById(R.id.card_view);
                                Log.d("abcd","cardview found");
                                cv.setCardBackgroundColor(Color.CYAN);
                                Log.d("abcd","changed cv color");
                            }*/
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

                holder.setYear(model.getYear());
                holder.setMonth(model.getMonth());
                holder.setDate(model.getDate());
                holder.setIntime(model.getInTime());
                holder.setOuttime(model.getOutTime());
                holder.setStatus(model.getStatus());
                holder.setEntrydate(model.getEntryDate());
                holder.setMyid(model.getMyID());
                Log.d("abcd","onbindviewholder reached");
            }


        };

        mRecyclerView.setAdapter(adapter);
        adapter.startListening();

    }

    public class AttendanceViewHolder extends RecyclerView.ViewHolder{
        TextView vhyear, vhmonth, vhdate, vhintime, vhouttime, vhentrydate, vhstatus, vhmyid;
        public AttendanceViewHolder(View itemView){
            super(itemView);
            Log.d("abcd","attendanceviewholder reached");
            vhyear = itemView.findViewById(R.id.year);
            vhmonth = itemView.findViewById(R.id.month);
            vhdate = itemView.findViewById(R.id.date);
            vhintime = itemView.findViewById(R.id.inTime);
            vhouttime = itemView.findViewById(R.id.outTime);
            vhentrydate = itemView.findViewById(R.id.entryDate);
            vhstatus = itemView.findViewById(R.id.status);
            vhmyid = itemView.findViewById(R.id.userId);
            Log.d("abcd","textviews found");

        }

        public void setYear(String year) {
            Log.d("abcd","setyear reached");
            vhyear.setText(year);
            Log.d("abcd",vhyear.getText().toString());
        }

        public void setMonth(String month) {
            Log.d("abcd","setmonth reached");
            vhmonth.setText(month);
        }

        public void setDate(String date) {
            Log.d("abcd","setdate reached");
            vhdate.setText(date);
        }

        public void setIntime(String inTime) {
            Log.d("abcd","setintime reached");
            vhintime.setText(inTime);
        }

        public void setOuttime(String outTime) {
            Log.d("abcd","setouttime reached");
            vhouttime.setText(outTime);
        }

        public void setEntrydate(String entrydate){
            Log.d("abcd","setentrydate reached");
            vhentrydate.setText(entrydate);
        }

        public void setStatus(String status){
            Log.d("abcd","setstatus reached");
            vhstatus.setText(status);
        }

        public void setMyid(String myid){
            Log.d("abcd","setmyid reached");
            vhmyid.setText(myid);
        }
    }

}

enter image description here

0 个答案:

没有答案