recyclerview未显示片段中的数据
我正在尝试从firebase获取一些数据,并在片段中显示在回收器视图中 因此,我确实创建了一个可以做到这一点的methode:
private void getModules(View view)
{
CollectionReference ref = db.collection(level).document(major).collection("Modules");
ref.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots)
{
for (DocumentSnapshot documentSnapshot:queryDocumentSnapshots.getDocuments()) {
Module module = documentSnapshot.toObject(Module.class);
modulesList.add(module);
}
ModulesAdapter adapter = new ModulesAdapter(modulesList);
modulesRecyclerView.setHasFixedSize(true);
modulesRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
modulesRecyclerView.setAdapter(adapter);
}
});
}
可靠地重新列出数据,它在数组列表中,但它挂在这些代码中:
ModulesAdapter adapter = new ModulesAdapter(modulesList);
modulesRecyclerView.setHasFixedSize(true);
modulesRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
modulesRecyclerView.setAdapter(adapter);
模块类是:
public class Module {
private String name;
private String responsable;
int coefficient;
Uri imageUri;
String imageUrl;
public Module() {
}
public Module(String name, String responsable, int coefficient , Uri image) {
this.name = name;
this.responsable = responsable;
this.imageUri = image;
this.coefficient = coefficient;
}
public Module(String name, String responsable, int coefficient) {
this.name = name;
this.responsable = responsable;
this.coefficient = coefficient;
this.imageUri= Uri.parse("android.resource://com.aymenmellah.edisco/" + R.drawable.ali);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getResponsable() {
return responsable;
}
public void setResponsable(String responsable) {
this.responsable = responsable;
}
public int getCoefficient() {
return coefficient;
}
public void setCoefficient(int coefficient) {
this.coefficient = coefficient;
}
@Exclude
public Uri getImageUri() {
return imageUri;
}
public void setImageUri(Uri imageUri) {
this.imageUri = imageUri;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
回收器视图适配器是:
private List<Module> modulesArrayList;
public ModulesAdapter(List<Module> modulesArrayList) {
this.modulesArrayList = modulesArrayList;
}
@NonNull
@Override
public ModulesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ModulesViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.module_item,parent,false));
}
@Override
public void onBindViewHolder(@NonNull ModulesViewHolder holder, int position) {
Module module = modulesArrayList.get(position);
holder.moduleName.setText(module.getName());
holder.moduleResponsable.setText(module.getResponsable());
holder.moduleCoefficient.setText(module.getCoefficient());
//ADD HERE IMAGE
}
@Override
public int getItemCount() {
return modulesArrayList.size();
}
public class ModulesViewHolder extends RecyclerView.ViewHolder {
TextView moduleName,moduleResponsable,moduleCoefficient,moduleImage;
public ModulesViewHolder(@NonNull View itemView) {
super(itemView);
moduleName = itemView.findViewById(R.id.module_name);
moduleResponsable = itemView.findViewById(R.id.responsable_name);
moduleCoefficient = itemView.findViewById(R.id.coefficient_module_item);
//moduleImage = itemView.findViewById(R.id.module_image_item);
}
}
}`
,这是OnCreateview Methode:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_modules, container, false);
modulesList = new ArrayList<>();
db=FirebaseFirestore.getInstance();
modulesRecyclerView = view.findViewById(R.id.modules_recyclerview);
getModules(view);
return view;
}
上面代码执行应用程序崩溃并返回上一个活动时,我会收到此错误:
android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:375)
at android.widget.TextView.setText(TextView.java:6411)
at com.aymenmellah.edisco.Controller.ModulesAdapter.onBindViewHolder(ModulesAdapter.java:38)
at com.aymenmellah.edisco.Controller.ModulesAdapter.onBindViewHolder(ModulesAdapter.java:18)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at androidx.recyclerview.widget.RecyclerView$LayoutManager.layoutDecoratedWithMargins(RecyclerView.java:9587)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1685)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1845)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
2022-04-23 17:46:14.854 26120-26120/com.aymenmellah.edisco E/AndroidRuntime: at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1845)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:842)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3565)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2988)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2051)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8456)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1065)
at android.view.Choreographer.doCallbacks(Choreographer.java:889)
at android.view.Choreographer.doFrame(Choreographer.java:816)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1050)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7830)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1040)
I'm trying to get some data from firebase and display it in a recycler view in fragment
so i did created a methode that do that :
private void getModules(View view)
{
CollectionReference ref = db.collection(level).document(major).collection("Modules");
ref.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots)
{
for (DocumentSnapshot documentSnapshot:queryDocumentSnapshots.getDocuments()) {
Module module = documentSnapshot.toObject(Module.class);
modulesList.add(module);
}
ModulesAdapter adapter = new ModulesAdapter(modulesList);
modulesRecyclerView.setHasFixedSize(true);
modulesRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
modulesRecyclerView.setAdapter(adapter);
}
});
}
the data retreived succefully and it is in the array list but it hink the problem is in these lines of code :
ModulesAdapter adapter = new ModulesAdapter(modulesList);
modulesRecyclerView.setHasFixedSize(true);
modulesRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
modulesRecyclerView.setAdapter(adapter);
the module class is :
public class Module {
private String name;
private String responsable;
int coefficient;
Uri imageUri;
String imageUrl;
public Module() {
}
public Module(String name, String responsable, int coefficient , Uri image) {
this.name = name;
this.responsable = responsable;
this.imageUri = image;
this.coefficient = coefficient;
}
public Module(String name, String responsable, int coefficient) {
this.name = name;
this.responsable = responsable;
this.coefficient = coefficient;
this.imageUri= Uri.parse("android.resource://com.aymenmellah.edisco/" + R.drawable.ali);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getResponsable() {
return responsable;
}
public void setResponsable(String responsable) {
this.responsable = responsable;
}
public int getCoefficient() {
return coefficient;
}
public void setCoefficient(int coefficient) {
this.coefficient = coefficient;
}
@Exclude
public Uri getImageUri() {
return imageUri;
}
public void setImageUri(Uri imageUri) {
this.imageUri = imageUri;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
the recycler view adapter is :
private List<Module> modulesArrayList;
public ModulesAdapter(List<Module> modulesArrayList) {
this.modulesArrayList = modulesArrayList;
}
@NonNull
@Override
public ModulesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ModulesViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.module_item,parent,false));
}
@Override
public void onBindViewHolder(@NonNull ModulesViewHolder holder, int position) {
Module module = modulesArrayList.get(position);
holder.moduleName.setText(module.getName());
holder.moduleResponsable.setText(module.getResponsable());
holder.moduleCoefficient.setText(module.getCoefficient());
//ADD HERE IMAGE
}
@Override
public int getItemCount() {
return modulesArrayList.size();
}
public class ModulesViewHolder extends RecyclerView.ViewHolder {
TextView moduleName,moduleResponsable,moduleCoefficient,moduleImage;
public ModulesViewHolder(@NonNull View itemView) {
super(itemView);
moduleName = itemView.findViewById(R.id.module_name);
moduleResponsable = itemView.findViewById(R.id.responsable_name);
moduleCoefficient = itemView.findViewById(R.id.coefficient_module_item);
//moduleImage = itemView.findViewById(R.id.module_image_item);
}
}
}`
and this is the onCreateView Methode :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_modules, container, false);
modulesList = new ArrayList<>();
db=FirebaseFirestore.getInstance();
modulesRecyclerView = view.findViewById(R.id.modules_recyclerview);
getModules(view);
return view;
}
when the above codes executed the app crashes and return to the previous activity and i get this error :
android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:375)
at android.widget.TextView.setText(TextView.java:6411)
at com.aymenmellah.edisco.Controller.ModulesAdapter.onBindViewHolder(ModulesAdapter.java:38)
at com.aymenmellah.edisco.Controller.ModulesAdapter.onBindViewHolder(ModulesAdapter.java:18)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at androidx.recyclerview.widget.RecyclerView$LayoutManager.layoutDecoratedWithMargins(RecyclerView.java:9587)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1685)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at androidx.viewpager2.widget.ViewPager2.onLayout(ViewPager2.java:527)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1845)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
2022-04-23 17:46:14.854 26120-26120/com.aymenmellah.edisco E/AndroidRuntime: at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1845)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:842)
at android.view.View.layout(View.java:22344)
at android.view.ViewGroup.layout(ViewGroup.java:6445)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3565)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2988)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2051)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8456)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1065)
at android.view.Choreographer.doCallbacks(Choreographer.java:889)
at android.view.Choreographer.doFrame(Choreographer.java:816)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1050)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7830)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1040)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
。
您没有遵循在文本视图中设置文本的方法 文本视图可以具有以下方法的文本:
现在,您正在做的是给它一个int(
module.getCoeffility()
return int),因此,第二种方法被调用。现在,第二种方法期望r.string.hello_world
等...但是,不是0、100、8593等...但是,您给它的第二种情况是我给出的第二种情况。那将会给出这个错误。解决方案
尝试将其更改为字符串。这些是这样做的3种方法:
使用
字符串
类:使用
Integer
类:使用
字符串
contantation:The issue
You are not following the method to set the text in a text view. A text view can have its text with these methods:
Now, what you are doing is giving it an int(
module.getCoefficient()
return int) because of which, the second method is called. Now, the second method expects something likeR.string.hello_world
and etc... But, not 0, 100, 8593 and etc... But, you are giving it the second case I gave. That will give that error.The solution
Try to change that to a string. These are the 3 methods to do so:
Use
String
class:Use the
Integer
class:Use
String
concantation: