I am working on an application where i am uploading multiple images with 4 edittexts to the firebase then I am trying to retrieve the data to the recyclerview now my editext's data is retrieving but the data of images is not showing in reyclerview.
my adapter code
#Override
public void onBindViewHolder(#NonNull Viewholder holder, int position)
{
holder.name.setText(datalist.get(position).getName());
holder.email.setText(datalist.get(position).getEmail());
holder.desc.setText(datalist.get(position).getDesc());
holder.book.setText(datalist.get(position).getBook());
Glide.with(context).load(datalist.get(position).getImage())
.into(holder.imageView);
}
#Override
public int getItemCount() {
return datalist.size();
}
public class Viewholder extends RecyclerView.ViewHolder{
TextView name, email, desc, book;
ImageView imageView;
public Viewholder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.text_name);
email = itemView.findViewById(R.id.text_email);
desc = itemView.findViewById(R.id.text_desc);
book = itemView.findViewById(R.id.text_book);
imageView = itemView.findViewById(R.id.imageView3);
}
}
In the comment session you mentioned, getImage() is an array then, How it will load the image you need to pass a particular image in the array?
Let's suppose, If you want to load the first image in an array use the below code -
if(!wishlist_models.get(position).getImage().isEmpty()) {
Glide.with(context)
.load(wishlist_models.get(position).getImage()get(0))
.into(holder.wishlist_image);
}
Related
I want to get the video ID from my database, use the string and load the YouTube video in the cardview. But I'm lost on how to load the video using the holder.
This is my onBindViewHolder in my adapter class.
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
Exercise exercise = list.get(position);
holder.exerciseName.setText(exercise.getExerciseName());
holder.muscleGroupName.setText(exercise.getMuscleGroupName());
holder.equipmentName.setText(exercise.getEquipmentName());
Glide.with(holder.imageView).load(exercise.getImage()).into(holder.imageView);
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
TextView exerciseName, muscleGroupName, equipmentName;
YouTubePlayerView youTubePlayerView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
exerciseName = itemView.findViewById(R.id.exerciseNameInput);
muscleGroupName = itemView.findViewById(R.id.muscleGroupNameInput);
equipmentName = itemView.findViewById(R.id.equipmentNameInput);
imageView = itemView.findViewById(R.id.imageInput);
youTubePlayerView = itemView.findViewById(R.id.video);
}
}
How to set multiple RecycleView to a single adapter in Firebase?
I am creating an app, there are a lot of categories in my app, and creating multiple adapters, models, and RecycleView makes an app complicated. It is hard to make different adapters for multiple RecycleView.
Can I set multiple RecycleView to one single adapter? By using if or else or switch cases how to do this?
Or is there any alternative?
First Model Class
public class FirstModel {
private String Image, Text;
FirstModel() {
}
public FirstModel(String image, String text) {
Image = image;
Text = text;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getText() {
return Text;
}
public void setText(String text) {
Text = text;
}
}
Second Model Class
public class SecondModel {
private String Image, Text;
SecondModel() {
}
public SecondModel(String image, String text) {
Image = image;
Text = text;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getText() {
return Text;
}
public void setText(String text) {
Text = text;
}
Link To One Adapter
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int FirstLayout = 0;
private final int SecondLayout = 1;
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == FirstLayout) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_categories, null);
return new GiftsForHerViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_categories, null);
return new GiftsForHimViewHolder(view);
}
}
#Override
public int getItemViewType(int position) {
????
return super.getItemViewType(position);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 1:
?????
}
}
#Override
public int getItemCount () {
return ???? .size();
}
class FirstViewHolder extends RecyclerView.ViewHolder {
private ImageView Image;
private TextView Text;
public FirstViewHolder(#NonNull View itemView) {
super(itemView);
Image = (ImageView) itemView.findViewById(R.id.Image);
Text = (TextView) itemView.findViewById(R.id.Text);
}
}
class SecondViewHolder extends RecyclerView.ViewHolder {
public SecondViewHolder(#NonNull View itemView) {
super(itemView);
}
}
}
One of the most important ideas in programming is reusability, and I quote, "In computer science and software engineering, reusability is the use of existing assets in some form within the software product development process".
So if the only thing that differs is the model classes and the layout files, then you can create a single adapter class. The key to solving this issue is to use generics. Here is a working example:
Create a Generic Base Adapter?
When you want to present items in a list by using a Recycle View and an adapter, then these items usually have similar UI and are base on the same type of data (e.x. Car, Person, Building, etc.). It's not a good idea to mix different entities in a single adapter. The data sources can be of different lengths and the list items will have different UI. Thus, stick to the idea - one Recycle View with one adapter.
I am trying to display images from my Firebase storage and realtime database to a CardView in my Android application. I have saved the images to the Firebase Storage and as a Url node under the specific user (dog). The CardView displays the dogs registered under each owner and each dog has its own profile picture essentially.. which I would like to display on the UI.
I am using Picasso as it was used within the tutorial I found, and seemed to be working, however it is trying to find 'context' within my DogUserAdapter, which is returning null. I wasn't previously using Context context as I am utilising a callback function instead on this particular CardView. What else can I use here to display the image from the database?
DogUserAdapter
public class DogUserAdapter extends FirebaseRecyclerAdapter<Dog, DogUserAdapter.DogViewHolder> {
private DogCallback dogCallback;
private Context context;
public DogUserAdapter(#NonNull FirebaseRecyclerOptions<Dog> options, #NonNull final DogCallback dogCallback) {
super(options);
this.dogCallback = dogCallback;
this.context = context;
}
#Override
protected void onBindViewHolder(#NonNull DogViewHolder holder, int position, #NonNull Dog model) {
holder.dogName.setText(model.getName());
Picasso.with(context)
.load(model.getImageUrl())
.into(holder.dogImage);
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dogCallback.onCardViewClick(model);
}
});
}
#NonNull
#Override
public DogViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dog, parent, false);
return new DogUserAdapter.DogViewHolder(view);
}
public class DogViewHolder extends RecyclerView.ViewHolder {
TextView dogName;
ImageView dogImage;
CardView cardView;
public DogViewHolder(#NonNull View itemView) {
super(itemView);
dogName = itemView.findViewById(R.id.dogName);
dogImage = itemView.findViewById(R.id.dogImage);
cardView = itemView.findViewById(R.id.card_view_dog);
}
}
public interface DogCallback{
void onCardViewClick(final Dog dog);
}
}
CardView
Firebase Hierarchy
Patrick is right. You do not need context for picasso
Picasso.get().load(url).into(imageView);
I'm trying to change the image in a RecyclerView with a local drawable image. So in the project the RecyclerView is getting data from a local database, when a boolean column gets true, it should change into a tick. My item view looks like this.
And when the column is true, that clock symbol changes to tick. Here is the code which I tried:
My Adapter code:
public class HawbListAdapter extends RecyclerView.Adapter<HawbListAdapter.MyViewHolder> {
private Context context;
private ArrayList<HawbLists> hawbLists;
private DatabaseHelper mDatabaseHelper;
public HawbListAdapter(Context context, ArrayList<HawbLists> hawbLists) {
this.context = context;
this.hawbLists = hawbLists;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.hawb_list, parent, false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.Hcode.setText(hawbLists.get(position).getHawbCode());
holder.name.setText(hawbLists.get(position).getName());
if (hawbLists.get(position).isTick()) {
holder.timeImage.setImageResource(R.drawable.ic_right);
}
//Picasso.get().load(hawbLists.get(position).isTick()).into(holder.timeImage);
//holder.timeImage.setImageResource(R.drawable.ic_right);
}
#Override
public int getItemCount() {
return hawbLists.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView Hcode;
private TextView name;
private ImageView timeImage;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
Hcode = itemView.findViewById(R.id.hawbCode12345);
name = itemView.findViewById(R.id.name_hawb_list);
timeImage = itemView.findViewById(R.id.timeImage);
}
}
}
even when I'm using this
if (hawbLists.get(position).isTick()) {
holder.timeImage.setImageResource(R.drawable.ic_right);
}
and one of the items is true, it is still showing the same clock (wait) symbol, but If I use this:
if (!hawbLists.get(position).isTick()) {
holder.timeImage.setImageResource(R.drawable.ic_right);
}
then all of them regardless of true or false turn into tick mark. Unable to figure out where I'm doing it wrong.
Try this:
if (hawbLists.get(position).isTick()){
holder.timeImage.setImageResource(R.drawable.ic_right);
} else {
holder.timeImage.setImageResource(R.drawable.ic_not_ticked);
}
Do you know about the concept of recycling? Look at your list in the device and see how many rows you have! Now add up one (as swap view) and it's all the views you have. It means no matter how many entries you have (hawbLists.size()), all of them are created/recycled when you scroll. So you have to consider all the possible cases of your item children. Because otherwise a recycled view won't change the view state and likely will show irrelevant behavior.
I am making an app where I have uploaded text files in Firebase Storage and now I want to retrieve those files and display the contents of those txt files in a text view inside a recycler view. I am able to get the uri of those text files but how do I display the contents of those files.
Here is the code I have done so far
public class RecyclerViewAdapter3 extends RecyclerView.Adapter<RecyclerViewAdapter3.ViewHolder> {
Context context3;
List<ImageUploadInfo> MainImageUploadInfoList;
public RecyclerViewAdapter3(Context context, List<ImageUploadInfo> TempList) {
this.MainImageUploadInfoList = TempList;
this.context3 = context;
}
public RecyclerViewAdapter3.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items3, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
#Override
public void onBindViewHolder(final RecyclerViewAdapter3.ViewHolder holder, int position) {
ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);
Uri uri = Uri.parse(UploadInfo.getImageURL());
Log.d("TAGGG" , ""+uri);
holder.userText.setText(uri.toString());
holder.imageNameTextView.setText(UploadInfo.getImageName());
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView imageNameTextView ;
public TextView userText;
public ViewHolder(View itemView) {
super(itemView);
userText = (TextView) itemView.findViewById(R.id.textView13);
imageNameTextView = (TextView) itemView.findViewById(R.id.caption);
}
}
}
In this code I am setting the uri in the textview. How do I set the content of the text file into the textview?
Please help
Change your file to JSON format.
{
"data" : "your text here"
}
And use common approach to download data and parse json (I suggest Retrofit).