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 :
First the class that holds the items :
public class MovieDetails {
protected static String title;
protected static Bitmap imageViewPoster;
protected static Bitmap imageViewFanart;}
Now i have the RecyclerView Adapter that also holds the ViewHolder inner class like this :
public class MovieDetailsAdapter extends RecyclerView.Adapter<MovieDetailsAdapter.MovieViewHolder> {
private List<MovieDetails> movieList;
public MovieDetailsAdapter(List<MovieDetails> movietList) {
this.movieList = movietList;
}
@Override
public int getItemCount() {
return movieList.size();
}
@Override
public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_layout_movies, viewGroup, false);
return new MovieViewHolder(itemView);
}
@Override
public void onBindViewHolder(MovieViewHolder movieViewHolder, int i) {
MovieDetails md = movieList.get(i);
movieViewHolder.vTitle.setText(md.title);
movieViewHolder.vPoster.setImageBitmap(md.imageViewPoster);
movieViewHolder.vFanart.setImageBitmap(md.imageViewFanart);
}
public static class MovieViewHolder extends RecyclerView.ViewHolder {
protected TextView vTitle;
protected ImageView vPoster;
protected ImageView vFanart;
public MovieViewHolder(View v)
{
super(v);
vTitle = (TextView)v.findViewById(R.id.title);
vPoster = (ImageView) v.findViewById(R.id.imageViewPoster);
vFanart = (ImageView) v.findViewById(R.id.imageViewFanart);
}
}}
And now my main Activity class, where i am setting the layout and view and creating the MovieList array of objects.
public class MoviesListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movies_list);
RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
MovieDetailsAdapter ca = new MovieDetailsAdapter(createList(5));
recList.setAdapter(ca);
}
private List<MovieDetails> createList(int size){
List<MovieDetails> moviedetailsList = new ArrayList<MovieDetails>();
for (int i=1; i <= size; i++) {
MovieDetails moviedetails = new MovieDetails();
moviedetails.title= "Name" +i;
Bitmap bit = resizeBitmap(getResources(), R.drawable.poster_example, 640, 955);
moviedetails.imageViewPoster = bit;
Bitmap bit2 = resizeBitmap(getResources(), R.drawable.fanart_example, 800, 450);
moviedetails.imageViewFanart = bit2;
moviedetailsList.add(moviedetails);
}
return moviedetailsList;
}
resizeBitmap is another function i use for resizing the images, irrelevant to the question, i edited it out.
Now, one i've created this CardView list, is there any way to modify the ImageView and TextViews inside it from the main Activity class ? I want to to this since i`m using AsyncTask to retrieve the new images i want to replace, so i first have to initialize the Bitmaps with some "default" images, and then later one modify them from the results of the AsyncTask used to retrieve them from the server.
Searched around google, found dozens of tutorials around CardViews but nothing regarding this, any help is appreciated, thanks.