Uday Kiran
Uday Kiran

Reputation: 15

Confused with "Working of onBindViewHolder" in android

I am a beginner and I am working on an Android project. I am creating an announcement tab with the Fields Title, Description , Location of the Event, Time of Event. The Idea is Admin of the app will announce some announcements and all the app users will get the Announcements.

There are some situations where the fields Location and time of the event fields are empty. So, I have created two Cardviews to show the announcements. One with those fields and another without those two fields. So Whenever admin do not enter those two fields the Adapter should switch to second card view and should be displayed on the recycler view. But Recycler is showing only one view even though the Time and location fields are not present.

Snapshot of CardView with All Fields

Snapshot of CardView without Location and Time fields

OnBindViewHolder:

Added a boolean Flag name flagdatelocation to know if those two fields are null or not. So, if those fields are null I made the flagdatelocation as false there by passsing it to onCreateViewHolder so as to decide on the selection of the cardview to opt. I have declared the flagdatelocation as global variable inside the class and inialised it with true.(not Static)

NOTE: If I do not initiate the flag as true, I am getting a Null Pointer Exception.

 boolean flagdateLocation=true;

 protected void onBindViewHolder(@NonNull AnnounceHolder holder, int position, @NonNull Announcements model) {
     holder.textViewTitle.setText(model.getTitle());
     holder.textViewDesc.setText(model.getDescription());
    Log.d(TAG, "onBindViewHolder: "+model.getLocation()+" "+model.getDate());
     if (model.getDate()!=null&&model.getLocation()!=null){
         flagdateLocation = true;
         holder.textViewLocation.setText(model.getLocation());
         holder.textViewDate.setText(model.getDate());

     }
     else{
         flagdateLocation = false;
     }

    Log.d(TAG, "onBindViewHolder: 2"+flagdateLocation);


    String creationDate="";
    Date date = model.getTimestamp();
    if (date != null) {
        DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.FULL, Locale.US);
        creationDate = dateFormat.format(date);
        Log.d("TAG", creationDate);
    }
     holder.textViewTodayDate.setText(creationDate);

}

OnCreateViewHolder:

I have added if Condition to check for the flag that was declared in OnBindViewHolder depending on those fields being the null or not, so to opt for the desired the Cardview.

public AnnounceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View V;
   if (flagdateLocation == false) {
       V = LayoutInflater.from(parent.getContext()).inflate(R.layout.announcements_card_view_without_l_s, parent, false);
   }
   else{
       V = LayoutInflater.from(parent.getContext()).inflate(R.layout.announcements_card_view, parent, false);
    }


    return new AnnounceHolder(V);
}

Firebase Struture:

Cloud Firestore where the fields are updated depending on the Admin Announcing

The Issue is even though the fields are null the Recyclerview is always showing the same Cardview. The if condition is somehow not working. My Question is that is my Approach for the requirement is correct or any other way is needed to be implemented. Where did I gone Wrong?

Total RecylerAdapter:

public class AnnouncementsAdapter extends FirestoreRecyclerAdapter<Announcements, AnnouncementsAdapter.AnnounceHolder> {

boolean flagdateLocation;

public AnnouncementsAdapter(@NonNull FirestoreRecyclerOptions<Announcements> options) {
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull AnnounceHolder holder, int position, @NonNull Announcements model) {
     holder.textViewTitle.setText(model.getTitle());
     holder.textViewDesc.setText(model.getDescription());
    Log.d(TAG, "onBindViewHolder: "+model.getLocation()+" "+model.getDate());
     if (model.getDate()!=null&&model.getLocation()!=null){
         flagdateLocation = true;
         holder.textViewLocation.setText(model.getLocation());
         holder.textViewDate.setText(model.getDate());

     }
     else{
         flagdateLocation = false;
     }

    Log.d(TAG, "onBindViewHolder: 2"+flagdateLocation);


    String creationDate="";
    Date date = model.getTimestamp();
    if (date != null) {
        DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.FULL, Locale.US);
        creationDate = dateFormat.format(date);
        Log.d("TAG", creationDate);
    }
     holder.textViewTodayDate.setText(creationDate);

}

@NonNull
@Override
public AnnounceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View V;
   if (flagdateLocation == false) {
       V = LayoutInflater.from(parent.getContext()).inflate(R.layout.announcements_card_view_without_l_s, parent, false);
   }
   else{
      V = LayoutInflater.from(parent.getContext()).inflate(R.layout.announcements_card_view, parent, false);
    }

    V = LayoutInflater.from(parent.getContext()).inflate(R.layout.announcements_card_view, parent, false);


    return new AnnounceHolder(V);
}


class AnnounceHolder extends RecyclerView.ViewHolder{
    TextView textViewTitle;
    TextView textViewDesc;
    TextView textViewLocation;
    TextView textViewDate;
    TextView textViewTodayDate;
    public AnnounceHolder(@NonNull View itemView) {
        super(itemView);
        textViewTitle = itemView.findViewById(R.id.title_announcements);
        textViewDesc = itemView.findViewById(R.id.description_announcements);
        textViewLocation = itemView.findViewById(R.id.location_announcemets);
        textViewDate = itemView.findViewById(R.id.date_announcements);
        textViewTodayDate = itemView.findViewById(R.id.todays_date);

    }
}

Upvotes: 1

Views: 52

Answers (1)

MehranB
MehranB

Reputation: 1525

I hope this helps:

public AnnounceHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View V;

    V = LayoutInflater.from(parent.getContext()).inflate(R.layout.announcements_card_view, parent, false);

    return new AnnounceHolder(V);
}

And:

protected void onBindViewHolder(@NonNull AnnounceHolder holder, int position, @NonNull Announcements model) {
 holder.textViewTitle.setText(model.getTitle());
 holder.textViewDesc.setText(model.getDescription());
Log.d(TAG, "onBindViewHolder: "+model.getLocation()+" "+model.getDate());
 if (model.getDate()!=null&&model.getLocation()!=null){
     
     //SET THE VIEWS YOU WANNA SEE TO VISIBLE
     holder.YOUR_DATE_FILED.setVisibility(View.VISIBLE);
     holder.YOUR_LOCATION_FILED.setVisibility(View.VISIBLE)

     holder.textViewLocation.setText(model.getLocation());
     holder.textViewDate.setText(model.getDate());

 }
 else{
     //SET THE VIEWS YOU DON'T WANNA SEE TO GONE
     holder.YOUR_DATE_FILED.setVisibility(View.GONE);
     holder.YOUR_LOCATION_FILED.setVisibility(View.GONE)
     
 }

Log.d(TAG, "onBindViewHolder: 2"+flagdateLocation);


String creationDate="";
Date date = model.getTimestamp();
if (date != null) {
    DateFormat dateFormat = SimpleDateFormat.getDateInstance(DateFormat.FULL, Locale.US);
    creationDate = dateFormat.format(date);
    Log.d("TAG", creationDate);
}
 holder.textViewTodayDate.setText(creationDate);

}

Upvotes: 2

Related Questions