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

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

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

我在我的项目中试过这个

@Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<BlogModel,BlogListViewHolder> adapter = new FirebaseRecyclerAdapter<BlogModel,BlogListViewHolder>(BlogModel.class,BlogListViewHolder.class,mDatabaseReference) {
            public void populateViewHolder(BlogListViewHolder blogListViewHolder,BlogModel blogModel,int position) {
                blogListViewHolder.postTitle.setText(blogModel.getPostTitle());
                blogListViewHolder.postDesc.setText(blogModel.getPostDesc());

            }
        };
        mBlogList.setAdapter(adapter);
    }

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

BlogListViewHolder类

public static class BlogListViewHolder extends RecyclerView.ViewHolder {

        ImageView postImage;
        TextView postTitle;
        TextView postDesc;

        public BlogListViewHolder(View view) {
            super(view);
            postImage = view.findViewById(R.id.blog_image_id);
            postTitle = view.findViewById(R.id.blog_title_id);
            postDesc = view.findViewById(R.id.blog_desc_id);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });

        }

    }

我使用这个依赖项

dependencies {



 compile fileTree(dir: 'libs',include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{
            exclude group: 'com.android.support',module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:26.0.2'
        compile 'com.google.firebase:firebase-auth:11.4.2'
        compile 'com.google.firebase:firebase-database:11.4.2'
        compile 'com.google.firebase:firebase-storage:11.4.2'
        compile 'com.firebaseui:firebase-ui-database:3.1.0'
        compile 'com.firebaseui:firebase-ui-firestore:3.1.0'
        compile 'com.firebaseui:firebase-ui-auth:3.1.0'
        compile 'com.firebaseui:firebase-ui-storage:3.1.0'
        testCompile 'junit:junit:4.12'
        compile 'com.squareup.picasso:picasso:2.5.2'

    }
    apply plugin: 'com.google.gms.google-services'

解决方法

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

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

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

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

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

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

private RecyclerView mPeopleRV;
private FirebaseRecyclerAdapter<PersonModel,PeopleVH> mPeopleRVAdapter;

在我片段的onCreateView()中:

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

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

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

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

DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("People");
    Query personsQuery = personsRef.orderByKey();

    mPeopleRV.hasFixedSize();
    mPeopleRV.setLayoutManager(new LinearLayoutManager(getActivity()));

    FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<PersonModel>().setQuery(personsQuery,PersonModel.class).build();

    mPeopleRVAdapter = new FirebaseRecyclerAdapter<PersonModel,PeopleVH>(personsOptions) {
        @Override
        protected void onBindViewHolder(PeopleVH holder,int position,PersonModel model) {
            holder.setPersonFirstName(model.getFirstName());
            holder.setPersonLastName(model.getLastName());
        }

        @Override
        public PeopleVH onCreateViewHolder(ViewGroup parent,int viewType) {

            View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.single_row_people_rv,parent,false);

            return new PeopleVH(view);
        }
    };

    mPeopleRV.setAdapter(mPeopleRVAdapter);

后来添加

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

@Override
public void onStart() {
    super.onStart();
    mPeopleRVAdapter.startListening();
}

@Override
public void onStop() {
    super.onStop();
    mPeopleRVAdapter.stopListening();
}
原文链接:https://www.f2er.com/android/310571.html

猜你在找的Android相关文章