Reputation: 679
I can populate my spinner with the array list from API. But unable to select or populate the selected item from spinner and display to the user.
The onItemSelected method does not get the position of the selected item in the spinner.
CustomSpinnerAdapter.java
public class CustomSpinnerAdapter extends BaseAdapter {
Context context;
List<String> userNames;
LayoutInflater inflter;
public CustomSpinnerAdapter(Context applicationContext, List<String> userNames) {
this.context = applicationContext;
this.userNames = userNames;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return userNames.size();
}
@Override
public Object getItem(int i) {
return userNames.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@NonNull
@SuppressLint({"ViewHolder", "InflateParams"})
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_item, null);
CustomTextView names = (CustomTextView) view.findViewById(R.id.tv_spinner_item);
names.setText(userNames.get(i));
return view;
}
}
My logic in fragment.
private SpinnerAdapter customAdapter;
private List<String> eqIds = new ArrayList<>;
apiInterface = ApiRequest.createService(ApiInterface.class);
Call<EquipmentTypeModel> call = apiInterface.getEquipmentType("application/json", token, id);
call.enqueue(new Callback<EquipmentTypeModel>() {
@Override
public void onResponse(Call<EquipmentTypeModel> call, Response<EquipmentTypeModel> response) {
if (response.isSuccessful()) {
eqIds.addAll(response.body().getData().getEquipmentList().getEquipIds());
}
}
@Override
public void onFailure(Call<EquipmentTypeModel> call, Throwable t) {
}
});
customAdapter = new CustomSpinnerAdapter(mContext, eqIds);
spTypeModel.setAdapter(customAdapter);
spTypeModel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, String.valueOf(parent.getAdapter().getItem(position)), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Upvotes: 1
Views: 785
Reputation: 14173
To get the selected value from spinner you should you
String selectedValue = spTypeModel.getSelectedItem().toString();
or
String selectedValue = String.valueOf(parent.getAdapter().getItem(position));
instead of
String selectedValue = String.valueOf(position);
Update: Change getItem
method in your custom adapter to
@Override
public Object getItem(int i) {
return userNames.get(i);
}
Update 2: I saw your problem, please change your code.
private SpinnerAdapter customAdapter;
to
private CustomSpinnerAdapter customAdapter;
then add this line after you add new data to your adapter.
eqIds.addAll(response.body().getData().getEquipmentList().getEquipIds());
customAdapter.notifyDataSetChanged(); // Add this line to notify your adapter about new data
Update 3: From your xml file, because your spinner height is 18dp.
You can set your spinner height to wrap_content
or keep current height but remove padding from spinner or from the custom text view. It's up to you.
Upvotes: 1
Reputation: 1126
Try if this works (It will show you the value, not the position)
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
Upvotes: 0