简体   繁体   中英

How to get Firebase Data using Spinner Android Studio Java

I Need to get Firebase Realtime Database Data Using Spinner.

Like Spinner - Product Name

After selecting Product Name need to fetch data automatically Rate and Stock Quantity

数据库截图

股票行截图

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

    Context context;
    ArrayList<Model> fill_list;

    public ChrompetSalesFillAdapter(Context context, ArrayList<Model> fill_list){
        this.context = context;
        this.fill_list = fill_list;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ChrompetSalesFillAdapter.MyViewHolder holder, int position) {
        Model model = fill_list.get(position);
        
        holder._stock_qty.setText(model.getProduct_stock_qty());
        holder._product_rate.setText(model.getProduct_rate());
        holder._product_id.setText(model.getProduct_id());

    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView  _product_rate, _stock_qty,_product_name,_product_id;

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

            _product_name = itemView.findViewById(R.id.product_name_sales_fill);
            _product_id =itemView.findViewById(R.id.pro_id_sales_fill);
            _product_rate = itemView.findViewById(R.id.rate_chr_sales_fill);
            _stock_qty = itemView.findViewById(R.id.stock_chr_sales_fill);

        }
    }
}

I think it possible with do like this

  1. Retrieve all product name or you can change it to other data, You can follow this link
  2. Store it to arraylist, you can do it with looping
  3. Create spinner dan make arraylist as spinner value, You can follow this tutorial
  4. Select value of spinner and make new variable which have value of selected spinner value
  5. Make loop and if statement inside it for comparison all product name equal with variable which you already create in last step. Create this in other function and call it inside onItemSelected
  6. If equals, retrieve data you needed and set it to textview or edittext

Let me know if you dont understand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM