CommonSenseCode
CommonSenseCode

Reputation: 25409

RecyclerView in Android not working without any error messages

I have followed different tutorials and official documentation trying to implement RecyclerView but it's not working properly and I get no error messages I hope someone can see what I'm missing:

my dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.android.support:recyclerview-v7:22.2.0'
    compile 'com.squareup.picasso:picasso:2.5.2'
}

Activity that uses the RecyclerView, I'm using the library butterknife for declaring the views and I already made sure the array Film[] filmography is populated:

public class ActorFilmographyActivity extends AppCompatActivity {

    public static final String TAG = ActorFilmographyActivity.class.getSimpleName();

    @Bind(R.id.actorNameLabel) TextView actorLabel;

    @Bind(R.id.actorProfileImage) ImageView profileImage;

    @Bind(R.id.recyclerView) RecyclerView filmsRecyclerView;

    private Film[] filmography;
    private FilmographyAdapter adapter;

    private Actor actor;

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

        ButterKnife.bind(this);

        Intent intent = getIntent();
        if (intent != null) {
            actor = (Actor) intent.getSerializableExtra("actor");
            actorLabel.setText(actor.getName().toString());
            loadActorProfilePicture();
            getFilmographyBasedOn(actor.getId());
        }

        adapter = new FilmographyAdapter(this, filmography);
        filmsRecyclerView.setAdapter(adapter);

        RecyclerView.LayoutManager layoutManager= new LinearLayoutManager(this);

        filmsRecyclerView.setLayoutManager(layoutManager);

        filmsRecyclerView.setHasFixedSize(true);//TODO chequear si sirve con esto

    }

This is my FilmographyAdapter class:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cooervo.filmography.R;
import com.cooervo.filmography.models.Film;


/**
 * Create the basic adapter extending from RecyclerView.Adapter
 * note that we specify the custom ViewHolder which gives us access to our
 */
public class FilmographyAdapter extends  RecyclerView.Adapter<FilmographyAdapter.ViewHolder>{

    private Film[] films;
    private Context context;

    public FilmographyAdapter(Context ctx, Film[] filmsArray){
        context = ctx;
        films = filmsArray;
    }

    /**
     * Provides a direct reference to each of the views within a data item
     * Used to cache the views within the item layout for fast access
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView filmTitle;

        public ViewHolder(View itemView){
            super(itemView);
            filmTitle = (TextView) itemView.findViewById(R.id.filmTitleLabel);
        }
    }

    //This method inflates a layout from XML and returning the holder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        // Inflate the custom layout
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.filmography_list_item,
                        parent,
                        false);

        return new ViewHolder(view);
    }

    // Populates data into the item through holder
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Film film = films[position];

        holder.filmTitle.setText(film.getTitle());
    }

    @Override
    public int getItemCount() {
        return 0;
    }

}

And this is the filmography_list_item.xml used as custom layout of the RecyclerView:

<?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:paddingBottom="8dp"
              android:paddingTop="8dp"
              tools:background="#10000000"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="100">


        <ImageView
            android:id="@+id/filmPosterImage"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:src="@mipmap/ic_launcher"
            android:layout_weight="30"/>

        <TextView
            android:id="@+id/filmTitleLabel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:layout_weight="30"
            android:textColor="#ff000000"
            android:textStyle="bold"
            android:gravity="center_vertical"
            android:textSize="25sp"/>

    </LinearLayout>

</LinearLayout>

I can't see what is wrong, no error messages nothing???

Upvotes: 2

Views: 5851

Answers (1)

Rodrigo Henriques
Rodrigo Henriques

Reputation: 1812

You forgot to put this.

@Override
public int getItemCount() {
    return films.length;
}

Upvotes: 7

Related Questions