-1

I have 2 TextViews inside a CardView. I have to hide one of the TextView for Certain Conditons.Even after writing textview.SetVisibility(View.GONE),it still remains visible. Please help

list_item.xml

<?xml version="1.0" encoding="utf-8"?>

    <android.support.v7.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:padding="30dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/listitemtaskname"
                android:layout_marginLeft="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textSize="25sp"
                tools:text="Hi" />


            <TextView
                android:id="@+id/listitemdatedetails"
                android:layout_marginLeft="5dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                tools:text="By" />
        </LinearLayout>
        >

    </android.support.v7.widget.CardView>

These is used in my recyclerview. If listitemdatedetails is empty i try to hide it so in CursorAdapter's onBindViewHolder method where i try to hide it but it does not go away

public class TaskCursorAdapter extends RecyclerCursorAdapter<TaskCursorAdapter.ViewHolder> {
    Context context;
    Cursor cursor;


    public TaskCursorAdapter(Context context, Cursor cursor){
        super(context,cursor);
        this.cursor=cursor;
        this.context=context;

    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView taskname;
        public TextView dateDetails;
        Context context;
        Cursor cursor;

        public ViewHolder(View view,Context context,Cursor cursor) {
            super(view);
            this.context = context;
            this.cursor = cursor;
            taskname=(TextView)view.findViewById(R.id.listitemtaskname);
            dateDetails=(TextView)view.findViewById(R.id.listitemdatedetails);
            view.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {

            // Form the content URI that represents the specific pet that was clicked on,
            // by appending the "id" (passed as input to this method) onto the
            // {@link PetEntry#CONTENT_URI}.
            // For example, the URI would be "content://com.example.android.pets/pets/2"
            // if the pet with ID 2 was clicked on.
            Intent intent = new Intent(context, EditorActivity.class);
            Uri currentPetUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, this.getAdapterPosition()+1);

            intent.setData(currentPetUri);
            context.startActivity(intent);


        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
        View view= layoutInflater.inflate(R.layout.list_item,parent,false);
        return new ViewHolder(view,context,cursor);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, Cursor cursor) {

        int taskColumnIndex=cursor.getColumnIndex(TaskContract.TaskEntry.NAME);
        int dateColumnIndex=cursor.getColumnIndex(TaskContract.TaskEntry.DUEDATE);
        int timeColumnIndex=cursor.getColumnIndex(TaskContract.TaskEntry.DUETIME);

        String task=cursor.getString(taskColumnIndex);
        String dateString=cursor.getString(dateColumnIndex);
        String timeString=cursor.getString(timeColumnIndex);

        viewHolder.cursor = cursor;
        viewHolder.taskname.setText(task);

        if(timeString==null && dateString==null){
            //viewHolder.dateDetails.setText("");
            dateDetails.setVisibility(View.GONE);

        }
        else {
            viewHolder.dateDetails.setText(dateString+"\t\t"+timeString);
        }

    }
}
Fargo
  • 1
  • 1
  • 6

1 Answers1

0

Basically, in your example, you cannot hide any of the TextViews inside your LinearLayout which is child of CardView because it is not allowed to use LinearLayout into CardView.

If you want to be able to hide the elements inside try to set your main layout to LinearLayout and inside it put the CardView with the TextViews you want to hide. Just like this:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:padding="30dp"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/listitemtaskname"
            android:layout_marginLeft="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="25sp"
            tools:text="Hi" />


        <TextView
            android:id="@+id/listitemdatedetails"
            android:layout_marginLeft="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            tools:text="By" />

    </android.support.v7.widget.CardView>

</LinearLayout>

And then try to set visibility to your elements.