如何在微调器中获取对象的特定ID

时间:2018-10-15 08:24:41

标签: android json android-spinner

{
"Schools": [
    {
        "SCHOOLRSN": "1",
        "SCHOOLNAME": "Name1"
    },
    {
        "SCHOOLRSN": "11",
        "SCHOOLNAME": "Name2"
    },
    {
        "SCHOOLRSN": "10",
        "SCHOOLNAME": "Name3"
    },
    {
        "SCHOOLRSN": "9",
        "SCHOOLNAME": "Name4"
    },
    {
        "SCHOOLRSN": "21",
        "SCHOOLNAME": "Name5"
    }] 

这是我的JSON,当我选择学校名称时,我想在我的日志猫中找到它的ID,这里有Java代码

SchoolNameList = new ArrayList<>();
    SchoolRSN = new ArrayList<>();
    spinner = (Spinner) findViewById(R.id.country_Name);
    loadSpinnerData(URL);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String schname = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

            Log.e("schname", "" + schname);
            Toast.makeText(getApplicationContext(), schname, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            // DO Nothing here
        }
    });

 public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("Schools");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    String schoolrsn = jsonObject1.getString("SCHOOLRSN");
                    String schoolname = jsonObject1.getString("SCHOOLNAME");
                    SchoolNameList.add(schoolname);
                    SchoolRSN.add(schoolrsn);
                    Log.e("schoolname", "" + schoolname);
                }
                spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, SchoolNameList));
                spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, SchoolRSN));
            } catch (JSONException e) {
                e.printStackTrace();
            }

我只收到一件事来回应SchoolRSN或学校名称,当我在下拉列表中选择Name1时做类似的事情,我自动在Logcat中获得SchoolRSN,帮助我摆脱它,这浪费了我很多时间

1 个答案:

答案 0 :(得分:0)

删除此行

spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, SchoolRSN));

选择微调器项目时间变短

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String schname = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

  String schId= SchoolRSN.get(i).toString();
//i ==>info e==> error
                Log.i("schname", "" + schname);
Log.i("schId", "" + schId);
                Toast.makeText(getApplicationContext(), schname, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // DO Nothing here
            }
        });