RecyclerView将不会显示数据

时间:2019-04-06 20:14:30

标签: android android-recyclerview

所以我是RecyclerView使用的新手,我正在尝试使用它。 我测试了数据,但是现在显示行了,我试了一天,但没有运气。 因此,请指出正确的方向。

活动:

package com.example.adelina_pc.testscanner3;
    public class PopularHourActivity extends AppCompatActivity {
        int length;
        RepoProducts repo;
        RecyclerAdapter  adapter;
        Product p;
        List<Product> array;
        RecyclerView list;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_popular_hour);


            TextView popularHour = findViewById(R.id.hour);
            popularHour.setText(String.valueOf(0));
            list = findViewById(R.id.list);

            Calendar rightNow = Calendar.getInstance();
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd/HH");
            String formattedDate = dateFormat.format(rightNow.getTime());

            DatabaseReference fireRef3 = FirebaseDatabase.getInstance().getReference().child("Info").child(formattedDate);
            fireRef3.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    length = (int) dataSnapshot.getChildrenCount();
                    repo = new RepoProducts(length);

                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        Product o = snapshot.getValue(Product.class);

                        repo.addObject(o);

                    }
                    if (repo.length==length){
                        restOfCode();
                        return;
                    }

                }
                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
        public void restOfCode(){
            if(length>3){
                length=3;
            }

            array = new ArrayList<>();
            for (int i=0;i<length;i++){
               array.add(i,repo.v[i]);
            }
            list.setLayoutManager(new LinearLayoutManager(this));
            list.setHasFixedSize(true);
            adapter = new RecyclerAdapter(this, array);

            list.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        }
    }

我省略了进口。

我的RecyclerAdapter:

package com.example.adelina_pc.testscanner3;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

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

    private List<Product> mData;
    private LayoutInflater mInflater;

    // data is passed into the constructor
    RecyclerAdapter(Context context, List<Product> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.custom_row_products, parent, false);
        return new ViewHolder(view);
    }

    // binds the data to the TextView in each row
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       Product p= new Product(mData.get(position));
        holder.name.setText(p.get_name());
        holder.barcode.setText(String.valueOf(p.get_barCode()));
        holder.code.setText(String.valueOf(p.get_code()));
        holder.price.setText(String.valueOf(p.get_price()));
        holder.stock.setText(String.valueOf(p.get_sold()));

    }



    // total number of rows
    @Override
    public int getItemCount() {
        return mData.size()
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView name;
        TextView price;
        TextView code;
        TextView barcode;
        TextView stock;

        ViewHolder(View view) {
             super(view);
             name = view.findViewById(R.id.name);
             price = view.findViewById(R.id.price);
             code = view.findViewById(R.id.code);
             barcode = view.findViewById(R.id.barcode);
             stock = view.findViewById(R.id.stock);
        }

        @Override
        public void onClick(View view) {

        }
    }

}

我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="70dp"
    android:height="100dp"
    android:minHeight="20dp">

    <TextView
        android:id="@+id/code"
        android:layout_width="0dp"
        android:layout_height="46dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:gravity="center"
        android:text="cod"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/price"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/price"
        android:layout_width="0dp"
        android:layout_height="46dp"

        android:gravity="center"
        android:text="price"
        app:layout_constraintHorizontal_weight="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/name"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/code"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="46dp"
        android:gravity="center"
        android:text="nume"
        app:layout_constraintHorizontal_weight="10"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/barcode"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/price"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/barcode"
        android:layout_width="0dp"
        android:layout_height="46dp"
        android:gravity="center"
        android:text="codbare"
        app:layout_constraintHorizontal_weight="10"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/stock"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/name"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/stock"
        android:layout_width="0dp"
        android:layout_height="46dp"

        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:gravity="center"
        app:layout_constraintHorizontal_weight="3"
        android:text="stock"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/barcode"
        app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

PopularHourLayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".PopularHourActivity">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="204dp"
        android:layout_height="48dp"
        android:background="@drawable/custom_button"
        android:cursorVisible="true"
        android:gravity="center"
        android:text="Populat Hour "
        android:textColor="#000000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.057" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="0dp"
        android:layout_height="238dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.74" />
</android.support.constraint.ConstraintLayout>

我想念什么吗?我做错什么了吗?

1 个答案:

答案 0 :(得分:1)

在“活动”中这样声明RecyclerView时

RecyclerView.Adapter adapter;

您应该这样声明它

RecyclerAdapter adapter;

此外,删除调用notifyDataSetChanged()的行;您不需要使用设置适配器的方式

还有LayoutInflater,您应该像这样在onCreateViewHolder()中获得LayoutInflater:

LayoutInflater.from(parent.getContext())