user2320239 user2320239 - 4 months ago 18
Android Question

RecycleView not showing TextViews

I'm following a tutorial on android programming and I've run into a problem. I've a recycle view, populated with an array list, but when I run the app the recycle view is there since I can see the shadows at the top and bottom, but it's not populated with views.

CrimeLab.java

package com.example.moore.criminalintent;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* Created by moore on 13/07/2016.
*/
public class CrimeLab {
private static CrimeLab sCrimeLab;
private List<Crime> mCrimes;

public static CrimeLab get(Context context){
if (sCrimeLab == null){
sCrimeLab = new CrimeLab(context);
}
return sCrimeLab;
}

private CrimeLab(Context context){
mCrimes = new ArrayList<>();
for (int i = 100; i<100; i++){
Crime crime = new Crime();
crime.setTitle("Crime :" + i);
crime.setSovled(i%2 == 0);
mCrimes.add(crime);
}
}

public List<Crime> getCrimes(){
return mCrimes;
}

public Crime getCrime(UUID id){
for (Crime crime : mCrimes){
if (crime.getID().equals(id)){
return crime;
}
}
return null;
}

}


CrimeListActivity.java

public class CrimeListActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
return new CrimeListFragment();
}
}


CrimeListFragment.java

package com.example.moore.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
* Created by moore on 14/07/2016.
*/
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;
}

public 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 TextView mTitleTextView;

public CrimeHolder(View itemView){
super(itemView);
mTitleTextView = (TextView) itemView;
}
}

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());
View view = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
return new CrimeHolder(view);
}

@Override
public void onBindViewHolder(CrimeHolder holder, int position) {
Crime crime = mCrimes.get(position);
holder.mTitleTextView.setText(crime.getTitle());
}

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


}


SingleFragmentActivity.java

package com.example.moore.criminalintent;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

/**
* Created by moore on 14/07/2016.
*/
public abstract class SingleFragmentActivity extends FragmentActivity {
protected abstract Fragment createFragment();

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

FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);

if(fragment == null){
fragment = createFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
}
}
}


activity_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.moore.criminalintent.CrimeActivity">


<include layout="@layout/content_fragment" />


</android.support.design.widget.CoordinatorLayout>


content_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.moore.criminalintent.CrimeActivity"
tools:showIn="@layout/activity_fragment">

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>


fragment_crime.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/Crime_title_label"
style="?android:listSeparatorTextViewStyle"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/crime_title"
android:hint="@string/crime_title_hint"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_detail_label"
style="?android:listSeparatorTextViewStyle"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/crime_date"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/crime_solved"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="@string/crime_solved_label"/>


</LinearLayout>


fragment_crime_list.xml

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


Any help with what is wrong with my code would be fantastic.

Answer

The problem is with this method

private CrimeLab(Context context){
    mCrimes = new ArrayList<>();
    for (int i = 100; i<100; i++){ // here is the problem
        Crime crime = new Crime();
        crime.setTitle("Crime :" + i);
        crime.setSovled(i%2 == 0);
        mCrimes.add(crime);
    }
}

change it to

private CrimeLab(Context context) {
    mCrimes = new ArrayList<>();
    for (int i = 100; i > 1; i--) {
        Crime crime = new Crime();
        crime.setTitle("Crime :" + i);
        crime.setSovled(i % 2 == 0);
        mCrimes.add(crime);
    }
}