Lucas Sierota
Lucas Sierota

Reputation: 180

How to fix TextView being rendered blank on new Activity

I'm trying to start another activity after clicking on a CardView inside a RecyclerView. The new activity renders after the click on any of the items inside the RecyclerView (It does the transition and changes the title TextView), but it does not show any of the TextView's inside the CardView on the layout.

The OnClick method is being set as an interface and being implemented in the first Activity.

Here is my first activity. I'm loading the data here from a SQLite database, but it's not relevant for the question to show the code from it.

public class MainActivity extends AppCompatActivity implements RecipeAdapterMain.OnCardListener {

    private TextView tv;


    ArrayList<RecipeObject> recipes = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView rv = findViewById(R.id.recipeListMain);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);

        RecipeAdapterMain adapter = new RecipeAdapterMain(recipes, this);
        rv.setAdapter(adapter);

        tv = findViewById(R.id.title);

    }
@Override
    public void onCardClick(int position) {
        Intent intent = new Intent(this, RecipeActivity.class);
        startActivity(intent);
    }
}

This is my Adapter.

public class RecipeAdapterMain extends RecyclerView.Adapter<RecipeAdapterMain.RecipeListViewHolder> {

    RecyclerView mRecyclerView;
    ArrayList<RecipeObject> recipeList;
    private OnCardListener mOnCardListener;

    public RecipeAdapterMain(Context context, ArrayList<RecipeObject> recipeList, OnCardListener onCardListener) {
        this.mOnCardListener = onCardListener;
        this.recipeList = recipeList;
    }



    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        mRecyclerView = recyclerView;
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public RecipeListViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recipe_main, parent, false);

        RecipeListViewHolder rcv = new RecipeListViewHolder(layoutView, mOnCardListener);
        return rcv;
    }

    @Override
    public void onBindViewHolder(@NonNull RecipeListViewHolder holder, int position) {
        holder.mName.setText(recipeList.get(position).getName());
    }

    @Override
    public int getItemCount(){
        return recipeList.size();
    }

    public class RecipeListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public TextView mName;

        OnCardListener onCardListener;
        public RecipeListViewHolder(final View view, OnCardListener onCardListener) {
            super(view);
            mName = view.findViewById(R.id.textViewTitle);
            this.onCardListener = onCardListener;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            onCardListener.onCardClick(getAdapterPosition());
        }


    }
    public interface OnCardListener {
        void onCardClick(int position);
    }
}

My second activity:

public class RecipeActivity extends AppCompatActivity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {

        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.layout_recipe);
}
}

This is the layout from the second activity. (layout_recipe):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

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

            <TextView
                android:id="@+id/textViewName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="This is a test"
                android:textColor="#1A1A1A"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/textViewIngr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="This is a test"
                android:textColor="#1A1A1A"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/textViewDire"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="This is a test"
                android:textColor="#1A1A1A"
                android:textSize="18sp" />
        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

And this is the layout from the first activity (activity_main):

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recipeListMain"
        android:scrollbars="vertical">

    </androidx.recyclerview.widget.RecyclerView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="@color/colorAccent"
        android:layout_gravity="end|bottom"
        android:layout_margin="16dp"
        android:layout_marginBottom="16dp"
        android:contentDescription="@string/submit"
        android:src="@drawable/ic_add_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:onClick="openActivityCreate"/>



</androidx.coordinatorlayout.widget.CoordinatorLayout>

From clicking in a Card on the MainActivity, I expected to render the second activity with it's TextViews (This is a test), but it's rendering a blank page instead.

Upvotes: 1

Views: 211

Answers (1)

Faisal
Faisal

Reputation: 725

try removing @Nullable PersistableBundle persistentState from onCreate on your second activity

Upvotes: 2

Related Questions