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.

1
  • your json is invalid.. plese correct it Commented Dec 5, 2016 at 9:40

3 Answers 3

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)
        {
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

can u specify what is list_values? array of name or array of id?
list_values is nothing but your json array values you have to pass here
actually i have 2 json arrays i.e "DoctorName" and "DoctorId" which json array should pass
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.
yes both arrays have same number of records. how to get the Id can u help programmatically? @ Pratik Dasa
|
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 :)

1 Comment

Getting error on "hash.put(DoctorId.get(i), DoctorName.get(i))" precisely on get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.