小编典典

如何在Firebase Android中执行SQL IN子句查询

java

如何在Firebase Android中查询SQL IN子句?我想在Firebase
Recycler适配器中使用它,以便根据某些条件仅检索某些子级。类似于以下语句:

SQL----> select * from posts where city in(10 cities comes here)

我需要一个Firebase查询才能在Firebase Recycler适配器中使用它。


阅读 301

收藏
2020-11-30

共1个答案

小编典典

我找到了解决方案:我们无法使用FirebaseRecyclerAdapter。相反,我们必须创建可扩展的自定义适配器RecyclerView.ViewHolder

要将值传递给此适配器,首先我们必须使用检索数据addValueEventListener,然后必须将值传递给我们的适配器。

这是我的代码…

final ArrayList<Timeline> timelines = new ArrayList<>();

    mDatabaseTimeline.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            final Timeline timeline = dataSnapshot.getValue(Timeline.class);

            if(timeline != null){

                mDatabaseFriends.child(mAuth.getCurrentUser().getUid()).child("active").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (mAuth.getCurrentUser().getUid().equals(timeline.getUid()) || dataSnapshot.hasChild(timeline.getUid())) {

                            timelines.add(timeline);
                            mTimelineRecycler.setAdapter(new RecyclerAdapter(TimelineFragment.this.getContext(), timelines));

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

适配器----->

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

    Context context;
    ArrayList<Timeline> timeline;


    public RecyclerAdapter(Context context, ArrayList<Timeline> timeline) {
        this.context = context;
        this.timeline = timeline;



    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View row = inflater.inflate(R.layout.timeline_row, parent, false);
        TimelineViewHolder holder = new TimelineViewHolder(row);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        final String post_key = timeline.get(position).getPostkey();

        ((TimelineViewHolder) holder).setUsername(timeline.get(position).getUsername());


 }

    @Override
    public int getItemCount() {
        return timeline.size();
    }

    public class TimelineViewHolder extends RecyclerView.ViewHolder {
        public TimelineViewHolder(View itemView) {
            super(itemView);

            view = itemView;
        }

        public View getView() {
            return view;
        }

        public void setUsername(String username) {
            TextView usernameTxtView = (TextView) view.findViewById(R.id.timeline_username);
            usernameTxtView.setText(username);
        }

    }
}
2020-11-30