2 years ago

#60147

test-img

Gurdeep Singh

how to use Spinner in Recyclerview using firebase database?

i got the Product data from firebase database and bind it into recyclerAdapter i have a model class named Product, i want to add a spinner in my Recyclerview both of them should get data from firebase database, please help This is my 'RecyclerviewAdapter' i want to add a 'Spinner' in it which gets all the values from 'Database', please help!!

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder> {

Context context;
ArrayList<Product> list;

public ProductAdapter(Context context, ArrayList<Product> list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.row_product,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    Product product = list.get(position);
    holder.xName.setText(product.getName());
    holder.xOldPrice.setText(product.getOldPrice());
    holder.xNewPrice.setText(product.getNewPrice());
    Glide.with(holder.xImage).load(product.getImage()).into(holder.xImage);

}

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

public static class MyViewHolder extends RecyclerView.ViewHolder{

    TextView xName, xOldPrice, xNewPrice;
    ImageView xImage;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        xImage = itemView.findViewById(R.id.item_Product_Image);
        xName = itemView.findViewById(R.id.product_name);
        xNewPrice = itemView.findViewById(R.id.new_price);
        xOldPrice = itemView.findViewById(R.id.old_price);
    }
}

}

this is my model class, which name is Product.

package com.example.otpverificationusingfirebase;

import android.content.Context;

public  class Product {
Context mContext;
String image, name, newPrice, oldPrice;

public Product() {
}

public Product(Context mContext, String image, String name, String newPrice, String oldPrice) 
{

    this.mContext = mContext;
    this.image = image;
    this.name = name;
    this.newPrice = newPrice;
    this.oldPrice = oldPrice;
    
}

public Context getmContext() {
    return mContext;
}

public void setmContext(Context mContext) {
    this.mContext = mContext;
}

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 getNewPrice() {
    return newPrice;
}

public void setNewPrice(String newPrice) {
    this.newPrice = newPrice;
}

public String getOldPrice() {
    return oldPrice;
}

public void setOldPrice(String oldPrice) {
    this.oldPrice = oldPrice;
}
}

this is my main activity, my recyclerview is on main activity.

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
DatabaseReference database;
ProductAdapter productAdapter;
ArrayList<Product> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

recyclerView = findViewById(R.id.idRecyclerView);
    database = FirebaseDatabase.getInstance().getReference("Product List");
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


    list = new ArrayList<>();
    productAdapter = new ProductAdapter(this,list);
    recyclerView.setAdapter(productAdapter);

    database.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for (DataSnapshot dataSnapshot : snapshot.getChildren()){

                Product product = dataSnapshot.getValue(Product.class);
                list.add(product);

            }

            productAdapter.notifyDataSetChanged();

        }

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

        }
    });

android

firebase-realtime-database

android-recyclerview

spinner

0 Answers

Your Answer

Accepted video resources