回收者更新卡片视图但不显示子视图

时间:2018-06-26 16:48:33

标签: java android firebase android-studio android-recyclerview

我正在尝试使用RecyclerView在cardview中接收Firebase节点。该应用程序显示Cardview项目,但不会更新详细信息。

这是我的基地 Firebase Db

活动

    public class PizzaActivity extends AppCompatActivity {
    private static Context mContext;
    private RecyclerView recyclerView;
    private FirebaseDatabase firebaseDatabase;
    private DatabaseReference databaseReference;
    private ArrayList allItems;
    private PizzaAdapter pizzaAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pizza);
        mContext=PizzaActivity.this;
        allItems=new ArrayList<Pizza>();

        firebaseDatabase=FirebaseDatabase.getInstance();
        databaseReference=firebaseDatabase.getReference("pizza");
        databaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                getAllItems(dataSnapshot);

            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                getAllItems(dataSnapshot);
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                getAllItems(dataSnapshot);
            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                getAllItems(dataSnapshot);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        recyclerView= findViewById(R.id.recycler_view);

        RecyclerView.LayoutManager recylerViewLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(recylerViewLayoutManager);

    }
    private void getAllItems(DataSnapshot dataSnapshot){
        for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
           // allItems.add(new Pizza());
            Pizza pizza = dataSnapshot.getValue(Pizza.class);
            pizzaAdapter = new PizzaAdapter(PizzaActivity.this, pizza);
            recyclerView.setAdapter(pizzaAdapter);
        }
    }

}

适配器类

 public class PizzaAdapter extends RecyclerView.Adapter<RecyclerViewHolder>  {
    private List<Pizza> pizzas;
    private Context context;

    public PizzaAdapter(Context context,List<Pizza>pizzas){
        this.context=context;
        this.pizzas=pizzas;
    }

    public PizzaAdapter(PizzaActivity context, Pizza pizza) {
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.pizza_delivery_item, parent, false);
        return new RecyclerViewHolder(layoutView, pizzas);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerViewHolder holder, int position) {

        holder.pizzaName.setText(pizzas.get(position).getType());
        holder.pizzaDesc.setText(pizzas.get(position).getSize());
        holder.location.setText(pizzas.get(position).getLocation());
        holder.description.setText(pizzas.get(position).getDescription());

        holder.delivered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.delivered.setImageResource(R.drawable.deliveredicon);

            }
        });
        holder.call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Toast.makeText(get," Can not complete call at the moment.", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        int arr=0;
        try {
            if (pizzas.size()==0){
                arr=0;
            }else {
                arr=pizzas.size();
            }
        }catch (Exception e){

        }
        return arr;
    }
}

持有人

    class RecyclerViewHolder extends RecyclerView.ViewHolder {

    public  View mView;
    public  TextView pizzaName,pizzaDesc,location,description;
    public  ImageView delivered,call;


    public RecyclerViewHolder(View layoutView, List<Pizza> pizzas) {
        super(layoutView);


        pizzaDesc= layoutView.findViewById(R.id.pizza_size);
        pizzaName= layoutView.findViewById(R.id.pizza_name);
        delivered=layoutView.findViewById(R.id.delivery_check);
        location=layoutView.findViewById(R.id.location);
        description=layoutView.findViewById(R.id.description);
        call=layoutView.findViewById(R.id.call);

        delivered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delivered.setImageResource(R.drawable.deliveredicon);

            }
        });
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
}

对象类

    public class Pizza {
    public String location;
    public String description;
    public String type;
    public String size;
    public String phone;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Pizza(String location, String description, String type, String size) {
        this.location = location;
        this.description = description;
        this.type = type;
        this.size = size;
    }
    public Pizza(){}

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }
}

有帮助吗?过去几天一直在尝试解决此问题。日志中没有错误,应用也不会崩溃。卡片视图什么也没显示。

0 个答案:

没有答案