0

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>
9
  • Have you confirmed that pictureList in your getData() method isn't empty? Commented Oct 3, 2018 at 12:37
  • why is it in adapter public void addPictures(ArrayList<UnsplashPic> pictureList) { pictureList.addAll(pictureList); notifyDataSetChanged(); } Commented Oct 3, 2018 at 12:37
  • Use it via activity. You can get many example of it. Commented Oct 3, 2018 at 12:39
  • @TheWanderer I have. In ArrayList<UnsplashPic> pictureList = pictureResponse.getResults(); i get a list of 10 items as expected and that's what i am passing to the Adapter's method addPictures(pictureList) Commented Oct 3, 2018 at 12:54
  • @RashpalSingh You can keep it in your adapter or in your main activity. I personally prefer the option of keeping that method in the adapter class for readability. Commented Oct 3, 2018 at 13:04

1 Answer 1

1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.