简体   繁体   中英

recyclerview doesn't show items

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

You forgot to bind your data into your adapter onBindViewHolder method.

private static class CrimeHolder extends RecyclerView.ViewHolder {
public TextView title, description;

    public CrimeHolder (View view) {
        super(view);

        title = view.findViewById(R.id.Title);
        description = view.findViewById(R.id.Description);
    }

}
    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) {
          holder.title.setText(m_crimes.get(position).getTitle());
          holder.description.setText(m_crimes.get(position).getDescription())


        }

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

having in mind that getTitle and getDescription are the getters for title and description.

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