gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:53 24 4
gpt4 key购买 nike

我想在我的项目中使用 FirebaseRecyclerAdapter 并且我在之前的项目中使用了这段代码

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, android.R.layout.two_line_list_item, 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'

最佳答案

语法已经改变。概述调用的文档部分位于此处:

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;

在我的 fragment 的 onCreateView() 中:

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

在我的 Fragment 的 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();
}

关于android - 我在 FirebaseRecyclerAdapter 类中找不到 populateViewHolder 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46859983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com