I am trying to make a list of Recycler View by loading data from FireBase
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
What am I doing wrong? And how do I solve it?
I would appreciate you answering me.
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.
Related
I am trying to retrieve data from firebase but my app is keep crashing. i have tried many solution but nothing is working.
In my app i am trying to retrieve name,image,descriptions,price form my Firebase database.
I have written this flowing code to display data in Card view.
my code:-
Main Activity
public class MainActivity extends AppCompatActivity {
private DatabaseReference ProductsRef;
private RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProductsRef = FirebaseDatabase.getInstance().getReference().child("Products");
recyclerView = findViewById(R.id.recycler_menu);
recyclerView.setHasFixedSize(true);
layoutManager= new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
FirebaseRecyclerOptions<Products> options =
new FirebaseRecyclerOptions.Builder<Products>()
.setQuery(ProductsRef, Products.class)
.build();
FirebaseRecyclerAdapter<Products, ProductViewHolder> adapter =
new FirebaseRecyclerAdapter<Products, ProductViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ProductViewHolder holder, int position, #NonNull Products model) {
holder.textProductName.setText(model.getName());
holder.textProductDescription.setText(model.getDescription());
holder.textProductPrice.setText("Price = ₹"+model.getPrice());
Picasso.get().load(model.getImage()).into(holder.imageView);
}
#NonNull
#Override
public ProductViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_items_layout, parent,false);
ProductViewHolder holder = new ProductViewHolder(view);
return holder;
}
};
recyclerView.setAdapter(adapter);
adapter.startListening();
}
}
Products Activity
public class Products {
private String description, image,name,price ;
public Products() {
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
ProductViewHolder Activity
[enter image description here][1]public class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textProductName, textProductDescription, textProductPrice;
public ImageView imageView;
public ProductViewHolder(#NonNull View itemView) {
super(itemView);
imageView= imageView.findViewById(R.id.product_image);
textProductName = textProductName.findViewById(R.id.product_name);
textProductDescription = textProductDescription.findViewById(R.id.product_description);
textProductPrice = textProductPrice.findViewById(R.id.product_price);
}
#Override
public void onClick(View v) {
}
}
It seems that your assignments at ProductViewHolder are wrong, try this:
public ProductViewHolder(#NonNull View itemView) {
super(itemView);
imageView= itemView.findViewById(R.id.product_image);
textProductName = itemView.findViewById(R.id.product_name);
textProductDescription = itemView.findViewById(R.id.product_description);
textProductPrice = itemView.findViewById(R.id.product_price);}
Hope I helped!
Attempt to invoke virtual method 'android.view.View
android.widget.ImageView.findViewById(int)' on a null object reference
findViewById() is nullable, so it might return a null value.. make sure that the ImageView resource id product_name exists in your R.layout.product_items_layout layout and not in any other layouts in your project.
I need to design the bus seat view in android application and I have seat positions in matrix format. For example
a1.row=4;
a1.column=5;
I only need to set those seats in gridview based on their matrix position. Other spaces will be blank or empty in matrix. After showing these seat view if I choose specific seat then that seat related object will be added in a list of object and if I deselect it will be removed from the list of object. Can anyone help me to find out a solution for this ? Is it possible to implement this using gridview but I will set the item in grid view using its matrix position
Try this code
Activityclass
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv_list);
showSeatList();
}
private void showSeatList()
{
List<SeatModel> seatModels = new ArrayList<>();
seatModels.add(new SeatModel("1","A",false));
seatModels.add(new SeatModel("2","B",false));
seatModels.add(new SeatModel("3","C",false));
seatModels.add(new SeatModel("4","D",false));
seatModels.add(new SeatModel("5","E",false));
seatModels.add(new SeatModel("6","F",false));
seatModels.add(new SeatModel("7","G",false));
seatModels.add(new SeatModel("8","H",false));
seatModels.add(new SeatModel("9","I",false));
seatModels.add(new SeatModel("10","J",false));
SeatSelectionAdapter adapter = new SeatSelectionAdapter(this,seatModels);
recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
recyclerView.setAdapter(adapter);
}
}
Adapter Class
public class SeatSelectionAdapter extends RecyclerView.Adapter<SeatSelectionAdapter.ViewHolder>
{
private List<SeatModel> seatModelList;
private LayoutInflater inflater;
private Context context;
public SeatSelectionAdapter(Context context, List<SeatModel>models)
{
this.seatModelList = models;
inflater = LayoutInflater.from(context);
this.context = context;
}
#NonNull
#Override
public SeatSelectionAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.model_seat,parent,false);
return new SeatSelectionAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SeatSelectionAdapter.ViewHolder holder, int position) {
SeatModel model = seatModelList.get(position);
holder.setData(model);
}
#Override
public int getItemCount() {
return seatModelList.size();
}
class ViewHolder extends RecyclerView.ViewHolder
{
private CardView cardView;
private LinearLayout lnLayout;
private TextView tvSeat;
private ViewHolder(View view) {
super(view);
tvSeat = view.findViewById(R.id.tv_seat);
cardView = view.findViewById(R.id.cv_card);
lnLayout = view.findViewById(R.id.ln_layout);
}
#SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
private void setData(final SeatModel model)
{
tvSeat.setText(model.getSeat());
if(model.isSelect())
lnLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
else lnLayout.setBackgroundColor(context.getResources().getColor(R.color.white));
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(model.isSelect())
{
model.setSelect(false);
Toast.makeText(context,"Seat not selected",Toast.LENGTH_LONG).show();
}
else{
model.setSelect(true);
Toast.makeText(context,"Seat selected",Toast.LENGTH_LONG).show();
}
notifyDataSetChanged();
}
});
}
}
}
Model Class
public class SeatModel
{
private String id,seat;
private boolean isSelect;
public SeatModel(String id, String seat, boolean isSelect) {
this.id = id;
this.seat = seat;
this.isSelect = isSelect;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
}
My RecyclerView not show any data from Firebase. I don't see any errors at logcat so I don't know why. I followed guide from youtube video. But still nothing. I'm using android 8.0 and API 27. Hope Somebody can help.
My Main Activity Code:
public class ViewProduct extends AppCompatActivity {
DatabaseReference ref;
ArrayList<Model> list;
private RecyclerView recyclerView;
private RecyclerView.Adapter viewHolder;
private RecyclerView.LayoutManager layoutManager;
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_product) ;
ref = FirebaseDatabase.getInstance().getReference().child("buddymealplanneruser").
child("Products");
recyclerView = findViewById(R.id.stallproductRecyclerView);
//recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//SEARCH VIEW
searchView = findViewById(R.id.searchProductStall);
//ADAPTER
list = new ArrayList<>();
viewHolder = new ViewHolder(list);
recyclerView.setAdapter(viewHolder);
}
#Override
protected void onRestart() {
super.onRestart();
if (ref!=null)
{
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
//list = new ArrayList<>();
list.clear();
for(DataSnapshot ds : dataSnapshot.getChildren())
{
list.add(ds.getValue(Model.class));
}
//ViewHolder viewHolder = new ViewHolder(list);
//recyclerView.setAdapter(viewHolder);
viewHolder.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ViewProduct.this, databaseError.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
if (searchView !=null)
{
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s)
{
return false;
}
#Override
public boolean onQueryTextChange(String s) {
search(s);
return true;
}
});
}
}
private void search(String str) {
ArrayList<Model> myList = new ArrayList<>();
for(Model object : list)
{
if(object.getProductName().toLowerCase().contains(str.toLowerCase()))
{
myList.add(object);
}
}
ViewHolder viewHolder = new ViewHolder(myList);
recyclerView.setAdapter(viewHolder);
}
}
My Adapter:
public class ViewHolder extends RecyclerView.Adapter<ViewHolder.MyViewHolder> {
ArrayList<Model> list;
public ViewHolder (ArrayList<Model> list)
{
this.list=list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_product, viewGroup,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.productName.setText(list.get(i).getProductName());
myViewHolder.productCalorie.setText(list.get(i).getProductCalorie());
myViewHolder.StallId.setText(list.get(i).getStallId());
myViewHolder.productType.setText(list.get(i).getProductType());
myViewHolder.productServing.setText(list.get(i).getProductServing());
myViewHolder.statusProduct.setText(list.get(i).getStatusProduct());
}
#Override
public int getItemCount()
{
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
TextView productName, productCalorie, StallId;
TextView productType, productServing, statusProduct;
ImageView productImageView;
public MyViewHolder (#NonNull View itemView){
super((itemView));
productName = itemView.findViewById(R.id.productTitle);
productCalorie = itemView.findViewById(R.id.productDescriptionCalorie);
StallId = itemView.findViewById(R.id.productDescriptionStallId);
productType = itemView.findViewById(R.id.productDescriptionType);
productServing=itemView.findViewById(R.id.productDescriptionServing);
statusProduct=itemView.findViewById(R.id.productDescriptionAvailibility);
//productImageView = itemView.findViewById(R.id.productImageView);
}
}
}
My Model :
package com.example.buddymealplannerstall.Child;
import android.view.Display;
public class Model {
//String productName, productImage, productCalorie, StallId, productType, productServing, statusProduct;
private String StallId;
private String productCalorie;
private String productImage;
private String productName;
private String productServing;
private String productType;
private String statusProduct;
public Model (String StallId,
String productCalorie,
String productImage,
String productName,
String productServing,
String productType,
String statusProduct){
this.StallId = StallId;
this.productCalorie = productCalorie;
this.productImage = productImage;
this.productName = productName;
this.productServing = productServing;
this.productType = productType;
this.statusProduct = statusProduct;
}
public Model(){
}
public String getStallId() {
return StallId;
}
public void setStallId(String stallId) {
StallId = stallId;
}
public String getProductCalorie() {
return productCalorie;
}
public void setProductCalorie(String productCalorie) {
this.productCalorie = productCalorie;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage) {
this.productImage = productImage;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductServing() {
return productServing;
}
public void setProductServing(String productServing) {
this.productServing = productServing;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getStatusProduct() {
return statusProduct;
}
public void setStatusProduct(String statusProduct) {
this.statusProduct = statusProduct;
}
}
Screen Shot of my firebase and my apps.
firebase
my app
According to your comment:
yes, buddymealplanner is my project's name
Please note that there is no need to add the name of your project as a child in the reference. So please change the following line of code:
ref = FirebaseDatabase.getInstance().getReference().child("buddymealplanneruser").
child("Products");
Simply to:
ref = FirebaseDatabase.getInstance().getReference().child("Products");
I dont think you are populating your list, you are sending it empty. From what I can see, you only add items to the list when you are restarting the activity. Try either populating it before sendinig the list, or changing the onRestart() for an onStart() or an onResume().
I am currently working on a project that uses firebase. I have problems concerning using firebase. How do I retrieve the images?
public class ViewHolder extends RecyclerView.Adapter<ViewHolder.ImageViewHolder> {
private Context mContext;
private List<Model> mModel;
public ViewHolder(Context context, List<Model> models)
{
mContext=context;
mModel=models;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(mContext).inflate(R.layout.row, viewGroup,false);
return new ImageViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHolder imageViewHolder, int i) {
Model mModelcur = mModel.get(i);
imageViewHolder.img_description.setText(mModelcur.getDescription());
Picasso.get().load(mModelcur.getImage()).into(imageViewHolder.image_view);
}
#Override
public int getItemCount() {
return mModel.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder
{
public TextView img_description;
public ImageView image_view;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
img_description = itemView.findViewById(R.id.rDescription);
image_view = itemView.findViewById(R.id.rImageview);
}
}
}
The code above shows the viewholder while this code is for executing the firebase with the viewholder and the getter and setter which is the model
public class Ordering extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ViewHolder mAdapter;
private DatabaseReference mDatabaseReference;
private List<Model> mModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ordering);
mRecyclerView= findViewById(R.id.recyclerview);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mModel = new ArrayList<>();
mDatabaseReference=FirebaseDatabase.getInstance().getReference("Data");
mDatabaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot:dataSnapshot.getChildren())
{
Model model=postSnapshot.getValue(Model.class);
mModel.add(model);
}
mAdapter=new ViewHolder(Ordering.this, mModel);
mRecyclerView.setAdapter(mAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(Ordering.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
The code works but it only shows this recycler part. I already did the database which looks like this database. How do I show the images which have been stored in the database and show it in the recycler view? I'm still new in using firebase can someone help me, please?
This is my model.class #Alex
public class Model {
String title,image,description; //These must be match in the firebase database
//Constructor
public Model()
{
}
//getter and setter
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
You are getting the following error:
No setter/field for image found on class com.example.firebaseproject.Model
Because you are using in your Model class the fileds with no modifier. To solve this, change the modifier to private:
private String title, image, description;
override fun onStart() {
super.onStart()
mRecyclerView.startListening()
}
override fun onStop() {
super.onStop()
mRecyclerView.stopListening()
}
It's look like the problem is from your image url use picasso 2.71828 and then you can see call back of image processing and you can see what is wrong there
Picasso.get().load(mModelcur.getImage()).into(imageViewHolder.image_view , new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError(Error e) {
Log.i("log" , e.getMessage());
}
});
I have a JSON node wich is displayed in logcat and look like this :
jsonData[{"numarProduse":"4","pretTotal":"1138","data":"11-27-2017","numeVanzator":"Clau","numarClient":0,"detaliiCos":{"metodaPlata":"CASH"}}]
But I want from this JSON response to display in another activity just 3 of them "numarProduse":"4", "pretTotal":"1138", "numarClient":0.
I tried something else, to upload data to Firebase and then retrive data from Firebase.
Here is my RecyclerView Adapter:
public class CardArrayAdapter extends RecyclerView.Adapter<CardArrayAdapter.ViewHolder> {
private List<Card> listdata;
public CardArrayAdapter(List<Card> listdata) {
this.listdata = listdata;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Card vanzatorProduse = listdata.get(position);
holder.txtViewNumarCumparator.setText(vanzatorProduse.getProdusId());
holder.listaProduse.setText(vanzatorProduse.getQty());
holder.sumaProduse.setText(vanzatorProduse.getSubTotal());
}
#Override
public int getItemCount() {
return listdata.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtViewNumarCumparator;
public TextView listaProduse;
public TextView sumaProduse;
public ViewHolder(View itemView) {
super(itemView);
txtViewNumarCumparator = (TextView) itemView.findViewById(R.id.txtViewNumarCumparator);
listaProduse = (TextView) itemView.findViewById(R.id.listaProduse);
sumaProduse = (TextView) itemView.findViewById(R.id.sumaProduse);
}
}
}
I made my layout for CardView, and object class with getters and setters.
public class Card implements Serializable {
public String produsId;
public String subTotal;
public String qty;
public Card(){}
public Card(String produsId, String subTotal, String qty) {
this.produsId = produsId;
this.subTotal = subTotal;
this.qty = qty;
}
public String getProdusId() {
return produsId;
}
public void setProdusId(String produsId) {
this.produsId = produsId;
}
public String getSubTotal() {
return subTotal;
}
public void setSubTotal(String subTotal) {
this.subTotal = subTotal;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
}
And here is where I tried to retrive data from Firebase:
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
VanzatorProduse produse = snapshot.getValue(VanzatorProduse.class);
Card listdata = new Card();
String id = produse.getProdusId();
String qty = String.valueOf(produse.getQty());
String suma = String.valueOf(produse.getSubTotal());
listdata.setProdusId(id);
listdata.setQty(qty);
listdata.setSubTotal(suma);
list.add(listdata);
}
CardArrayAdapter recycler = new CardArrayAdapter(list);
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(VanzatorActivity.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}
But is not working, I get two columns where is 0 displayed. I have no idea what to do next, here I'm blocked.
You can create a bean class with setter getter for your List Items and the bean class should implement parcelable so that you can send the bean object to other activity. For recycler View you use adapter of same bean type and set that adapter once data is loaded from server.