我想通过使用orderbylist和按值排序从Firebase数据库中获取一些用户数据

时间:2020-09-13 11:42:50

标签: android firebase firebase-realtime-database

在这里,我上传数据库图片,查询在该数据库中。我在“任务类型”中具有在线和物理等多类型用户的值。我只想获取那些属于物理任务类型的用户数据。我使用查询功能,但无法正常工作。 我为此使用片段布局,这是我的片段代码 片段代码


import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

import com.example.smartup.R;
import com.example.smartup.ui.PostTask.PostTaskClass;
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.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.HashMap;

public class EarnFragment extends Fragment{
    String[] task = {"All Task", "Physical", "Online"};
    private Spinner earnspiner;
    private RecyclerView recyclerView;
    private EarnAdapter earnAdapter;
    private ArrayList<PostTaskClass> arrayList;
    private DatabaseReference reference;
    private ImageButton search, location, filter;
    private SwipeRefreshLayout refreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_earn, container, false);
        search = root.findViewById(R.id.earnbtnsearch);
        location = root.findViewById(R.id.earnbtnlocation);
        filter = root.findViewById(R.id.earnbtnadjust);
        earnspiner = root.findViewById(R.id.earnspinner);
        ArrayAdapter aa = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_item,task);
        aa.setDropDownViewResource(android.R.layout.simple_list_item_checked);
        earnspiner.setAdapter(aa);
        recyclerView = root.findViewById(R.id.earn_recycler_view);
        refreshLayout = root.findViewById(R.id.auto_refresh_earn);
        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.butns));
        arrayList = new ArrayList<>();
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        earnspiner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                switch (adapterView.getSelectedItemPosition()){
                    case 0:
                        arrayList.clear();
                        reference = FirebaseDatabase.getInstance().getReference("All Tasks");
                        reference.addValueEventListener(listener);
                        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                            @Override
                            public void onRefresh() {
                                arrayList.clear();
                                reference = FirebaseDatabase.getInstance().getReference("All Tasks");
                                reference.addValueEventListener(listener);
                                refreshLayout.setRefreshing(false);
                            }
                        });
                        break;
                    case 1:
                        Query query = FirebaseDatabase.getInstance().getReference("All Tasks")
                                .orderByChild("tasktype")
                                .equalTo("Physical");
                        query.addValueEventListener(listener);
                        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                            @Override
                            public void onRefresh() {
                                Query query = FirebaseDatabase.getInstance().getReference("All Tasks")
                                        .orderByChild("tasktype")
                                        .equalTo("Physical");
                                query.addValueEventListener(listener);
                                refreshLayout.setRefreshing(false);
                            }
                        });
                        break;
                    case 2:
                        arrayList.clear();
                        Query query1 = FirebaseDatabase.getInstance().getReference("All Tasks")
                                .orderByChild("tasktype")
                                .equalTo("Online");
                        query1.addValueEventListener(listener);
                        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                            @Override
                            public void onRefresh() {
                                arrayList.clear();
                                Query query1 = FirebaseDatabase.getInstance().getReference("All Tasks")
                                        .orderByChild("tasktype")
                                        .equalTo("Online");
                                query1.addValueEventListener(listener);
                                refreshLayout.setRefreshing(false);
                            }
                        });
                        break;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                Toast.makeText(getActivity(), "Nothing to select", Toast.LENGTH_SHORT).show();
            }
        });
        earnAdapter = new EarnAdapter(getContext(), arrayList);
        recyclerView.setAdapter(earnAdapter);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return root;
    }
    ValueEventListener listener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            arrayList.clear();
            if (snapshot.exists()) {
                for (DataSnapshot snapshot1 : snapshot.getChildren()) {
                    if (snapshot1.exists()) {
                        for (DataSnapshot snapshot2 : snapshot1.getChildren()) {
                            PostTaskClass pt = snapshot2.getValue(PostTaskClass.class);
                            arrayList.add(pt);
                        }
                        earnAdapter.notifyDataSetChanged();
                    }
                    PostTaskClass ptc = snapshot1.getValue(PostTaskClass.class);
                    arrayList.add(ptc);
                }
                earnAdapter.notifyDataSetChanged();
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(getActivity(), error+"", Toast.LENGTH_SHORT).show();
        }
    };
}```
  [1]: https://i.stack.imgur.com/3rme7.png

0 个答案:

没有答案