Anjali Patel
Anjali Patel

Reputation: 825

How to map json array elements to option selected in Spinner?

I have a json array like below and want to select corresponding id when an option is selected from spinner which is also dynamic i.e. also a json array which is displayed in Spinner

{
   "DoctorName": ["0001 DR. Sameer", "0001 DR.Krishna murti", "110 Mr. Ram", "4 Mr. Yash Pathak.", "99 Dr. Varma"],
    "DoctorId": [3,2,110,4,99]
};

and I have to do it into Android. Any help will be appreciated.

Upvotes: 0

Views: 883

Answers (3)

Anjali Patel
Anjali Patel

Reputation: 825

1.First Create a class

 public class DoctorName
  {

public String id = "";
public String name = "";

public void setId(String id)
{
    this.id = id;
}

public void setName(String name)
{
    this.name = name;
}


public String getName()
{
    return name;
}

public String getId()
{
    return id;
}

// A simple constructor for populating our member variables for this tutorial.
public DoctorName( String _id, String _name)
{
    id = _id;
    name = _name;

}

// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control.  If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
    return( name );
}

}

2.create another class MainClass.java

ArrayList<DoctorName> doctList = new ArrayList<DoctorName>() ;

    for(int i=0;i<arr_name.length;i++)
    {
        doctList.add(new DoctorName(arr_id[i],arr_name[i]));
    }

    //fill data in spinner
    //ArrayAdapter<DoctorName> adapter = new ArrayAdapter<DoctorName>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, answers);
    ArrayAdapter <DoctorName>adapter= new ArrayAdapter<DoctorName>
            (getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,doctList );

    Doctor_selection.setAdapter(adapter);

    Doctor_selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        {

            DoctorName doctorName = (DoctorName) parent.getSelectedItem();
            Log.i("SliderDemo", "getSelectedItemId" +doctorName.getId());

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent)
        {
        }
    });

Upvotes: 2

Aniket-Shinde
Aniket-Shinde

Reputation: 213

  1. Create two arrays, that is DoctorName and DoctorId
  2. Create dynamic HashMap using above arrays, put all the values in key - value form by using for loop. But for this the length of both above arrays should be same.

    HashMap<String, String> hash;
    for(int i = 0; i < DoctorName.size() ; i++) {
    
        hash = new HashMap<String, String>();
        hash.put(DoctorId.get(i), DoctorName.get(i));
    }
    
  3. For spinner send only doctor name list from Map (hash), and onclick of spinner gets its Id that is doctorId. Write below code in Spinner onclick

    String name = spinner.getSelectedItem().toString();
    String id = hash.get(name);
    

    In id you will get the corresponding id of selected name.

Hope It helps :)

Upvotes: 0

Raju
Raju

Reputation: 1223

You have to use ArrayAdapter to show the json array values into spinner

Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list_values); //selected item will look like a spinner set from XML
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

//Set on item select Listener
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

             // here you can get your selected id

            }

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

            }
        });

For more check here.

Upvotes: 0

Related Questions