根据其他微调器中的选择填充微调器

时间:2016-05-03 10:25:20

标签: android android-spinner

我正在尝试将服务器中的数据导入到微调器中,我的json对象是

{"result":{"AndhrPradesh":["Jayamahal","ABC","JP nagar"],"Mumbai":["XYZ","PQR"],"Pune":["123","Hi"]}}

我能够在一个旋转器中获得AndhraPradesh,Mumbai和Pune的值。现在我的问题是在选择城市后我想在该城市显示相应的数据。 例如,如果从一个微调器中选择Mumbai我想在另一个微调器中显示XYZ,PQR,请帮助我。

2 个答案:

答案 0 :(得分:0)

首先解析json数据,如

JSONObject jsonObject = response.getJSONObject("data");
JSONArray cityName= jsonObject.getJSONArray("AndhrPradesh")
for(i=0;i<cityName.length();i++){
//your inner string of the Array
}

然后

citynameSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //here set the values to another spinner
            }
        });

答案 1 :(得分:0)

请使用此代码段

public class SpinnerFilterActivity extends AppCompatActivity {
private ArrayAdapter<String> mArrayAdapter;
private Spinner mMainSPN;
private ArrayList<String> mSpinnerArray;
private Map<String,ArrayList<String>> innterDataMap;
private Spinner mInnerSPN;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSpinnerArray = new ArrayList<String>();
    innterDataMap = new HashMap<String, ArrayList<String>>();
    mMainSPN = (Spinner) findViewById(R.id.spn_main);
    mInnerSPN = (Spinner) findViewById(R.id.spn_inner);

    mMainSPN.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String key = mSpinnerArray.get(position);
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(SpinnerFilterActivity.this,android.R.layout.simple_spinner_item,innterDataMap.get(key));
            mInnerSPN.setAdapter(arrayAdapter);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
    parseJson();
}

private void parseJson() {
    String s= "{\"result\":{\"AndhrPradesh\":[\"Jayamahal\",\"ABC\",\"JP nagar\"],\"Mumbai\":[\"XYZ\",\"PQR\"],\"Pune\":[\"123\",\"Hi\"]}}\n";
    try {
        JSONObject jsonObject = new JSONObject(s);
        JSONObject obj= jsonObject.getJSONObject("result");
        Iterator<String> stringIterator = obj.keys();
        while (stringIterator.hasNext()){
            String key = stringIterator.next();
            mSpinnerArray.add(key);
            JSONArray jsonArray = obj.getJSONArray(key);
            ArrayList<String> innerList = new ArrayList<>();
            for (int i=0;i<jsonArray.length();i++){
                innerList.add(String.valueOf(jsonArray.get(i)));
            }
            innterDataMap.put(key,innerList);
        }
        populateDataInSpinner();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

private void populateDataInSpinner() {
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mSpinnerArray);
    mMainSPN.setAdapter(arrayAdapter);
  }
}

在这段代码中,我将数据保存在2个字段中,一个mSpinnerArray显示aster列表,另一个是map,其中内部列表代表键保存为父值。每当调用onOptionItemSelected方法时,我代表来自主列表的seleted项从地图中获取内部数据列表,因为它作为innerMap数据中的键。我希望我很清楚。如果有任何问题,请告诉我。