简体   繁体   English

使用 Volley 在 Android Studio 中具有 id 和名称的 Spinner

[英]Spinner with id and name in Android Studio using Volley

In Android Studio, I want show a spinner with id and name dynamically using Java.在 Android Studio 中,我想使用 Java 动态显示一个带有 id 和名称的微调器。 And also when clicking submit I want to get the id of the name from spinner using Volley.而且,当单击提交时,我想使用 Volley 从微调器获取名称的 ID。 Here I displayed the spinner with dynamic data which having only name, but here I want sync my id with this spinner.在这里,我用只有名称的动态数据显示了微调器,但在这里我想将我的 id 与这个微调器同步。 Up to now I have tried,到目前为止,我已经尝试过,

private void spinnerbind() {
        displayLoader();
        JSONObject request = new JSONObject();
        try {
            request.put("action", "get_specialist");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsArrayRequest = new JsonObjectRequest
                (Request.Method.POST, url,request, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        pDialog.dismiss();
                        try {

                            JSONArray dataArray  = response.getJSONArray("data");
                            if(response.optString("status").equals("1")){

                                goodModelArrayList = new ArrayList<>();
                                //JSONArray dataArray  = obj.getJSONArray("data");

                                for (int i = 0; i < dataArray.length(); i++) {

                                    PlayerModel playerModel = new PlayerModel();
                                    JSONObject dataobj = dataArray.getJSONObject(i);

                                    playerModel.setid(dataobj.getString("id"));
                                    playerModel.setTitle(dataobj.getString("specialist"));

                                    goodModelArrayList.add(playerModel);

                                }

                                for (int i = 0; i < goodModelArrayList.size(); i++){
                                    names.add(goodModelArrayList.get(i).getTitle().toString());
                                }
                                spinnerArrayAdapter = new ArrayAdapter<String>(ProfileEditActivity.this, simple_spinner_item, names);
                                spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
                                mySpinner.setAdapter(spinnerArrayAdapter);

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //displaying the error in toast if occurrs
                        Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

        MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
    }

try this:尝试这个:

mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
     {
        PlayerModel playerModel = goodModelArrayList.get(position);
        String id=playerModel.getId();
        String title=playerModel.getTitle();
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM