RecyclerView不显示(显示)

卡米尔河

RecyclerView完全不显示。仅显示TextView。我查看了其他问题,并使用了先前问题的所有答案。其他答案表明,回收站的宽度不能设置为换行内容,或者应该在设置布局管理器且满足这些条件后调用setAdapter。

主要活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = findViewById(R.id.main_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        final MainAdapter adapter = new MainAdapter();
        recyclerView.setAdapter(adapter);

        List<FirebaseProduct> firebaseProductList = new ArrayList<>();
        firebaseProductList = getData(); //Here is my Firebase code
        adapter.setList(firebaseProductList);

    }
 }

activity_main.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">
    <TextView
        android:id="@+id/main_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/aktualne_produkty"
        android:gravity="center"
        android:background="@color/colorPrimary"
        android:textAppearance="@style/TextAppearance.AppCompat.Large">
    </TextView>
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/main_recycler"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        tools:listitem="@layout/card_main"
        android:layout_below="@id/main_text"/>
</RelativeLayout>

主适配器

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainHolder> {
    private List<FirebaseProduct> productList = new ArrayList<>();

    @NonNull
    @Override
    public MainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_main, parent, false);
        return new MainHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MainHolder holder, int position) {
        FirebaseProduct product = productList.get(position);
        Log.d("Prod holder", "name - " + product.getProductName());
        Log.d("Prod holder", "num - " + product.getProductNumber());
        holder.nameView.setText(product.getProductName());
        holder.numView.setText(product.getProductNumber());
       
    }

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

    public void setProductList(List<FirebaseProduct> productList1){
        this.productList = productList1;
        notifyDataSetChanged();
    }

    public List<FirebaseProduct> getList(){
        return productList;
    }

    public static class MainHolder extends RecyclerView.ViewHolder{
        private TextView nameView;
        private TextView numView;
       

        public MainHolder(View itemView){
            super(itemView);
            nameView = itemView.findViewById(R.id.product_name);
            numView = itemView.findViewById(R.id.product_count);
           
        }
    }
}

card_main

<androidx.cardview.widget.CardView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="8dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:layout_weight="3">
            <TextView
                android:id="@+id/product_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">
            </TextView>
            <TextView
                android:id="@+id/product_count"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/product_name">
            </TextView>
        </RelativeLayout>
        <ImageView
            android:id="@+id/product_image"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:contentDescription="@string/product_description">
        </ImageView>
    </LinearLayout>
</androidx.cardview.widget.CardView>
Shikhar深

您正在拨入adapter.setList(firebaseProductList);MainActivity但其中的函数名称MainAdaptersetProductList()因此将其更改为adapter. setProductList(firebaseProductList)

另外,您要确保getData();inMainActivity返回非空列表。您可以使用调试器或在之前添加一条日志语句来对其进行验证adapter.setList(firebaseProductList)像这样:

Log.d("LIST_SIZE", firebaseProductList.size()); //this will print list size
adapter.setList(firebaseProductList); //you have to change it to adapter. setProductList(firebaseProductList)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

RecyclerView不显示

来自分类Dev

CollapsingToolbarLayout不显示RecyclerView

来自分类Dev

RecyclerView 不显示数据

来自分类Dev

RecyclerView 从不显示

来自分类Dev

android recyclerview不显示项目

来自分类Dev

Android RecyclerView不显示项目

来自分类Dev

RecyclerView 什么都不显示

来自分类Dev

RecyclerView 不显示任何项目

来自分类Dev

我的 RecyclerView ContextMenu 不显示

来自分类Dev

ViewPager Fragment中的RecyclerView不显示

来自分类Dev

android-TextView不显示在RecyclerView中

来自分类Dev

RecyclerView 不显示来自 JSON 的数据

来自分类Dev

RecyclerView不显示任何内容,也没有显示错误

来自分类Dev

RecyclerView显示字符串,但不显示图像

来自分类Dev

RecyclerView onclick不显示波纹效果或突出显示

来自分类Dev

RecyclerView不显示任何内容,也没有显示错误

来自分类Dev

与StaggeredGridLayoutManager一起使用RecyclerView不显示任何内容

来自分类Dev

与StaggeredGridLayoutManager一起使用RecyclerView不显示任何内容

来自分类Dev

活动开始时,CardView是否不显示在recyclerview内?

来自分类Dev

ImageView.setImageBitmap在RecyclerView中不显示图像

来自分类Dev

为什么我的 recyclerView 不显示任何数据?

来自分类Dev

如何显示不显示脚本

来自分类Dev

显示警报不显示

来自分类Dev

引导字形不显示

来自分类Dev

OpenGL glRectf()不显示

来自分类Dev

DataTemplate不显示与TabControl

来自分类Dev

图片//细节不显示

来自分类Dev

SVG“使用”不显示

来自分类Dev

MPAndroidChart不显示MarkerView