没有从Android Recycler视图中的Firebase Firestore获取数据

时间:2018-02-08 18:13:00

标签: android firebase android-recyclerview google-cloud-firestore

我无法在我的Android应用程序中将数据从cloud firestore设置为recyclerview。我最近从firebase数据库切换到firestore。

public class OrderNow extends Fragment {


    private RecyclerView mDishList;
    private FirebaseFirestore mFirestore;
    private FirestoreRecyclerAdapter adapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private List<DishClass> veg, nonveg, both;
    //private DishListAdapter dishListAdapter;
    LinearLayoutManager linearLayoutManager;

    String[] categoryItems;
    private String category = "Veg";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.ordernow_tab, container, false);
        setHasOptionsMenu(true);
        ButterKnife.bind(getActivity());


        mDishList = (RecyclerView) rootView.findViewById(R.id.dishesList);
        mDishList.setHasFixedSize(true);

        linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mDishList.setLayoutManager(linearLayoutManager);

        veg = new ArrayList<>();
        nonveg = new ArrayList<>();
        both = new ArrayList<>();

        //dishListAdapter = new DishListAdapter(getActivity(), both);

        //mDishList.setAdapter(dishListAdapter);
        mFirestore = FirebaseFirestore.getInstance();
        // Set up the ViewPager with the sections adapter.
        getData();
        //mViewPager.endFakeDrag();
        categoryItems = new String[]{"Veg", "NonVeg"};
        //getData(category);

        return rootView;
    }

    public void getData() {
        Toast.makeText(getContext(), "Enter into getdata", Toast.LENGTH_SHORT).show();
        Query query = mFirestore.collection("Dishes");

        FirestoreRecyclerOptions<DishClass> dishClassRecycler = new FirestoreRecyclerOptions.Builder<DishClass>()
                .setQuery(query, DishClass.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<DishClass, DishHolder>(dishClassRecycler) {
            @Override
            protected void onBindViewHolder(DishHolder holder, int position,  DishClass model) {
                Toast.makeText(getContext(), "Enter into adapter", Toast.LENGTH_SHORT).show();
                holder.dish_name.setText(model.getDishName());
                holder.dish_price.setText(model.getDishPrice());

                Picasso.with(getContext()).load(model.getDishImage()).into(holder.dish_image);
            }



            @Override
            public DishHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.card, parent, false);

                return new DishHolder(view);
            }
            @Override
            public void onError(FirebaseFirestoreException e) {
                Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        };
        adapter.notifyDataSetChanged();
        mDishList.setAdapter(adapter);
    }
    public class DishHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.txtDishName)
        TextView dish_name;
        @BindView(R.id.txtDishPrice)
        TextView dish_price;
        @BindView(R.id.imgDish)
        ImageView dish_image;


        public DishHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }

    }
    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

这是logcat。错误显示我在null TextView引用上设置字符串。

FATAL EXCEPTION: main
Process: com.cofoz.cofoz, PID: 17404
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    at com.cofoz.cofoz.OrderNow$1.onBindViewHolder(OrderNow.java:105)
    at com.cofoz.cofoz.OrderNow$1.onBindViewHolder(OrderNow.java:101)
    at com.firebase.ui.firestore.FirestoreRecyclerAdapter.onBindViewHolder(FirestoreRecyclerAdapter.java:125)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6508)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6541)

这是我的数据库。在我的数据库中,我有一个根目录&#34; Dishes&#34;然后我保存了每道菜的细节。

Firestore

DishClass

public class DishClass {
    private String dishName;
    private String dishImage;
    private String dishPrice;


    public DishClass(){}

    public DishClass(String dishName, String dishImage, String dishPrice) {
        this.dishName = dishName;
        this.dishImage = dishImage;
        this.dishPrice = dishPrice;

    }



    public String getDishName() {
        return dishName;
    }

    public void setDishName(String dishName) {
        this.dishName = dishName;
    }

    public String getDishImage() {
        return dishImage;
    }

    public void setDishImage(String dishImage) {
        this.dishImage = dishImage;
    }

    public String getDishPrice() {
        return dishPrice;
    }

    public void setDishPrice(String dishPrice) {
        this.dishPrice = dishPrice;
    }
}

0 个答案:

没有答案