2016-12-05 71 views
0

我有一个JSON数组像下面和要选择当一个选项被从喷丝选择相应的ID,这也是动态即也被显示在微调如何将json数组元素映射到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] 
}; 

JSON数组和我必须把它做成为Android系统。任何帮助将不胜感激。

+0

你的json无效..请更正它 –

回答

1

1.首先创建一个类

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.创建另一个类 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) 
     { 
     } 
    }); 
0

你必须使用ArrayAdapter显示JSON数组值到微调

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) { 

      } 
     }); 

更多check here.

+0

你能指定什么是list_values?数组的名称或数组的ID? –

+0

list_values不过是你的json数组值,你必须通过这里 – Raju

+0

其实我有2个json数组,即“DoctorName”和“DoctorId”,json数组应该通过 –

0
  1. 创建两个阵列,即DoctorName和DoctorId
  2. 使用创建动态的HashMap上面的数组,通过使用for循环将所有值放在键 - 值形式中。但是对于这两个数组的长度应该是相同的。

    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. 对于微调从地图(哈希)发送唯一的医生名单,并且微调的的onclick得到它的ID doctorId。 下面写代码的onclick微调

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

    在ID您将获得选择的名称对应的ID。

希望它能帮助:)

+0

获取“hash.put(DoctorId.get(i),DoctorName.get(i))”的错误正确地得到 –

+0

错误是什么? – user3530687

相关问题