在 java 中未在回收站视图中获取输出数据

Not getting output data in recycler view in java

我正在尝试在 java 的回收站视图中显示数据。但是我无法在回收站视图中显示数据。我哪里错了。我不知道我哪里出错了。请帮忙。这是我的适配器 class.

public class ListAdapter1 extends RecyclerView.Adapter<ListAdapter1.ViewHolder> {

    Context context;
    List<SupermarketModels> supermarketModels;

    public ListAdapter1(Context context, List<SupermarketModels> supermarketModels) {
        this.context = context;
        this.supermarketModels = supermarketModels;
    }


    @NonNull
    @org.jetbrains.annotations.NotNull
    @Override
    public ListAdapter1.ViewHolder onCreateViewHolder(@NonNull @org.jetbrains.annotations.NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.tileslist,null);


        return  new ListAdapter1.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @org.jetbrains.annotations.NotNull ListAdapter1.ViewHolder holder, int position) {
        holder.tvvegetables.setText(supermarketModels.get(position).getVegetables());
        holder.tvquantity.setText(supermarketModels.get(position).getQuantity());
        holder.tvprice.setText(supermarketModels.get(position).getPrice());
        holder.tvexpiry.setText(supermarketModels.get(position).getExpiry());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvvegetables, tvquantity, tvprice, tvexpiry;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);

            tvvegetables = itemView.findViewById(R.id.vegetables);
            tvquantity = itemView.findViewById(R.id.quantity);
            tvprice = itemView.findViewById(R.id.price);
            tvexpiry = itemView.findViewById(R.id.expiry);

        }
    }
}

这是我的主activityclass。我正在尝试在主 activity 内的回收器中显示样本数据。我在这里显示来自模型 class 的数据,即 SupermarketModels.

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    ListAdapter1 listAdapter;
    List<SupermarketModels> supermarketModelsList = new ArrayList<>();


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

        supermarketModelsList.add(new SupermarketModels(1,"sugar cane","20kg",
                "50rs","31/12/2021"));

        supermarketModelsList.add(new SupermarketModels(2,"sugar cane","20kg",
                "50rs","31/12/2021"));
        supermarketModelsList.add(new SupermarketModels(3 ,"sugar cane","20kg",
                "50rs","31/12/2021"));
        initialization();

        setadapter(supermarketModelsList);



    }

    private void initialization(){
        recyclerView = findViewById(R.id.recyclerview);
    }

    private void setadapter(List<SupermarketModels> supermarketModels){

        listAdapter = new ListAdapter1(this, supermarketModels);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(listAdapter);



    }
}

这是我的 xml 代码。

<RelativeLayout 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">

   <androidx.appcompat.widget.Toolbar

       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/maintoolbar"
       android:background="@color/white"


       />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/maintoolbar"
        android:orientation="vertical"
        >
        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/recyclerview"


            />
    </LinearLayout>


</RelativeLayout>


你应该在你的 recyclerview 上设置布局管理器:
recyclerView.setLayoutManager(new LinearLayoutManager(context))
LinearLayoutManager 是一个 vertical/horizontal LayoutManager。如果你想用网格布局显示数据,你可以使用 GridLayoutManager

我已经检查了您的代码及其显示的项目,问题出在您的主要 xml 布局中。在添加 toolbar 的位置,对工具栏布局高度执行 wrap content,然后它将显示项目。像这样

 <androidx.appcompat.widget.Toolbar

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/maintoolbar"
    android:background="@color/white"
    />