Search code examples
javaandroidfirebasefirebase-realtime-databasespinner

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

Database Screenshot

Stock Row Screenshot

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);

        }
    }
}

Solution

  • 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

       FirebaseDatabase mDatabase = FirebaseDatabase.getInstance();
       DatabaseReference mDataRef = mDatabase.getReference().child("stock");
       Arraylist list;
       TextView textview1;
       TextView textview2;
       Spinner spinner;
      
       textview1 = findViewById(YourId1);
       textview2 = findViewById(YourId2);
       spinner = findViewById(YourId3);
       list = new ArrayList<>();
      
    2. Store it to arraylist, you can do it with looping

       mDataRef.addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
               list.clear();
               for (DataSnapshot ds : snapshot.getChildren()) {
                   String name  = String.valueOf(ds.child(snapshot.getKey()).child("name"));
                   list.add(name);
               }
           }
      
           @Override
           public void onCancelled(@NonNull DatabaseError error) {
           }
       });
      
    3. Create spinner dan make arraylist as spinner value, You can follow this tutorial

      spinner = findViewById(/*Your Id Here*/);
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(DetailTransactionActivity.this, R.layout.support_simple_spinner_dropdown_item, list);
      spinner.setAdapter(adapter);
      
    4. Select value of spinner and make new variable which have value of selected spinner value

      spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                   @Override
                   public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                       String selectedValue = list.get(position);
                       compare(selectedValue);
                   }
      
                   @Override
                   public void onNothingSelected(AdapterView<?> adapterView) {
      
                   }
               });
      
    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

       public void compare(String selectedValue){
        mDataRef.addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
               for (DataSnapshot ds : snapshot.getChildren()) {
                   /*Next Step*/
               }
           }
      
           @Override
           public void onCancelled(@NonNull DatabaseError error) {
           }
       });
      

    }

    1. If equals, retrieve data you needed and set it to textview or edittext

      for (DataSnapshot ds : snapshot.getChildren()) {
                   if(ds.child(snapshot.getKey()).child("name").equals(selectedValue)){
           String rate = String.valueOf(ds.child(snapshot.getKey()).child("product_rate"));
           String quantity = String.valueOf(ds.child(snapshot.getKey()).child("product_stock_qty"));
           textview1.setText(rate);
           textview2.setText(quantity);
           break;
                   }
               }