2017-09-22 62 views
0

我在android studio上使用firebase創建了一個應用程序。使用移動otp進行身份驗證後會出現一個searchview。當用戶在搜索視圖上搜索時,它會過濾結果並轉到回收站視圖等。首次啓動活動(即,認證後的家庭活動)應用程序完美運行。數據未正確載入Firebase應用程序

但我的問題是我的應用沒有加載數據後一段時間later.Also其他領域,包括searchview也沒有工作。另外,應用程序在註銷後工作,並在發生此問題後再次登錄到應用程序。

有人知道如何解決它嗎?

public class FragmentHome extends Fragment { 
private SearchView search; 
View view; 
DatabaseReference dataRef; 
private FirebaseListAdapter<String> firebaseListAdapter; 
ListView mListView; 
private TextView text; 
ArrayList<String> city=new ArrayList<String>(); 
private int i=0; 
private String data,val,var; 
ArrayAdapter<String> adapter; 
ProgressBar progressBar; 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable final Bundle savedInstanceState) { 
    View v= inflater.inflate(R.layout.activity_fragment_home,container,false); 
    if (container!=null){ 
     container.removeAllViews(); 
    } 
    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<String>(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(); 
     } 
    }); 
    return v; 
} 

}

+0

這是很難搞清楚的問題,而無需代碼 – mrid

+0

'可以肯定的說這產生的問題是不是由於我的code':如何你能這麼肯定嗎? – mrid

+0

由於該應用程序工作很長一段時間,即使我關閉了應用程序並重新開始。 – Suhail

回答

0

你寫在onCreateView所有的代碼應該在onViewCreated被寫入。

最好對onViewCreated中的字段進行任何子視圖分配。這是因爲框架爲你做了一個自動的空檢查,以確保你的Fragment的視圖層次結構已被創建和膨脹。

public class FragmentHome extends Fragment { 
    private SearchView search; 
    View v; 
    DatabaseReference dataRef; 
    private FirebaseListAdapter<String> firebaseListAdapter; 
    ListView mListView; 
    private TextView text; 
    ArrayList<String> city=new ArrayList<String>(); 
    private int i=0; 
    private String data,val,var; 
    ArrayAdapter<String> 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<String>(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); ==>視圖只會憑空出現的onViewCreated

+0

這是修復問題嗎? – Suhail

+0

無論如何,我現在要嘗試... – Suhail

+0

@Suhail我沒有整個代碼,所以不能確定 – mrid

相關問題