繁体   English   中英

recyclerview 未在片段中显示数据

[英]recyclerview is not showing data in fragment

我正在尝试从 firebase 获取一些数据并将其显示在片段的回收站视图中,所以我确实创建了一个这样做的方法:

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);

模块 class 是:

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 方法:

@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)

问题


您没有遵循在文本视图中设置文本的方法。 文本视图可以使用这些方法来获取文本:

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

现在,你正在做的是给它一个 int( module.getCoefficient() return int) 因为它,第二个方法被调用。 现在,第二种方法需要类似R.string.hello_world等的东西……但是,不是 0、100、8593 等等……但是,你给出的是我给出的第二种情况。 那会给出那个错误。

解决方案


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

  1. 使用String class:

     holder.moduleCoefficient.setText(String.valueOf(module.getCoefficient()));
  2. 使用Integer class:

     holder.moduleCoefficien.setText(Integer.toString(module.getCoefficient()));
  3. 使用String连接:

     holder.moduleCoefficient.setText(module.getCoefficient() + "");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM