android – 我在FirebaseRecyclerAdapter类中找不到populateViewHolder方法

前端之家收集整理的这篇文章主要介绍了android – 我在FirebaseRecyclerAdapter类中找不到populateViewHolder方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的项目中使用FirebaseRecyclerAdapter,并在之前的Project中使用了此代码
  1. FirebaseRecyclerAdapter<ChatMessage,ChatMessageViewHolder> adapter = new FirebaseRecyclerAdapter<ChatMessage,ChatMessageViewHolder>(ChatMessage.class,android.R.layout.two_line_list_item,ChatMessageViewHolder.class,ref) {
  2. public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder,ChatMessage chatMessage,int position) {
  3. chatMessageViewHolder.nameText.setText(chatMessage.getName());
  4. chatMessageViewHolder.messageText.setText(chatMessage.getMessage());
  5. }
  6. };
  7. recycler.setAdapter(mAdapter);

并且它工作正常,但现在它不再起作用了,FirebaseUi的新版本中是否有任何更新,现在不允许我重用此代码

我在我的项目中试过这个

  1. @Override
  2. protected void onStart() {
  3. super.onStart();
  4. FirebaseRecyclerAdapter<BlogModel,BlogListViewHolder> adapter = new FirebaseRecyclerAdapter<BlogModel,BlogListViewHolder>(BlogModel.class,BlogListViewHolder.class,mDatabaseReference) {
  5. public void populateViewHolder(BlogListViewHolder blogListViewHolder,BlogModel blogModel,int position) {
  6. blogListViewHolder.postTitle.setText(blogModel.getPostTitle());
  7. blogListViewHolder.postDesc.setText(blogModel.getPostDesc());
  8.  
  9. }
  10. };
  11. mBlogList.setAdapter(adapter);
  12. }

和Android Studio提供错误说你必须实现onCreateViewHolder,onBindViewHolder方法.

BlogListViewHolder类

  1. public static class BlogListViewHolder extends RecyclerView.ViewHolder {
  2.  
  3. ImageView postImage;
  4. TextView postTitle;
  5. TextView postDesc;
  6.  
  7. public BlogListViewHolder(View view) {
  8. super(view);
  9. postImage = view.findViewById(R.id.blog_image_id);
  10. postTitle = view.findViewById(R.id.blog_title_id);
  11. postDesc = view.findViewById(R.id.blog_desc_id);
  12. view.setOnClickListener(new View.OnClickListener() {
  13. @Override
  14. public void onClick(View v) {
  15. }
  16. });
  17.  
  18. }
  19.  
  20. }

我使用这个依赖项

  1. dependencies {
  2.  
  3.  
  4.  
  5. compile fileTree(dir: 'libs',include: ['*.jar'])
  6. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
  7. exclude group: 'com.android.support',module: 'support-annotations'
  8. })
  9. compile 'com.android.support:appcompat-v7:26.0.2'
  10. compile 'com.google.firebase:firebase-auth:11.4.2'
  11. compile 'com.google.firebase:firebase-database:11.4.2'
  12. compile 'com.google.firebase:firebase-storage:11.4.2'
  13. compile 'com.firebaseui:firebase-ui-database:3.1.0'
  14. compile 'com.firebaseui:firebase-ui-firestore:3.1.0'
  15. compile 'com.firebaseui:firebase-ui-auth:3.1.0'
  16. compile 'com.firebaseui:firebase-ui-storage:3.1.0'
  17. testCompile 'junit:junit:4.12'
  18. compile 'com.squareup.picasso:picasso:2.5.2'
  19.  
  20. }
  21. apply plugin: 'com.google.gms.google-services'

解决方法

语法已更改.概述电话的doc部分在这里:

https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md

以下是使用Firebase数据库和FirebaseUI版本的应用程序gradle文件

  1. // FirebaseUI for Firebase Realtime Database
  2. compile 'com.firebaseui:firebase-ui-database:3.1.0'
  3. compile 'com.google.firebase:firebase-database:11.4.2'

我定义了标准的ViewHolder和Model类.为了简洁,我会把它们留下来.

我的Fragment类顶部的实例变量:

  1. private RecyclerView mPeopleRV;
  2. private FirebaseRecyclerAdapter<PersonModel,PeopleVH> mPeopleRVAdapter;

在我片段的onCreateView()中:

  1. mPeopleRV = v.findViewById(R.id.peopleRV);

在我的片段的onActivityStarted()中是所有动作的所在.

onCreateViewHolder是行布局膨胀的地方.然后使用bindViewHolder()填充从Firebase获取的模型数据的行.

适配器的构造函数现在采用您根据查询和模型定义的一组选项.

  1. DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("People");
  2. Query personsQuery = personsRef.orderByKey();
  3.  
  4. mPeopleRV.hasFixedSize();
  5. mPeopleRV.setLayoutManager(new LinearLayoutManager(getActivity()));
  6.  
  7. FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<PersonModel>().setQuery(personsQuery,PersonModel.class).build();
  8.  
  9. mPeopleRVAdapter = new FirebaseRecyclerAdapter<PersonModel,PeopleVH>(personsOptions) {
  10. @Override
  11. protected void onBindViewHolder(PeopleVH holder,int position,PersonModel model) {
  12. holder.setPersonFirstName(model.getFirstName());
  13. holder.setPersonLastName(model.getLastName());
  14. }
  15.  
  16. @Override
  17. public PeopleVH onCreateViewHolder(ViewGroup parent,int viewType) {
  18.  
  19. View view = LayoutInflater.from(parent.getContext())
  20. .inflate(R.layout.single_row_people_rv,parent,false);
  21.  
  22. return new PeopleVH(view);
  23. }
  24. };
  25.  
  26. mPeopleRV.setAdapter(mPeopleRVAdapter);

后来添加

不要忘记在onStart()和onStop()中启动和停止适配器侦听器:

  1. @Override
  2. public void onStart() {
  3. super.onStart();
  4. mPeopleRVAdapter.startListening();
  5. }
  6.  
  7. @Override
  8. public void onStop() {
  9. super.onStop();
  10. mPeopleRVAdapter.stopListening();
  11. }

猜你在找的Android相关文章