2016-12-05 47 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

相關問題