Search code examples
javaandroidarraylistandroid-recyclerviewparcelable

TextView not appearing. Blank CardViews


I'm currently working on a basic shopping cart project where I select items from a RecyclerView and add them to a custom ArrayList which should then be seen in EstimateActivity in a new Recycleview. I'm able to add custom objects to the "cartList" and pass it through intent(through parseable). When I click the button to go to the Estimate Activity all that appears in the new RecyclerView are an appropriate amount of CardViews without any of the TextViews(code and name). Am I wrong to believe that the intent is being passed correctly due to the fact that the correct amount of CardViews are found in the EstimateActivity's RecyclerView? Since everything else is working I am having troubles figuring out where the bug lies. Any help with how to figure out bugs like this on my own would also be appreciated.

Here is how I'm passing the intent from the Main Activity to Estimate Activity:


         button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    Intent intent = new Intent(MainActivity.this, EstimateActivity.class);
                    intent.putExtra("cartList", cartList); 
                    v.getContext().startActivity(intent);
            }
        });

This is the Estimate Activity:


        
public class EstimateActivity extends AppCompatActivity {
    private RecyclerView eRecyclerview;
    private CartAdapter eAdapter;
    private RecyclerView.LayoutManager eLayoutManager;
    private ArrayList<Inventory> eCartList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_estimate);

        eCartList = new ArrayList<Inventory>();
        Bundle bundle = getIntent().getExtras();
        eCartList = bundle.getParcelableArrayList("cartList");

        eRecyclerview = findViewById(R.id.recyclerview);
        eLayoutManager = new LinearLayoutManager(this);
        eAdapter = new CartAdapter(eCartList); //  THIS IS WHERE IM PASSING THE LIST
        eRecyclerview.setLayoutManager(eLayoutManager);
        eRecyclerview.setAdapter(eAdapter);

    }
}

Here is the CartAdapter:


        
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.CartViewHolder> {
    private ArrayList<Inventory> eInventoryList;

    public static class CartViewHolder extends RecyclerView.ViewHolder {
        private TextView eCardCode;
        private TextView eCardName;
        private CardView eContainerView;

        public CartViewHolder(View itemView) {
            super(itemView);
            eCardCode = itemView.findViewById(R.id.code_view);
            eCardName = itemView.findViewById(R.id.name_view);
            eContainerView = itemView.findViewById(R.id.container_view2);
        }
    }

    public CartAdapter(ArrayList<Inventory> eCartList){   //this is where i recieve the list
       eInventoryList = eCartList;
    }

    @NonNull
    @Override
    public CartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_row, parent, false);
        CartViewHolder mvh = new CartViewHolder(view);
        return mvh;
    }

    @Override
    public void onBindViewHolder(@NonNull CartViewHolder holder, int position) {
        Inventory currentItem = eInventoryList.get(position);
        holder.eCardName.setText(currentItem.getName());
        holder.eCardCode.setText(currentItem.getCode());
        holder.eContainerView.setTag(currentItem);
    }



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

Lastly here is the XML for the CardView in the RecyclerView


        <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/black"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="5dp"
    android:padding="5dp"
    android:layout_marginBottom="2dp"
    android:layout_marginTop="2dp"
    android:id="@+id/container_view2"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp">
        <TextView
            android:id="@+id/code_view"
            android:text="code_view"
            android:textSize="10sp"
            android:textColor="@color/white"
            android:layout_centerInParent="true"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            />

        <TextView
            android:id="@+id/name_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:layout_alignParentTop="true"
            android:layout_marginTop="0dp"
            android:text="Name Name Name Name Name"
            android:textColor="@android:color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            android:paddingTop="10dp"
            />


    </RelativeLayout>

</androidx.cardview.widget.CardView>

Solution

  • Try to debug the application, put a breakpoint after receiving the bundle content and check what values ​​it has to make sure that it is arriving well, you can do the same inside the adapter. This can be done every time you need to know what values ​​the variables have at a specific time and place Good luck in this new world !!