在RecyclerView中获取随机数据android firebase数据库

时间:2017-11-07 23:37:43

标签: java android firebase firebase-realtime-database

Firebase数据库

enter image description here

在小孩(“AdUsersINFO”)中,我有一个孩子用户(userid)在这个孩子中我们有帖子。

我的目标是随机发布帖子并在RecyclerView

中显示数据

目前,我在onCreateView,上有此代码,但它没有获取任何数据:

recycle = (RecyclerView) view.findViewById(R.id.recycle);
        mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO");
        mAdUserDatabse.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                int ads = (int) dataSnapshot.getChildrenCount();
                int rand = new Random().nextInt(ads);
                Iterator itr = dataSnapshot.getChildren().iterator();

                for (int i = 0; i < rand; i++) {
                    list = new ArrayList<Adver>();
                    Adver value = dataSnapshot.getValue(Adver.class);
                    Adver fire = new Adver();
                    String type = value.getType();
                    String categories = value.getCategories();
                    String city = value.getCity();
                    fire.setType(type);
                    fire.setCategories(categories);
                    fire.setCity(city);
                    list.add(fire);

                    itr.next();
                }
                RecyclerAdapter recyclerAdapter = new RecyclerAdapter(list, getContext());
                final RecyclerView.LayoutManager recyce = new GridLayoutManager(getContext(), 2);
                recycle.setLayoutManager(recyce);
                recycle.setItemAnimator(new DefaultItemAnimator());
                recycle.setAdapter(recyclerAdapter);
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w("Failed to read value.", error.toException());
            }
        });

这是我的Adapter类

CardAdapter类

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.MyHoder> {

    List<Adver> list;
    Context context;

    public CardAdapter(List<Adver> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyHoder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.card, parent, false);
        return new MyHoder(view);
    }

    @Override
    public void onBindViewHolder(MyHoder holder, int position) {
        Adver mylist = list.get(position);
        holder.type.setText(mylist.getType());
        holder.categories.setText(mylist.getCategories());
        holder.city.setText(mylist.getCity());
        holder.availability.setText(mylist.getAvailability());
        holder.cost.setText(mylist.getCost());

    }

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

        }
        return arr;
    }

    class MyHoder extends RecyclerView.ViewHolder {
        TextView type, categories, city, availability, cost;
        ImageView img;

        public MyHoder(View itemView) {
            super(itemView);
            type = (TextView) itemView.findViewById(R.id.vType);
            categories = (TextView) itemView.findViewById(R.id.vCategories);
            city = (TextView) itemView.findViewById(R.id.vCity);
            availability = (TextView) itemView.findViewById(R.id.vAvailability);
            cost = (TextView) itemView.findViewById(R.id.vPrice);

        }
    }
}

2 个答案:

答案 0 :(得分:0)

您的DatabaseReference错了。您需要更改此行代码:

mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO");

mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO").child(uid);

其中uid是登录用户的用户ID。要获得uid,请使用以下代码:

String uid = FirebaseUser.getInstance().getCurrentUser().getUid();

对于您的代码,当您使用getChildren()方法进行迭代时,您可以按照我的理解来迭代用户而不是帖子。

答案 1 :(得分:0)

mAdUserDatabse = FirebaseDatabase.getInstance().getReference().child("AdUsersINFO");
mAdUserDatabse.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int ads = (int) dataSnapshot.getChildrenCount();
        int rand = new Random().nextInt(ads);
        for(DataSnapshot data: dataSnapshot.getChildren()){
        for(DataSnapshot datas: data.getChildren()){
        for(int i = 0; i < rand; i++) {

        list = new ArrayList<Adver>();

            Adver value = datas.getValue(Adver.class);
            Adver fire = new Adver();
            String type = value.getType();
            String categories = value.getCategories();
            String city = value.getCity();
            fire.setType(type);
            fire.setCategories(categories);
            fire.setCity(city);
            list.add(fire);

}}}}