简体   繁体   English

RecyclerView的仅第一个CardView被着色

[英]Only first CardView of RecyclerView is being Colored

I have created an app in Android Studio, using CardViews in RecyclerView in connection with Firebase Realtime database, in which user can check his attendance such that if the user came on a holiday(R,G, Saturday or Sunday) that CardView is colored. 我已经在Android Studio中创建了一个应用程序,使用了RecyclerView中的CardViews和Firebase Realtime数据库,用户可以在其中检查出勤情况,以便用户在假期(R,G,周六或周日)来查看CardView的颜色。 But in my code only the first CardView is getting colored. 但是在我的代码中,只有第一个CardView被着色。 I have checked through Log statements that it's traversing through both R and G's if-else statements. 我已经通过Log语句检查了它是否遍历R和G的if-else语句。

My code is: 我的代码是:

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);
myRef = FirebaseDatabase.getInstance().getReference().child("Attendance_Records").child(mPinFromFrag1ofAttendance).child(yearf1).child(monthf1);

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) {

            @NonNull
                @Override
                public AttendanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    Log.d("abcd","oncreateviewholder reached");
                    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();

                    try {
                        Log.d("abcd","StringTodate about to be called");
                        StringToDate(userdate);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    holidaysRef.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(final DataSnapshot dataSnapshot) {
                            if(dataSnapshot.child(userdate).exists()){
                                Log.d("abcd","date matched");
                       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 if(day==7){
                                            Log.d("abcd","its Saturday");
                                            CardView cv = getView().findViewById(R.id.card_view);
                                            Log.d("abcd","cardview found");
                                            cv.setCardBackgroundColor(Color.GRAY);
                                            Log.d("abcd","changed cv color");
                                        }
                                        else if(day==1){
                                            Log.d("abcd","its Sunday");
                                            CardView cv = getView().findViewById(R.id.card_view);
                                            Log.d("abcd","cardview found");
                                            cv.setCardBackgroundColor(Color.DKGRAY);
                                            Log.d("abcd","changed cv color");
                                        }


                                        else{
                                            Log.d("abcd","None from G,R or Weekend");
                                        }
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });
                            }
                        }

                        @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();

}

private void StringToDate(String date) throws ParseException{
    Log.d("abcd","StringtoDate reached");
    DateFormat formatter = null;
    Date convertedDate = null;

    formatter = new SimpleDateFormat("dd-MM-yyyy");
    convertedDate = (Date)formatter.parse(date);
    Log.d("abcd","date converted");

    Calendar c = Calendar.getInstance();
    Log.d("abcd","Calendar getInstance reached");
    c.setTime(convertedDate);
    Log.d("abcd","userdate set in Calendar");
    day = c.get(Calendar.DAY_OF_WEEK);
    Log.d("abcd","day of week is "+day);
}

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

}

My Attendance_Records table 我的Attendance_Records表

My Holidays Table 我的假期表

UPDATE 更新

I have achieved for holidays in holidays table but still not able to color the weekends. 我已经在假期表中取得了假期,但仍然无法为周末上色。 Should I put those conditions somewhere else? 我应该把这些条件放在其他地方吗?

You should define the CardView in the ViewHolder: 您应该在ViewHolder中定义CardView:

    CardView cv;

        public AttendanceViewHolder(View itemView){
            super(itemView);
                Log.d("abcd","attendanceviewholder reached");
                vhyear = itemView.findViewById(R.id.year);
                cv = itemView.findViewById(R.id.card_view);
    ...
        } 

Then use 然后使用

holder.cv.setCardBackgroundColor(Color.CYAN);

instead of 代替

CardView cv = getView().findViewById(R.id.card_view);
Log.d("abcd","cardview found");
cv.setCardBackgroundColor(Color.CYAN);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM