簡體   English   中英

在 java 的回收站視圖中未獲取 output 數據

[英]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);

        }
    }
}

這是我的主要活動 class。 我正在嘗試在主要活動內的回收站中顯示示例數據。 在這里,我顯示來自 model 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>


我檢查了您的代碼及其顯示的項目,問題出在您的主要 xml 布局中。 在您添加工具欄的地方,請wrap content ,然后它將顯示項目。 像這樣

 <androidx.appcompat.widget.Toolbar

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

您在 logcat 上有任何錯誤消息嗎? 或檢查您的 xml 布局。 看起來你的代碼沒有問題

你應該在你的 recyclerview 上設置 layoutmanager:
recyclerView.setLayoutManager(new LinearLayoutManager(context))
LinearLayoutManager是一個垂直/水平布局管理器。 如果要使用網格布局顯示數據,可以使用GridLayoutManager

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM