Skip to content Skip to sidebar Skip to footer

Accessing Items In Cardview After It Has Been Created And Put Into A Recyclerview

i'm trying to figure out how to modify a certain ImageView or TextView widget in a CardView after i created it and passed it to the main RecyclerView. So my code is as follows : Fi

Solution 1:

The CardView does not care. It is just like a LinearLayout. It does not need any different treatment than a standard RecyclerView.

First, you need to have and modify only one List<MovieDetails> and only one MovieDetailsAdapter for this to work. (You can keep your work in a temporary one if you need).

So, you should make the List public static in the code of the adapter.

Then in the Activity, instead of

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    ...
    MovieDetailsAdapterca=newMovieDetailsAdapter(createList(5));
    ...
}

do

privateMovieDetailsAdapter ca;
...
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    ...
    ca = newMovieDetailsAdapter(createList(5));
    ...
}

Move the createList's generic MovieDetails (using the size) to another method (I'll call it makeGenericMovieDetails()). (In the createList, just replace this in the for loop.)

private MovieDetails makeGenericMovieDetails() {
    MovieDetailsmoviedetails=newMovieDetails();
    // The size of the list, but add one.
    moviedetails.title= "Name" + (adapter.getSize() + 1);

    Bitmapbit= resizeBitmap(getResources(), R.drawable.poster_example, 640, 955);
    moviedetails.imageViewPoster = bit;

    Bitmapbit2= resizeBitmap(getResources(), R.drawable.fanart_example, 800, 450);
   moviedetails.imageViewFanart = bit2; 
return moviedetails;
}

Now when you want to change it, do this (once you resized it):

privatevoidsetMovieDetails(@Nullable Bitmap poster, @Nullable Bitmap fanart, @NullableString title, int id) { 
    MovieDetails details;
    boolean isAlreadyThere = true;
    try {
        details = ca.movieList.get(id);
    } catch (IndexOutOfBoundsException e) {
        isAlreadyThere = false;
        // If we come with a bad ID we crash. No fun.
        details = makeGenericMovieDetails();
    }
    if (poster != null)
        details.imageViewPoster = poster;
    if (fanart != null)
        details.imageViewFanart = fanart;
    if (title != null)
        details.Title = title;
    ca.movieList.set(id, details);

    if (isAlreadyThere) {
        ca.notifyItemChanged(id);
    } else {
        ca.notifyItemInserted(id);
    }
}

The key line here is

ca.notifyItemChanged(id);

which tells the adapter to update that item.

I hope this works, I am doing this from StackExchange's app off hand and it keeps losing the draft, so test first.

Now, when you want to change the pic, just call setMovieDetails(poster, fanart, title, id); and pass null for a value you do not want to change. (Except id).

BTW try to make variables start with a lowercase letter. So instead of Title do something like title or mTitle. It looks better and makes it easier to distinguish.

Post a Comment for "Accessing Items In Cardview After It Has Been Created And Put Into A Recyclerview"