Only one set of Crime Title and Crime Date appear

Only one item appears instead of what it should look like in figure 8.10

public class CrimeListFragment extends Fragment {

private RecyclerView mCrimeRecyclerView;
private CrimeAdapter mAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_crime_list, container, false);

    mCrimeRecyclerView = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
    mCrimeRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    updateUI();

    return view;
}

private void updateUI() {
    CrimeLab crimeLab = CrimeLab.get(getActivity());
    List<Crime> crimes = crimeLab.getCrimes();

    mAdapter = new CrimeAdapter(crimes);
    mCrimeRecyclerView.setAdapter(mAdapter);
}

private class CrimeHolder extends RecyclerView.ViewHolder {
        public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_crime, parent, false));
        }
    }

    private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
        private List<Crime> mCrimes;

        public CrimeAdapter(List<Crime> crimes) {
            mCrimes = crimes;
        }

        @Override
        public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

            return new CrimeHolder(layoutInflater, parent);
        }

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

        }

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

Fixed it! I made an error in the xml files.

cookey29,

I have the same issue. Would you post the xml files?
Thanks.

Peter

Hi Peter,

I seem to have forgotten which xml file I had the error with. However I know it was an error with using wrap_content instead of match_parent.

Hopefully this helps.

Matt

I chnaged one of xml files (list_item_crime.xml) as you suggested and it worked. Thaks a lot.

Peter

Thanks for posting this here, I had the same exact problem.

My wrap_content and match_parent seem to be okay but I still have this problem. Can somebody please post how their list_item_crime.xml looks like?

I had the exact same bug, and yes, the problem was in list_item_crime.xml.

In the LinearLayout I had android:layout_height="match_parent" (which is what the new layout creation wizard will put there) but it should be android:layout_height="wrap_content" (otherwise it will fill the screen).

Full listing:

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

    <TextView
        android:id="@+id/crime_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Crime Title"/>

    <TextView
        android:id="@+id/crime_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Crime Date"/>

</LinearLayout>
1 Like

Thank you very much. The problem was in the layout_height of the LinearLayout which should be wrap_content instead of match_parent.

1 Like

Thanks. I had the same layout problem as well.

Thank you sir.There was big white space between two list items. Once I changed layout_height to “wrap_content” I fixed my problem.

Thank you for taking your time to post this XML file. It fixed my problem!!

1 Like