Reputation: 3
I'm using a code with a dependent spinner, but I have the following problem:
the spinner loads from a JSON file correctly, but when I try to pass the result of the first spinner to a textview, it does not show me the text of the spinner but this:
I attach image
I use this code for pass a Textview
ed_acu.setText(jornadaSpinner.getSelectedItem().toString());
But i get this error in my Textview "ed_acu", y not the text.
com.elgeos.tracker.spinnerCOL.State@a8f178
State 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;
}
}
My MainActivity code
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);
}
Upvotes: 0
Views: 65
Reputation: 3000
As you can see in StateAdapter
, you are setting the spinner to display state.getStateName()
. What you are seeing in the other TextView is not an error, but the default toString()
for your State
object that you are directly passing to setText()
. Use jornadaSpinner.getSelectedItem().getStateName()
instead, and you should get the desired result.
Upvotes: 1
Reputation: 485
jornadaSpinner.getSelectedItem() returns State object and You need to get state name from that object. So, kindly do the following thing
if(!TextUtils.isEmpty(jornadaSpinner.getSelectedItem().getState()))
ed_acu.setText(jornadaSpinner.getSelectedItem().getState());
Upvotes: 0
Reputation: 153
You must override the toString() function in Your StateModel. What you are seeing is the default toString() value.
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;
}
}
Upvotes: 0