Jose Garcia
Jose Garcia

Reputation: 158

RecyclerView does not show list of objects

I tried the solutions on the other similar posts that I found here but they did not help me. I hope someone can help.

I get data from Unsplash's API and the ArrayList contains 10 items once I get a response, that's why I think It has to be something with the way the view is inflated.

This is code:

public class MainActivity extends AppCompatActivity {

private static final String TAG = "unsplash";
private final String CLIENT_ID = "...myclientID...";
// testing keyword used on the API to search for pictures 
private final String KEYWORD = "stackOverflow";
private final String urlBase = "https://api.unsplash.com/";
private Retrofit retrofit;

private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;

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

    retrofit = new Retrofit.Builder()
            .baseUrl(urlBase)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);

    GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(layoutManager);

    recyclerViewAdapter = new RecyclerViewAdapter();
    recyclerView.setAdapter(recyclerViewAdapter);

    getData();
}

private void getData() {
    PictureService service = retrofit.create(PictureService.class);
    Call<PictureResponse> pictureResponseCall = service.getPictureList(KEYWORD, CLIENT_ID);

    pictureResponseCall.enqueue(new Callback<PictureResponse>() {
        @Override
        public void onResponse(Call<PictureResponse> call, Response<PictureResponse> response) {
            if (response.isSuccessful()){

                PictureResponse pictureResponse = response.body();
                ArrayList<UnsplashPic> pictureList = pictureResponse.getResults();

                recyclerViewAdapter.addPictures(pictureList);

            }else{
                Log.e (TAG, " onResponse " + response.errorBody());
            }
        }

        @Override
        public void onFailure(Call<PictureResponse> call, Throwable t) {
            Log.e (TAG, " onFailure " + t.getMessage());
        }
    });
}

}

My adapter class:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

private ArrayList<UnsplashPic> pictureList;

public RecyclerViewAdapter (){
    pictureList = new ArrayList<>();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.item_view_holder, viewGroup, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {

    UnsplashPic pic = pictureList.get(i);
    viewHolder.artistName.setText(pic.user.getUsername());
    viewHolder.unsplash.setText("Unsplash");
}

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

public void addPictures(ArrayList<UnsplashPic> pictureList) {

    pictureList.addAll(pictureList);
    notifyDataSetChanged();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private ImageView imageView;
    private TextView artistName;
    private TextView unsplash;

    public ViewHolder (View itemView){
        super(itemView);

        imageView = itemView.findViewById(R.id.imageview);
        artistName = itemView.findViewById(R.id.artist_name);
        unsplash = itemView.findViewById(R.id.unsplash_name);
    }

}

}

item_view_holder.xml

<?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">

<ImageView
    android:id="@+id/imageview"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:background="#ababab"
    android:contentDescription="unsplash picture" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Photo by "/>

    <TextView
        android:id="@+id/artist_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="on "/>

    <TextView
        android:id="@+id/unsplash_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true" />

</LinearLayout>

activity_main.xml

<LinearLayout
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:orientation="vertical"
tools:context=".MainActivity">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Search for pictures"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Search"/>

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recyclerview">

</android.support.v7.widget.RecyclerView>
</LinearLayout>

Upvotes: 0

Views: 88

Answers (1)

Macmist
Macmist

Reputation: 723

Here:

public void addPictures(ArrayList<UnsplashPic> pictureList) {

    pictureList.addAll(pictureList);
    notifyDataSetChanged();
}

you are actually adding the content of the pictureList you send as parameter of the function to that same list, your list from adapter is not updated.

You should do like this instead:

public void addPictures(ArrayList<UnsplashPic> pictureList) {

    this.pictureList.addAll(pictureList);
    notifyDataSetChanged();
}

Upvotes: 1

Related Questions