想要通过单击菜单项来更新现有的listView片段

时间:2018-11-23 02:56:17

标签: java android android-fragments firebase-realtime-database android-listview

我正在创建一个用于查找附近献血者的应用程序。在这个应用程序中,我使用三个片段进行滑动导航。在一个片段中,显示了可用的捐赠者列表,但是我想通过单击特定的血型来从菜单列表中更改列表。第一个列表是,但是当我单击项目时,它什么也没显示。

Java代码:

public class DonorsFragment extends Fragment {

    SwipeMenuListView listView;
    FirebaseListAdapter adapter;
    TextView fullName,bloodGroup,gender,age,address;
    Query query;
    View rootView;

    FirebaseListOptions<DonorAdapterClass> options;


    public DonorsFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);
    }

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

         rootView =inflater.inflate(R.layout.fragment_donors, container, false);

        listView = rootView.findViewById(R.id.listview);


Toast.makeText(getActivity(),"oncreateview",Toast.LENGTH_SHORT).show();

         query =FirebaseDatabase.getInstance().getReference().child("Users");

         setQuery(query);



        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth(170);
                // set a icon
                deleteItem.setIcon(R.drawable.ic_phone);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };

        listView.setMenuCreator(creator);

        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // open

                        Toast.makeText(getActivity(),"Item Clicked",Toast.LENGTH_SHORT).show();
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });

        // Inflate the layout for this fragment
        return rootView;

    }

    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.blood_group_menu,menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){

            case R.id.a_pos: {
                Toast.makeText(getActivity(),"A+",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_A_Pos").equalTo("yes");
                setQuery(query);
                break;
            }

            case R.id.a_neg: {
                Toast.makeText(getActivity(),"A-",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_A_Neg").equalTo("yes");
                setQuery(query);
                break;
            }


            case R.id.b_pos: {
                Toast.makeText(getActivity(),"B+",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_B_Pos").equalTo("yes");
                setQuery(query);
                break;
            }

            case R.id.b_neg: {
                Toast.makeText(getActivity(),"B-",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_B_Neg").equalTo("yes");
                setQuery(query);
                break;
            }

            case R.id.ab_pos: {
                Toast.makeText(getActivity(),"AB+",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_A_Pos").equalTo("yes");
                setQuery(query);
                break;
            }

            case R.id.ab_neg: {
                Toast.makeText(getActivity(),"AB-",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users");
                setQuery(query);
                break;
            }

            case R.id.o_pos: {
                Toast.makeText(getActivity(),"O+",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_O_Pos").equalTo("yes");
                setQuery(query);
                break;
            }

            case R.id.o_neg: {
                Toast.makeText(getActivity(),"O-",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_O_Neg").equalTo("yes");
                setQuery(query);
                break;
            }

            case R.id.list_all: {
                Toast.makeText(getActivity(),"All",Toast.LENGTH_SHORT).show();
                query=FirebaseDatabase.getInstance().getReference().child("Users");
                setQuery(query);
                break;
            }

        }
        return super.onOptionsItemSelected(item);
    }


    public void setQuery(Query query){

        listView = rootView.findViewById(R.id.listview);

        options = new FirebaseListOptions.Builder<DonorAdapterClass>()
                .setLayout(R.layout.donor_list)
                .setQuery(query,DonorAdapterClass.class)
                .build();

        adapter = new FirebaseListAdapter(options) {

            @Override
            protected void populateView(@NonNull View v, @NonNull Object model, int position) {
                DonorAdapterClass donors = (DonorAdapterClass) model;

                if(donors.getIsDonor().toString().equals("yes")){

                    fullName = v.findViewById(R.id.fullName_list);
                    bloodGroup = v.findViewById(R.id.bloodGroup_list);
                    gender = v.findViewById(R.id.gender_lis);
                    address = v.findViewById(R.id.address_list);
                    age = v.findViewById(R.id.age_list);



                    fullName.setText(donors.getFullName().toString());
                    bloodGroup.setText(donors.getBloodGroup().toString());
                    gender.setText(donors.getGender().toString());
                    address.setText(donors.getAddress().toString());
                    age.setText(donors.getAge().toString());

                }

            }
        };

        listView.setAdapter(adapter);

    }
}

所有firebase查询和适配器​​都在setQuery()方法中设置..当从onCreateView()调用时它正在工作...但是当从onOptionsItemSelected中的切换案例调用它时,它什么也不显示,并且以前的列表视图也隐藏

先谢谢了:)

1 个答案:

答案 0 :(得分:0)

这是我几个世纪以来的第一个答案,请耐心等待。这不是答案,而只是指向解决之道的指针。

在以下几点设置一个断点

  • onOptionsItemSelected
  • 案例R.id.a_pos
  • setQuery()
  • setQuery :: listView初始化语句
  • setQuery :: populateView :: donor初始化语句

运行该应用。使用调试器附加进程。现在,当您从选项中单击“ A +”时,它是从服务器获得响应还是引发异常?