Steven Steven -4 years ago 46
Android Question

Android textview visibility with recycle view

I have setup an application which populates a recycle view using information from an SQL database e.g. dates and times etc... I have recently setup a small section which either display or hide a textview depending on if the textview above it is the same or not. For example it will populate the first Textview with 13/06/2017 and make it visible, if the next date it wants to display is the same then it will simply hide the textview allowing multiple results with the same date to stack under each other.

Problem
My code is working as it should be and it stacking correctly. The problem I have is that when I scroll down the page and the up again all the headers disappear. This is caused by me not having correctly setup my viewHolder. Could you possibly give me some guidance on the situation.

Bind View

Code allowing the stacking of multiple duplicate results

if (position - 1 >= 0) {

String currentDate = cursor.getString(cursor.getColumnIndexOrThrow("complete_date"));

cursor.moveToPosition(position - 1 );

String previousDate = cursor.getString(cursor.getColumnIndexOrThrow("complete_date"));


Log.i("storedDates", currentDate);

if (currentDate.equalsIgnoreCase(previousDate)) {

txtHeader.setVisibility(View.GONE);
cardView.setVisibility(View.GONE);

} else {


txtHeader.setVisibility(View.VISIBLE);
txtHeader.setText(currentDate);

}

} else {

txtHeader.setVisibility(View.VISIBLE);
txtHeader.setText(cursor.getString(cursor.getColumnIndexOrThrow("complete_date")));
}


Start ViewHolder

public static class Viewholder extends RecyclerView.ViewHolder{

TextView header;
public Viewholder(View itemView) {
super(itemView);

header = (TextView) itemView.findViewById(R.id.txtHeader);
}
}


OncreateViewholder and OnBindViewHolder

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = mCursorAdapter.newView(mContext,mCursorAdapter.getCursor(), parent);

return new Viewholder(view);



}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

mCursorAdapter.getCursor().moveToPosition(position);




mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());

}






@Override
public int getItemCount() {
return mCursorAdapter.getCount();
}


Any suggestions are appreciated

Answer Source

You are missing cardView visibility so where you setting txtHeader visibility also set cardView visibility

    if (position - 1 >= 0) {

       String currentDate = cursor.getString(cursor.getColumnIndexOrThrow("complete_date"));

       cursor.moveToPosition(position - 1 );

       String previousDate = cursor.getString(cursor.getColumnIndexOrThrow("complete_date"));


       Log.i("storedDates", currentDate);    

       if (currentDate.equalsIgnoreCase(previousDate)) {

          txtHeader.setVisibility(View.GONE);
          cardView.setVisibility(View.GONE);

        } else {

          cardView.setVisibility(View.VISIBLE);
          txtHeader.setVisibility(View.VISIBLE);
          txtHeader.setText(currentDate);

       }

     } else {
       cardView.setVisibility(View.VISIBLE);
       txtHeader.setVisibility(View.VISIBLE);
     txtHeader.setText(cursor.getString(cursor.getColumnIndexOrThrow("complete_date")));
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download