recyclerview未显示片段中的数据

发布于 2025-01-23 11:43:50 字数 10935 浏览 0 评论 0原文

我正在尝试从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2025-01-30 11:43:50


您没有遵循在文本视图中设置文本的方法 文本视图可以具有以下方法的文本:

public void setText(String text);
// or
public void setText(int resId); // the resource id

现在,您正在做的是给它一个int(module.getCoeffility() return int),因此,第二种方法被调用。现在,第二种方法期望r.string.hello_world等...但是,不是0、100、8593等...但是,您给它的第二种情况是我给出的第二种情况。那将会给出这个错误。

解决方案


尝试将其更改为字符串。这些是这样做的3种方法:

  1. 使用字符串类:

      holder.modulecoeffitice.setText(string.valueof(module.getCoeffility()));
     
  2. 使用Integer类:

      holder.modulecoefficien.settext(integer.tostring(module.getCoeffficity()));
     
  3. 使用字符串 contantation:

      holder.modulecoefficed.setText(module.getCoefffority() +“”);
     

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:

public void setText(String text);
// or
public void setText(int resId); // the resource id

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 like R.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:

  1. Use String class:

    holder.moduleCoefficient.setText(String.valueOf(module.getCoefficient()));
    
  2. Use the Integer class:

    holder.moduleCoefficien.setText(Integer.toString(module.getCoefficient()));
    
  3. Use String concantation:

    holder.moduleCoefficient.setText(module.getCoefficient() + "");
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文