Amazonian pet
Amazonian pet

Reputation: 47

Cannot load data from FireBase into Recycler view

I am trying to make a list of Recycler View by loading data from FireBase enter image description here

For that I made MainList.java

public class MainList {
private String imageResourceId;
private String name;

public MainList() {}


public MainList(String imageResourceId, String name){
    this.imageResourceId = imageResourceId;
    this.name = name;
}

public String getImageResourceId() { return imageResourceId; }

public String getName(){ return name;}

}

and MainListAdapter

public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.MainListViewHolder>{

ImageView item_image;

List<MainList> mMainList;
LayoutInflater inflater;

class MainListViewHolder extends RecyclerView.ViewHolder {

    TextView item_name;
    ImageView item_image;

    MainListAdapter mAdapter;

    public MainListViewHolder(View itemView, MainListAdapter adapter) {
        super(itemView);

        item_name = itemView.findViewById(R.id.item_name);
        item_image = itemView.findViewById(R.id.item_image);
        this.mAdapter = adapter;
    }
}

public MainListAdapter(Context context, List<MainList> main_list){
    inflater = LayoutInflater.from(context);
    this.mMainList = main_list;
}

@NonNull
@Override
public MainListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View mItemView = inflater.inflate(R.layout.list_item, parent, false);
    return new MainListViewHolder(mItemView, this);
}

@Override
public void onBindViewHolder(@NonNull MainListViewHolder holder, int position) {
    MainList mainList = mMainList.get(position);
    holder.item_name.setText(mainList.getName());
    Picasso.get().load(mainList.getImageResourceId()).into(item_image);
    //holder.item_image.setImageResource(mainList.getImageResourceId());
}

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

} And I try to use use Picasso inside of OnBindViewHolder for my image

and in MainActivity I try to get my data from FireBase to fill my List

main_comics_List = new ArrayList<>();

    mRef = FirebaseDatabase.getInstance().getReference().child("ComicsData").child("AllComicses");
    mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
                MainList item_of = dataSnapshot1.getValue(MainList.class);
                main_comics_List.add(item_of);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

But when I start my app nothing shows at the place of my Recycler and I get the message

enter image description here

What am I doing wrong? And how do I solve it? I would appreciate you answering me.

Upvotes: 1

Views: 90

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80944

The instance variables inside the class MainList should be the same as the attributes inside the database. Therefore inside the MainList add the following:

private String imageLink;
private String title;

And the setters and getters:

public String getImageLink() { return imageLink; }

public String getTitle(){ return title;}

public void setImageLink(String imageLink) { this.imageLink = imageLink; }

public void setTitle(String title){ this.title = title;}

Also in your database it doesnt seem that you are using name and imageResourceId therefore you can remove them from the class MainList.

Upvotes: 1

Related Questions