2017-10-14 97 views
2

我想從我的Firebase數據庫檢索數據,但我的屏幕沒有顯示任何內容。我沒有看到任何錯誤,所以我不知道爲什麼沒有發生。任何人都能發現任何錯誤?我通過一個stackoverflow成員的幫助來跟蹤FirebaseUI指南。這些數據應顯示在街上,在屏幕上,用戶Android - RecyclerView不工作

Database

片段:

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

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

    Query keyQuery = FirebaseDatabase.getInstance().getReference("/Addresses/Street 10/users"); 
    DatabaseReference dataRef = FirebaseDatabase.getInstance().getReference("/User"); 
    recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 

    FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>() 
      .setIndexedQuery(keyQuery, dataRef, User.class) 
      .build(); 

    FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<User, BetViewHolder>(options) { 
     @Override 
     public BetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View view = LayoutInflater.from(parent.getContext()) 
        .inflate(R.layout.item_row, parent, false); 

      return new BetViewHolder(view); 
     } 

     @Override 
     protected void onBindViewHolder(BetViewHolder holder, int position, User model) { 
      holder.setUserName(model.getUsername()); 
     } 


    }; 

    recyclerView.setAdapter(adapter); 

    return rootView; 


} 


public static class BetViewHolder extends RecyclerView.ViewHolder { 

    TextView tw; 

    public BetViewHolder(View itemView) { 
     super(itemView); 

     tw = (TextView) itemView.findViewById(R.id.usersTextview); 
    } 

    public void setUserName(String username) { 
     tw.setText("Hej" + username); 
     Log.e("Activity", "In Vehicle " + username); 

    } 
} 

用戶等級:

public class User { 
    private String username; 
    private String usertype; 

    public User() { 
    } 

    public String getUsername() { 
     return this.username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getUsertype() { 
     return usertype; 
    } 

    public void setUsertype(String usertype) { 
     this.usertype = usertype; 
    } 
} 
+0

調試和檢查適配器的內容,如果有我檢查適配器是否是空的,adapter.getItemCount任何數據或不 – nomag

+0

和原來它是空的。我不明白爲什麼它是空的 – JDoe

+0

這是因爲keyQuery和dataRef指向不同的firebase節點。兩者都應該指向同一個父節點。 –

回答

2

在FirebaseUI的3.0版本,你需要明確請在適配器上調用startListening() & stopListening()指示它啓動/停止從數據庫檢索數據。

適配器生命週期 - 在以前的版本中的適配器就開始聽 在實例立即有cleanup()呼籲停止 聽。在3.x中,您必須明確地撥打startListening()stopListening()或將LifecycleOwner傳遞給選項生成器。

來源:Upgrading to FirebaseUI 3.0

相關問題