如何以滑动刷新布局检索特定用户

问题描述 投票:0回答:1

我正在创建一个婚姻应用程序,并使用firebase作为数据库,其中第一个片段是搜索配置文件。在搜索配置文件中,它显示了数据库中的所有配置文件,可能是男性还是女性,但我想显示女性用户的男性配置文件,反之亦然。反之亦然。并且每个用户都是经过身份验证的用户。我尝试了不同的查询,但没有成功。请帮助解决问题。

这些是firebase中的用户:

These are users in firebase

这些是每个用户中的字段:

These are the fields in every user

下面是搜索配置文件的代码:

public class Search_fragment extends Fragment {

    private SwipeRefreshLayout mSwipeRefreshLayout;
    DatabaseReference databaseReference;
    Firebase firebase;
   // List<User> list = new ArrayList<User>();
    RecyclerView recyclerView;
    Boolean isScrolling = false;
    Context context;
    FirebaseAuth mAuth;

    Button filter;
    FirebaseRecyclerPagingAdapter<User, UserViewHolder> adapter;
    User user10=new User();
    FirebaseUser firebaseUser;

    public Search_fragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        final View view =  inflater.inflate(R.layout.search__profile, container, false);

        ((AppCompatActivity) getActivity()).getSupportActionBar().hide();

        String authgender=user10.getGender();


        filter=(Button)view.findViewById(R.id.button9);
        filter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent(getActivity(),FilterMain.class);
                startActivity(it);
            }
        });

        mSwipeRefreshLayout =view.findViewById(R.id.swipe_refresh_layout);
        recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


        databaseReference = FirebaseDatabase.getInstance().getReference().child("User");
        PagedList.Config config = new PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPrefetchDistance(2)
                .setPageSize(1)
                .build();


        DatabasePagingOptions<User> options = new DatabasePagingOptions.Builder<User>()
                .setLifecycleOwner(this)
                .setQuery(databaseReference, config, User.class)
                .build();

        adapter = new FirebaseRecyclerPagingAdapter<User, UserViewHolder>(options) {
            @NonNull
            @Override
            public UserViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new UserViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler, parent, false),context) {
                };
            }
            @Override
            protected void onBindViewHolder(@NonNull final UserViewHolder viewHolder, final int i, @NonNull final User user) {

                viewHolder.setItem(user, i);


                int newPosition = viewHolder.getAdapterPosition();

            }
            @Override
            protected void onLoadingStateChanged(@NonNull LoadingState state) {
                switch (state) {
                    case LOADING_INITIAL:
                    case LOADING_MORE:
                        // Do your loading animation
                        mSwipeRefreshLayout.setRefreshing(true);
                        break;

                    case LOADED:
                        // Stop Animation
                        mSwipeRefreshLayout.setRefreshing(false);
                        break;

                    case FINISHED:
                        //Reached end of Data set
                        mSwipeRefreshLayout.setRefreshing(false);
                        break;

                    case ERROR:
                        retry();
                        break;
                }
            }
            @Override
            protected void onError(@NonNull DatabaseError databaseError) {
                super.onError(databaseError);
                mSwipeRefreshLayout.setRefreshing(false);
                databaseError.toException().printStackTrace();
                // Handle Error
                retry();
            }
        };
        recyclerView.setAdapter(adapter);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                adapter.refresh();
            }
        });
return view;
    }
}

下面是UserViewHolder的代码:

class UserViewHolder extends RecyclerView.ViewHolder {

    DatabaseReference databaseReference;
    Firebase firebase;
    FirebaseAuth mAuth;
    List<User> MainImageUploadInfoList;
    ImageView imageView,invite;
    CircleImageView shortlist;
    public TextView FirstNameTextView;
    public TextView DateTextView;
    public TextView HeightTextView;
    public TextView EducationTextView;
    public TextView OccupationTextView;
    public TextView UserIDTextView;
    public Button send_invitation,shortlist_profile;
    public TextView income;
    public TextView city_state;
    public TextView hobbies;
    public TextView marital_status;
    public TextView family_members;
    public TextView mothers_name;
    public TextView fathers_name;
    public TextView fathers_occupation;
    public TextView mothers_occupation;
    public TextView gendertv;
    public CardView mCardView;
    String sluidgl,sluidtemp;
    public List<User> mUploads;
    int flag=0;
    HashSet<String> set=new HashSet<String>();

    User user=new User();
    private final Context context;
    // private Object Context;

    public UserViewHolder(@NonNull final View itemView, final Context context) {
        super(itemView);
        this.context=context;

        itemView.setTag(user.getUser_id());
        mCardView = (CardView) itemView.findViewById(R.id.cardview1);
        FirstNameTextView = (TextView) itemView.findViewById(R.id.textView1);
        DateTextView = (TextView) itemView.findViewById(R.id.textView3);
        HeightTextView = (TextView) itemView.findViewById(R.id.textView4);
        EducationTextView = (TextView) itemView.findViewById(R.id.textView5);
        OccupationTextView = (TextView) itemView.findViewById(R.id.textView6);
        UserIDTextView = (TextView) itemView.findViewById(R.id.textView7);
        income = (TextView) itemView.findViewById(R.id.income);
        city_state = (TextView) itemView.findViewById(R.id.city_state);
        hobbies = (TextView) itemView.findViewById(R.id.hobbies);
        marital_status = (TextView) itemView.findViewById(R.id.marital_status);
        family_members = (TextView) itemView.findViewById(R.id.family_members);
        mothers_name = (TextView) itemView.findViewById(R.id.mothers_name);
        fathers_name = (TextView) itemView.findViewById(R.id.fathers_name);
        fathers_occupation = (TextView) itemView.findViewById(R.id.fathers_occupation);
        mothers_occupation = (TextView) itemView.findViewById(R.id.mothers_occupation);
        gendertv = (TextView) itemView.findViewById(R.id.gender);
        imageView=(ImageView)itemView.findViewById(R.id.imageView2);
        shortlist=(CircleImageView)itemView.findViewById(R.id.shortlisted);
        invite=(ImageView)itemView.findViewById(R.id.invite);

 mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = (int) itemView.getTag();

                for(i=0;i<=0;i++){
                    final Intent it = new Intent(v.getContext(), ViewProfile.class);
                    it.putExtra("first_name", FirstNameTextView.getText().toString());
                    it.putExtra("date", DateTextView.getText().toString());
                    it.putExtra("height", HeightTextView.getText().toString());
                    it.putExtra("city_state", city_state.getText().toString());
                    it.putExtra("hobbies", hobbies.getText().toString());
                    it.putExtra("highest_education", EducationTextView.getText().toString());
                    it.putExtra("occupation", OccupationTextView.getText().toString());
                    it.putExtra("income", income.getText().toString());
                    it.putExtra("marital_status", marital_status.getText().toString());
                    it.putExtra("family_members", family_members.getText().toString());
                    it.putExtra("fathers_name", fathers_name.getText().toString());
                    it.putExtra("mothers_name", mothers_name.getText().toString());
                    it.putExtra("fathers_occupation", fathers_occupation.getText().toString());
                    it.putExtra("mothers_occupation", mothers_occupation.getText().toString());
                    it.putExtra("user_id", UserIDTextView.getText().toString());
                    imageView.buildDrawingCache();
                    Bitmap bitmap = imageView.getDrawingCache();
                    it.putExtra("profile_image",bitmap);
                    it.putExtra("positionGroup", i);
                    itemView.getContext().startActivity(it);
                }
            }
        });
    }
    public void setItem(final User user, final int i){
mCardView.setTag(i);
                    FirstNameTextView.setText(user.getFirst_name() + " " + user.getLast_name());
                    DateTextView.setText(user.getDate());
                    HeightTextView.setText(user.getHeight());
                    EducationTextView.setText(user.getHighest_education());
                    OccupationTextView.setText(user.getOccupation());
                    UserIDTextView.setText(user.getUser_id());
                    income.setText(user.getIncome());
                    city_state.setText(user.getCity_state());
                    hobbies.setText(user.getHobbies());
                    marital_status.setText(user.getMarital_status());
                    family_members.setText(user.getFamily_members());
                    mothers_name.setText(user.getMothers_name());
                    fathers_name.setText(user.getFathers_name());
                    fathers_occupation.setText(user.getFathers_occupation());
                    mothers_occupation.setText(user.getMothers_occupation());
                    gendertv.setText(user.getGender());
                    Picasso.get().load(user.getMainImage()).placeholder(R.mipmap.ic_launcher).fit().into(imageView);
  }
}

编辑:

W/PersistentConnection: pc_0 - Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding '".indexOn": "gender"' at User/User to your security and Firebase Database rules for better performance
W/System.err: com.google.firebase.database.DatabaseException: Firebase Database error: Data not found at given child path!
        at com.google.firebase.database.DatabaseError.toException(com.google.firebase:firebase-database@@19.2.1:229)
W/System.err:     at accentra.in.Search_fragment$2.onError(Search_fragment.java:142)
        at com.shreyaspatil.firebase.recyclerpagination.FirebaseRecyclerPagingAdapter$4.onChanged(FirebaseRecyclerPagingAdapter.java:76)
W/System.err:     at com.shreyaspatil.firebase.recyclerpagination.FirebaseRecyclerPagingAdapter$4.onChanged(FirebaseRecyclerPagingAdapter.java:73)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
W/System.err:     at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
W/System.err:     at androidx.lifecycle.Transformations$2$1.onChanged(Transformations.java:155)
        at androidx.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:152)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
W/System.err:     at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
W/System.err:     at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at androidx.lifecycle.LiveData$1.run(LiveData.java:91)
        at android.os.Handler.handleCallback(Handler.java:794)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6662)
        at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
W/FirebaseDataSource: retry() not valid when in state: LOADING_INITIAL
android firebase-realtime-database swiperefreshlayout
1个回答
0
投票
您可以在Firebase查询中过滤出不同性别的用户。

根据您的数据,您可以应用以下查询:

FirebaseDatabase.getInstance().getReference().child("User").orderByChild("gender").equalTo("REQUIRED_GENDER_HERE");

将此数据传递到您的视图,它应该可以工作。在您的情况下,由于您是在回收站视图上使用Firebase包装器,因此在代码中更新查​​询就足够了。

为了警告您的数据库中不存在有关性别的索引警告,您需要在Firebase规则中指定索引。在这里查看规则文档:https://firebase.google.com/docs/database/security/indexing-data

© www.soinside.com 2019 - 2024. All rights reserved.