Recycler View not showing any data

Apoorv Srivastava :

I am trying to implement recycler View in android but after adding it to the app it is not showing anything. I have searched online yet I am not able to figure out what am I doing wrong. Also When I declare dashpercent and dashsubject (mentioned in adapter) in class RecyclerViewHolder extends RecyclerView.ViewHolder the on writing :

@Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        dashboard_recycler_components data = dashlist.get(position);
        String sub = data.Subject;

        holder.dashpercent.setText(data.percentage);
        holder.dashsubject.setText(data.Subject);

    }

then dashpercent and dashsubject give an error that they are not declared.

Adapter


package com.example.attendance;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private ArrayList<dashboard_recycler_components> dashlist;

    RecyclerAdapter(ArrayList<dashboard_recycler_components> a) {
        this.dashlist = a;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from((parent.getContext()));
        View view = inflater.inflate(R.layout.attendace_card, parent, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        dashboard_recycler_components data = dashlist.get(position);
        String sub = data.Subject;
        TextView dashpercent = (TextView) holder.itemView.findViewById(R.id.dashPercent);
        TextView dashsubject = (TextView) holder.itemView.findViewById(R.id.dashSubject);
        ProgressBar dashProgress = (ProgressBar) holder.itemView.findViewById(R.id.recycler_progress);
        dashpercent.setText(data.percentage);
        dashsubject.setText(data.Subject);

    }

    @Override
    public int getItemCount() {
        return dashlist.size();
    }

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {


        RecyclerViewHolder(@NonNull View itemView) {
            super(itemView);
        }

    }
}

dashboadfragment:

package com.example.attendance;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class dashboardfragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ArrayList<dashboard_recycler_components> subList = new ArrayList<>();
        dashboard_recycler_components a = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components b = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components c = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components d = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components e = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components f = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components g = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components h = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components i = new dashboard_recycler_components("DSA",33);
        dashboard_recycler_components j = new dashboard_recycler_components("DSA",33);
        subList.add(a);
        subList.add(b);
        subList.add(c);
        subList.add(d);
        subList.add(e);
        subList.add(f);
        subList.add(g);
        subList.add(h);
        subList.add(i);
        subList.add(j);
        View v = inflater.inflate(R.layout.fragment_dashboard,container,false);
        RecyclerView  recycle = (RecyclerView)v.findViewById(R.id.recycler_view1);
        recycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycle.setAdapter(new RecyclerAdapter(subList));

        return v;
    }
}

dashbord_recycler_componets:

package com.example.attendance;

public class dashboard_recycler_components {
    int percentage;
    String Subject;

    public dashboard_recycler_components(String a , int b){
        this.percentage = b;
        this.Subject = a;
    }

    public int getPercentage() {
        return percentage;
    }

    public String getSubject() {
        return Subject;
    }

    public void setPercentage(int percentage) {
        this.percentage = percentage;
    }

    public void setSubject(String subject) {
        this.Subject = subject;
    }
}

MainActivity:

package com.example.attendance;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ProgressBar;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        BottomNavigationView bottomNav = findViewById(R.id.bottom_nav_bar);
        bottomNav.setOnNavigationItemSelectedListener(navListener);
        getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,
                new homefragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener = new
            BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment frag = null;
                    switch (menuItem.getItemId()) {
                        case R.id.dashboard:
                            frag = new dashboardfragment();
                            break;
                        case R.id.profile:
                            frag = new profilefragment();
                            break;
                        case R.id.home:
                            frag = new homefragment();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,
                            frag).commit();
                    return true;
                }
            };





}

Biscuit :

Your percentage in dashboard_recycler_components is an int if you want to assign your percentage is an int if you want to assign it to your dashpercent you have to first make this int a String such as : dashpercent.setText(String.valueOf(data.percentage));

PS:

You should rename your class object dashboard_recycler_components to dashboardRecyclerComponents and use camelCase instead of snake_case

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=340269&siteId=1