2

I'm trying to make a form to make a selection, but I have a spinner with more than one possibility, so the spinners are automatically generated. Here is my script to make a spinner in the middle of json data loop, and the spinners are underneath textviews loop :

StringRequest stringRequest = new StringRequest(Request.Method.GET, HttpUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    progressDialog.dismiss();
                    try {
                        JSONObject obj = new JSONObject(response);
                        JSONArray gejalaArray = obj.getJSONArray("result");
                        for (int i = 0; i < gejalaArray.length(); i++) {
                            JSONObject gejalaObject = gejalaArray.getJSONObject(i);
                            final LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.llMain);
                            final TextView textView = new TextView(getApplicationContext());
                            textView.setText(gejalaObject.getString("gejala"));
                            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
                                    (LinearLayout.LayoutParams.MATCH_PARENT,
                                            LinearLayout.LayoutParams.MATCH_PARENT);
                            textView.setLayoutParams(params);


                            final Spinner spinner = new Spinner(getApplicationContext());
                            spinner.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                            final String[] personNames = {"Rahul", "Jack", "Rajeev", "Aryan", "Rashmi", "Jaspreet", "Akbar"};
                            ArrayAdapter arrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, personNames);
                            spinner.setAdapter(arrayAdapter);

                            relativeLayout.addView(textView);
                            relativeLayout.addView(spinner);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

And have a script where if you press the button, it displays all the values ​​of the spinner in the loop :

BtnPro.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), spinner.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
            }
        });

can anyone help me? I have done several experiments but I still fail until now. Thank you all.

Sairaj Sawant
  • 1,842
  • 1
  • 12
  • 16

1 Answers1

0

In your onClick function, you can retrieve the ArrayAdapter for the spinner then loop through its items:

BtnPro.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                ArrayAdapter adapter = ((Spinner)view).getAdapter();
                int n = adapter.getCount();
                for (int i = 0; i < n; i++) {
                    Toast.makeText(getApplicationContext(), adapter.getItem(i).toString(), Toast.LENGTH_SHORT);
                }
            }
        });

Just change however you want to display the item in the for loop.

See this answer for a similar solution.

siralexsir88
  • 418
  • 1
  • 4
  • 14