简体   繁体   中英

recyclerview is not showing data in fragment

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)

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() + "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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