Data did n't load in firebase app properly

后端 未结 1 642
天涯浪人
天涯浪人 2021-01-28 05:30

I created an app using firebase on android studio.There is a searchview will appear after authentication with mobile otp. When user search on search view it will filter the resu

1条回答
  •  清酒与你
    2021-01-28 06:04

    All code you have written in onCreateView should be written in onViewCreated.

    It's better to do any assignment of subviews to fields in onViewCreated. This is because the framework does an automatic null check for you to ensure that your Fragment's view hierarchy has been created and inflated.

    public class FragmentHome extends Fragment {
        private SearchView search;
        View v;
        DatabaseReference dataRef;
        private  FirebaseListAdapter firebaseListAdapter;
        ListView mListView;
        private TextView text;
        ArrayList city=new ArrayList();
        private int i=0;
        private String data,val,var;
        ArrayAdapter adapter;
        ProgressBar progressBar;
    
        @Nullable
        @Override
    
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) {
            v = inflater.inflate(R.layout.activity_fragment_home,container,false);
            if (container!=null){
                container.removeAllViews();
            }
            return v;
        }
    
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        progressBar= (ProgressBar) v.findViewById(R.id.search_pro);
        search= (SearchView) v.findViewById(R.id.search);
        search.onActionViewExpanded();
        search.setIconified(false);
        search.setInputType(InputType.TYPE_NULL);
        search.setQueryHint("Search Docter By City Or Department");
        mListView= (ListView) v.findViewById(R.id.mList);
        mListView.setVisibility(View.INVISIBLE);
    
    dataRef=FirebaseDatabase.getInstance().getReference().child("Docters");
    adapter=new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, city);
    mListView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    dataRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            data=dataSnapshot.child("city").getValue(String.class);
            val=dataSnapshot.child("department").getValue(String.class);
            if (!city.contains(data)){
                city.add(data);
            }
            if (!city.contains(val)){
                city.add(val);
            }
            search.setInputType(InputType.TYPE_CLASS_TEXT);
            progressBar.setVisibility(View.GONE);
            adapter.notifyDataSetChanged();
    
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            data=dataSnapshot.child("city").getValue(String.class);
            val=dataSnapshot.child("department").getValue(String.class);
            city.add(data);
            city.add(val);
            city.add(var);
            adapter.notifyDataSetChanged();
        }
    
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
    
        }
    
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String text) {
    
            return false;
        }
    
        @Override
        public boolean onQueryTextChange(String text) {
            if (!text.isEmpty()){
                adapter.notifyDataSetChanged();
            mListView.setVisibility(View.VISIBLE);
               adapter.getFilter().filter(text);
            }else{
                mListView.setVisibility(View.INVISIBLE);
            }
            return false;
        }
    
    });
    
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            String itemRef = (String) adapter.getItem(position);
            search.setVisibility(View.INVISIBLE);
            mListView.setVisibility(View.INVISIBLE);
            FragmentTransaction ft=getChildFragmentManager().beginTransaction();
            DocterList fragment=new DocterList();
            Bundle item=new Bundle();
            item.putString("item",itemRef);
            fragment.setArguments(item);
            ft.replace(R.id.activity_fragment_home,fragment);
            ft.addToBackStack(null);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }
    });
        }
    

    progressBar= (ProgressBar) v.findViewById(R.id.search_pro); ==> the view will only come into existence in onViewCreated

    0 讨论(0)
提交回复
热议问题