Only one set of Crime Title and Crime Date appear


#1

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();
        }
    }
}

#2

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


#3

cookey29,

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

Peter


#4

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


#5

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

Peter


#6

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


#7

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?


#8

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>

RecyclerView only shows a single instance of Crime Title and Crime Date
#9

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


#10

Thanks. I had the same layout problem as well.