0

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.

Learning Always
  • 1,563
  • 4
  • 29
  • 49
Anjali Patel
  • 807
  • 2
  • 11
  • 23

3 Answers3

2

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)
        {
        }
    });
Anjali Patel
  • 807
  • 2
  • 11
  • 23
0

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.

Community
  • 1
  • 1
Raju
  • 1,183
  • 3
  • 11
  • 19
  • can u specify what is list_values? array of name or array of id? – Anjali Patel Dec 05 '16 at 09:51
  • list_values is nothing but your json array values you have to pass here – Raju Dec 05 '16 at 09:54
  • actually i have 2 json arrays i.e "DoctorName" and "DoctorId" which json array should pass – Anjali Patel Dec 05 '16 at 09:56
  • see my updated answer, first of all you have to change you json format it is invalid . make it into one array like one id with one name then try the above code. – Raju Dec 05 '16 at 10:02
  • "DoctorName" and "DoctorId" then make adapter for DoctorName and when it selects then get the value from DoctorID array, as I assume that DoctorId and DoctorName both arrays have same number of records in it. @AnjaliPatel – Pratik Dasa Dec 05 '16 at 10:20
  • 1
    yes both arrays have same number of records. how to get the Id can u help programmatically? @ Pratik Dasa – Anjali Patel Dec 05 '16 at 10:27
0
  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 :)

Aniket-Shinde
  • 213
  • 3
  • 9