2016-12-05 5 views
0

は、私は以下のようなJSON配列を有し、またもスピナーSpinnerで選択されたオプションにjson配列要素をマップするには?

{ 
    "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配列の動的すなわちあるオプションはスピナーから選択され、対応するIDを選択したいと私はにそれをしなければなりませんアンドロイド。どんな助けもありがとう。

+0

あなたのjsonは無効です.. pleseはそれを修正します –

答えて

1

1.First

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別のクラス 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

次の2つのアレイを作成するよりcheck here.

+0

は何がlist_valuesであることを指定できますか?名前の配列またはidの配列? –

+0

list_valuesはあなたのjson配列の値です。ここに渡す必要があります – Raju

+0

実際には2つのjson配列、つまり "DoctorName"と "DoctorId"がjson配列に渡す必要があります –

0
  1. についてスピナー

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

    にJSON配列値を示すためArrayAdapterを使用する必要があり、それは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のはdoctorIdでそのIDを取得します。スピナーのコード以下 書き込みは、選択した名前の、対応するIDを取得しますIDで

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

    をonclickの。

はそれがお役に立てば幸いです:)

+0

"hash.put(DoctorId.get(i)、DoctorName.get(i))"にエラーが発生しました。正確には –

+0

を取得してください。 – user3530687

関連する問題