recyclerview doesn’t show items

  android, android-recyclerview, java

Am coming from big nerd ranch book
the recycler view does not show any items

but the code seems the same

This is my code

a class where the adapter and view holder be

 public class CrimeListFragment extends Fragment {
private RecyclerView crime_recyclerView;
        private  CrimeAdapter crimeAdapter ;


    @Override
    public View onCreateView( LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_crime_list, container, false);
        crime_recyclerView=view.findViewById(R.id.crime_recycler_View);
        crime_recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        crime_recyclerView.smoothScrollToPosition(0);

        updateUI();
        return view;
    }
    private void updateUI(){
      CrimeLab crimeLab = CrimeLab.get(getActivity());
      List<Crime> crimes =crimeLab.getcrimes();
      crimeAdapter=new CrimeAdapter(crimes);
      crime_recyclerView.setAdapter(crimeAdapter);
    }
    private static class CrimeHolder extends RecyclerView.ViewHolder {

        public CrimeHolder ( LayoutInflater inflater,ViewGroup parent) {
            super(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_crime,parent,false));
        }

    }
        private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{
          private final List<Crime> m_crimes;

            public CrimeAdapter(List<Crime>  crimes) {
                this.m_crimes = crimes;
            }
            @Override
            public CrimeHolder onCreateViewHolder( ViewGroup parent, int viewType) {
                LayoutInflater inflater=  LayoutInflater.from(getActivity());
                return new CrimeHolder(inflater,parent);
            }

            @Override
            public void onBindViewHolder( CrimeHolder holder, int position) {




            }

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

}

This holder of Two TextView XML


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

  <TextView
      android:id="@+id/Title"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      tools:text="@tools:sample/lorem"
      android:textSize="19sp"/>

  <TextView
      android:id="@+id/Description"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      tools:text="@tools:sample/lorem" />
</LinearLayout>

This is a class where create an array list and fill it

public class CrimeLab {

    private static CrimeLab sCrimeLab;
    private List <Crime> mcrimes;
    private CrimeLab(Context context) {
    mcrimes =new ArrayList<>();
    for (int i=0 ;i<100;i++){
        Crime crime = new Crime();
        crime.setTitle("Crime #"+i);
        crime.setSolved(i%2==0);
        mcrimes.add(crime);
    }
    }
    public static CrimeLab get(Context context){
        if(sCrimeLab==null){
            sCrimeLab=new CrimeLab(context);
        }
        return sCrimeLab;
    }
    public List<Crime> getcrimes()
    {
        return  mcrimes;
    }

    /////////////////////////////////:
    public Crime getCrime(UUID id){
       for(Crime crime :mcrimes){
           if(crime.getId().equals(id)){
               return crime;
           }
        }
       return null;
    }

}

This where the items should appear

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Fragment_container" />

there are a lot of classes but this is what you need

Source: Android Questions

LEAVE A COMMENT