我有适配器问题。谁能知道解决方案

时间:2019-03-17 08:12:20

标签: android firebase firebase-realtime-database firebase-authentication

有人可以帮我解决这个问题吗?我看过其他学习者发布的类似问题,但仍然无法解决我的问题。当应用程序运行时,活动仅显示白屏。

我的目录显示以下错误: E / RecyclerView:未连接适配器;跳过布局。

这是我的代码:

订单状态活动:

package com.example.android_foodbot;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android_foodbot.AccountActivity.User;
import com.example.android_foodbot.Model.Request;
import com.example.android_foodbot.ViewHolder.OrderViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class OrderStatusActivity extends AppCompatActivity {

    public RecyclerView recyclerView;
    public RecyclerView.LayoutManager layoutManager;

    FirebaseRecyclerAdapter<Request, OrderViewHolder> adapter;

    FirebaseDatabase database;
    DatabaseReference requests;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_status);
    enter code here

        //Firebase
        database = FirebaseDatabase.getInstance();
        requests = database.getReference("Requests");

        recyclerView = (RecyclerView)findViewById(R.id.listOrders);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);



        //Getting current user details
        String uid;
        FirebaseAuth auth;
        auth = FirebaseAuth.getInstance();
        uid = auth.getCurrentUser().getUid();

        FirebaseDatabase.getInstance().getReference().child("Users").child(uid).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);
//                if (user==null){
//                    Toast.makeText(OrderStatusActivity.this, "NUll user", Toast.LENGTH_SHORT).show();
//                }
//                else{
//                    Toast.makeText(OrderStatusActivity.this, "user is "+user.getPhone(), Toast.LENGTH_SHORT).show();
//                }

                loadOrders(user.getPhone());

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                throw databaseError.toException();

            }
        });



    }


    private void loadOrders(String phone){
//        Toast.makeText(this, "phone is "+phone, Toast.LENGTH_SHORT).show();
        adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(
                Request.class,
                R.layout.order_layout,
                OrderViewHolder.class,
                requests.orderByChild("phone").equalTo(phone)) {
            @Override
            protected void populateViewHolder(OrderViewHolder viewHolder, Request model, int position) {
                viewHolder.txtOrderId.setText(adapter.getRef(position).getKey());
                viewHolder.txtOrderStatus.setText(convertCodeToStatus(model.getStatus()));
                viewHolder.txtOrderPhone.setText(model.getPhone());



            }
        };
        recyclerView.setAdapter(adapter);


    }


    private String convertCodeToStatus(String status){
        switch (status) {
            case "0":
                return "Placed";
            case "1":
                return "Processing";
            default:
                return "Served";
        }

    }


}

订单视图持有人:

package com.example.android_foodbot.ViewHolder;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.android_foodbot.Interface.ItemClickListener;
import com.example.android_foodbot.R;

public class OrderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView txtOrderId, txtOrderStatus, txtOrderPhone;

    private ItemClickListener itemClickListener;

    public OrderViewHolder(@NonNull View itemView) {
        super(itemView);

        txtOrderId = (TextView)itemView.findViewById(R.id.order_id);
        txtOrderStatus = (TextView)itemView.findViewById(R.id.order_status);
        txtOrderPhone = (TextView)itemView.findViewById(R.id.order_phone);

        itemView.setOnClickListener(this);

    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public void onClick(View v) {
        itemClickListener.onClick(v, getAdapterPosition(), false);

    }
}

订单活动:

<?xml version="1.0" encoding="utf-8"?>
<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=".OrderStatusActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/listOrders"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"

        >


    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

当我检查适配器时,我发现getItemCount()方法返回0值。有人知道如何解决这个问题吗?

private void loadOrders(String phone){

    adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(
            Request.class,
            R.layout.order_layout,
            OrderViewHolder.class,
            requests.orderByChild("phone").equalTo(phone)) {
        @Override
        protected void populateViewHolder(OrderViewHolder viewHolder, Request model, int position) {
            viewHolder.txtOrderId.setText(adapter.getRef(position).getKey());
            viewHolder.txtOrderStatus.setText(convertCodeToStatus(model.getStatus()));
            viewHolder.txtOrderPhone.setText(model.getPhone()); //                Toast.makeText(OrderStatusActivity.this, "Show", Toast.LENGTH_SHORT).show();




        }


    };

    recyclerView.setAdapter(adapter);


    if (recyclerView != null){
        recyclerView.setHasFixedSize(true);
        Toast.makeText(this, "Recycler View is not null ", Toast.LENGTH_SHORT).show();

    }
    if (recyclerView == null){ //            Log.v("TAG", "RECYCLERVIEW NULL");
        Toast.makeText(this, "RECYCLERVIEW NULL", Toast.LENGTH_SHORT).show();

    } else if (layoutManager == null){ //            Log.v("TAG", "LAYOUTMANAGER NULL");
        Toast.makeText(this, "LAYOUTMANAGER NULL", Toast.LENGTH_SHORT).show();

    } else if (adapter == null) { //            Log.v("TAG", "mFIREADAPTER NULL");
        Toast.makeText(this, "mFIREADAPTER NULL", Toast.LENGTH_SHORT).show();

    }else if (adapter != null) { //            Log.v("TAG", "mFIREADAPTER NULL");
        Toast.makeText(this, "FIREADAPTER IS NOT NULL" +adapter.getItemCount(), Toast.LENGTH_SHORT).show();

    }


}

1 个答案:

答案 0 :(得分:0)

添加此行:

<pdf-viewer [src]="link" [render-text]="true" style="display: block"></pdf-viewer>

首先在recyclerView.setAdapter(adapter); 中初始化onCreate,而不是在查询完成时初始化,而在查询完成时,只需使用新数据更新reyclerView变量即可。

快乐编码