2

我剛開始使用Firestore。所以我嘗試將我的數據直接放入recyclerview,這裏是我用作參考的code。 這裏是我的UserRecyclerView片段:使用FirebaseUI進入recyclerview的Firestore,沒有顯示數據

public class UserRecyclerView extends Fragment{ 
private ArrayList<User> user; 
RecyclerView mRecyclerView; 
public UserRecyclerView(){} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.recycler_view,container,false); 
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); 

    FirestoreRecyclerAdapter adapter = getFirestoreRecyclerAdapter(); 
    mRecyclerView.setAdapter(adapter); 
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 

return view; 
} 

@NonNull 
private FirestoreRecyclerAdapter getFirestoreRecyclerAdapter() { 
    Query query = FirebaseFirestore.getInstance() 
      .collection("users"); 

    FirestoreRecyclerOptions<User> options = new FirestoreRecyclerOptions.Builder<User>() 
      .setQuery(query, User.class).build(); 

    return new FirestoreRecyclerAdapter<User, UserViewHolder>(options) { 
     @Override 
     protected void onBindViewHolder(UserViewHolder holder, int position, User model) { 
      Log.d("USER", "DATA: "+model.getName()); 
      holder.BindView(position, model); 

     } 
     @Override 
     public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.users_list, parent, false); 

      return new UserViewHolder(view); 
     } 
    }; 
} 

private class UserViewHolder extends RecyclerView.ViewHolder{ 
    TextView info; 
    UserViewHolder(View itemView) { 
     super(itemView); 
     info = (TextView) itemView.findViewById(R.id.info_text); 
    } 

    void BindView(int position, User model){ 

     info.setText(model.getName()+" , "+model.getId()); 
    } 
} 

沒有什麼在我的MainActivity除了啓動上述片段。我不知道我的代碼有什麼問題。它顯示沒有錯誤,當我運行應用程序它不會崩潰。它只是進入recyclerview佈局,我通過放置textview字段來確認(現在已被刪除)來檢查它。我希望你能幫我解決代碼問題。這可能是因爲適配器?幫我弄清楚。我也上傳了我的數據庫的一個片段。 在此先感謝!

回答

0

像下面創建recyclerview:

FirestoreRecyclerAdapter adapter = getFirestoreRecyclerAdapter(); 
//add below line 
mRecyclerView.setHasFixedSize(true); 
mRecyclerView.setAdapter(adapter); 
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));