转到textview时取决于Spinner的问题

时间:2019-03-29 01:56:02

标签: java android android-spinner

我正在使用带有相关微调器的代码,但是存在以下问题:

微调器可以正确地从JSON文件加载,但是当我尝试将第一个微调器的结果传递给textview时,它不会向我显示微调器的文本,但这是

我附上图片

Sample image enter image description here

我使用此代码传递Textview

ed_acu.setText(jornadaSpinner.getSelectedItem().toString());

但是我在Textview“ ed_acu”中收到此错误,而不是文本。

com.elgeos.tracker.spinnerCOL.State@a8f178

状态Java

public class State {
    private String stateName;
    private List<String> cities;


    public State(String stateName, List<String> cities) {
        this.stateName = stateName;
        this.cities = cities;
    }

    public String getStateName() {
        return stateName;
    }

    public List<String> getCities() {
        return cities;
    }

}

StateAdapter java

public class StateAdapter extends ArrayAdapter<State> {
    private List<State> stateList = new ArrayList<>();

    public StateAdapter(@NonNull Context context, int resource, int spinnerText, @NonNull List<State> stateList) {
        super(context, resource, spinnerText, stateList);
        this.stateList = stateList;
    }

    @Override
    public State getItem(int position) {
        return stateList.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return initView(position);

    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return initView(position);
    }

    /**
     * Gets the state object by calling getItem and
     * Sets the state name to the drop-down TextView.
     *
     * @param position the position of the item selected
     * @return returns the updated View
     */
    private View initView(int position) {
        State state = getItem(position);
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.jornada_list, null);
        TextView textView =  v.findViewById(R.id.spinnerText);
        textView.setText(state.getStateName() );
        return v;

    }
}

我的MainActivity代码

private void loadStateCityDetails() {
        final List<State> statesList = new ArrayList<>();
        final List<String> states = new ArrayList<>();
        JsonArrayRequest jsArrayRequest = new JsonArrayRequest
                (Request.Method.GET, cities_url, (String) null, new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray responseArray) {
                        pDialog.dismiss();
                        try {
                            //Parse the JSON response array by iterating over it
                            for (int i = 0; i < responseArray.length(); i++) {
                                JSONObject response = responseArray.getJSONObject(i);
                                String state = response.getString(KEY_STATE);
                                JSONArray cities = response.getJSONArray(KEY_CITIES);
                                List<String> citiesList = new ArrayList<>();
                                for (int j = 0; j < cities.length(); j++) {
                                    citiesList.add(cities.getString(j));
                                }
                                statesList.add(new State(state, citiesList));
                                states.add(state);

                            }
                            final StateAdapter stateAdapter = new StateAdapter(activity_make_user.this,
                                    R.layout.jornada_list, R.id.spinnerText, statesList);
                            jornadaSpinner.setAdapter(stateAdapter);

                            jornadaSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                                    //Populate City list to the second spinner when
                                    // a state is chosen from the first spinner
                                    State cityDetails = stateAdapter.getItem(position);
                                    List<String> cityList = cityDetails.getCities();
                                    ArrayAdapter citiesAdapter = new ArrayAdapter<>(activity_make_user.this,
                                            R.layout.colegio_list, R.id.citySpinnerText, cityList);
                                    colegioSpinner.setAdapter(citiesAdapter);
                                }

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

                                }
                            });

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        pDialog.dismiss();

                        //Display error message whenever an error occurs
                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();

                    }
                });

        // Access the RequestQueue through your singleton class.
        MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);

    }

3 个答案:

答案 0 :(得分:1)

正如您在StateAdapter中所见,您正在将微调器设置为显示state.getStateName()。在另一个TextView中看到的不是错误,而是直接传递给toString()的{​​{1}}对象的默认State。请改用setText(),您应该会得到所需的结果。

答案 1 :(得分:0)

jornadaSpinner.getSelectedItem()返回State对象,您需要从该对象获取状态名称。因此,请执行以下操作

if(!TextUtils.isEmpty(jornadaSpinner.getSelectedItem().getState()))
    ed_acu.setText(jornadaSpinner.getSelectedItem().getState());

答案 2 :(得分:0)

您必须在StateModel中重写toString()函数。您看到的是默认的toString()值。

public class State {
    private String stateName;
    private List<String> cities;


    public State(String stateName, List<String> cities) {
        this.stateName = stateName;
        this.cities = cities;
    }

    public String getStateName() {
        return stateName;
    }

    public List<String> getCities() {
        return cities;
    }

    @Override
    String toString(){
     return stateName;
    }

}