How can I send the product id by selecting the name of the product on the spinner?

1

How can I make a spinner show the names of x products of a JsonObject that at the time I select, instead of sending me the name that I send the id of that product.

So I put what I want to show

private void getCategori(JSONObject j) throws JSONException {

      Iterator<String> keys = j.keys();
      while (keys.hasNext()) {
          // obtiene el nombre del objeto.
          String key = keys.next();
          Log.i("Parser", "objeto : " + key);
          JSONObject jsonObject1 = j.getJSONObject(key);


          cate.add(jsonObject1.getString(Categorias.TAG_NAME));
          cateid.add(jsonObject1.getString(Categorias.TAG_ID));
          opciones1 = new String[]{cate_name};

          ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(prueba.this, android.R.layout.simple_spinner_item, cate);
          scategory.setAdapter(adapter1);

          scategory.setPrompt("Categorias");
    }

and so I get the position of what is selected

 private String getCatep(int position){
          String c="";
          try {
          JSONArray jsonArray = new JSONArray();
          jsonArray.put(returndatacategoria);

          JSONObject json= jsonArray.getJSONObject(position);

          c= json.getString(Categorias.TAG_ID);
      } catch (JSONException e) {
          e.printStackTrace();
      }

      return c;
  }
    
asked by Maria Isabel 09.08.2018 в 21:57
source

1 answer

0

You need to implement the click on each element to be able to do something with it, below the ArrayAdapter that you think you would be like that.

      ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(prueba.this, android.R.layout.simple_spinner_item, cate);
      scategory.setAdapter(adapter1);

      scategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        String myCateId= cateid.get(i);
                        Log.e("CateId =",""+myCateId);


                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

First, you fill in the spinner with cate which are the names, and then you get the ID with cateid of each element you click

    
answered by 10.08.2018 / 01:35
source