TextView没有出现空白CardViews

问题描述

我目前正在一个基本的购物车项目中,在该项目中,我从RecyclerView中选择商品并将其添加自定义ArrayList中,然后应在新的Recycleview的EstimateActivity中看到该商品。我能够将自定义对象添加到“ cartList”中,并通过intent(通过parseable)将其传递。当我单击按钮转到“估算活动”时,新的RecyclerView中显示的所有内容都是适当数量的CardView,而没有任何TextView(代码名称)。我是否会因为在EstimateActivity的RecyclerView中找到了正确数量的CardView而正确地传递了意图?由于其他所有功能都正常工作,因此我很难弄清错误所在。对于如何自行找出此类错误的任何帮助也将不胜感激。

这是我将意图从主要活动传递到估计活动的方式:


         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);
            }
        });

这是估算活动:


        
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);

    }
}

这是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();
    }
}                

最后这是RecyclerView中CardView的XML


        <?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>

解决方法

尝试调试应用程序,在收到捆绑包内容后放置一个断点,并检查它必须确保到达的值是什么,您可以在适配器内部执行相同的操作。 每当您需要知道变量在特定时间和地点具有什么值时,都可以执行此操作 在这个新世界里祝你好运!

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...