BetterSpinner
BetterSpinner copied to clipboard
Using setText to select viewed item programmatically removes all other items in the list
Hey, guys! Great component!
Info:
compileSdkVersion: 23
app level gradle:
dependencies {
compile 'com.weiwangcn.betterspinner:library-material:1.1.0'
}
activity_main.xml:
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="@+id/places_selector"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Location Selector"
android:textColorHint="#05ab9a"
app:met_floatingLabel="normal" />
MainActivity.java:
List<String> locationStringList = new ArrayList<>();
locationStringList.add("Whole Business");
for (Location location : locationList) {
locationStringList.add(location.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line, locationStringList);
placeSelector.setAdapter(adapter);
// Initialize location picker position:
locationPos = ...getLocationIndex()...
placeSelector.setText(locationStringList.get(locationPos));
Before placeSelector.seText(...) the spinner has all locationStringList items visible, after setText, only the selected string is displayed in the dropdown.
Anybody has an idea what I'm doing wrong?
Thanks! Ilai.
I'm suffering with same issue. @ilaif, are you using this component in a fragment? I'm asking because I use the component in other two apps in activities and this behavior not appears.
up
I have same problem, @ilaif have you find a solution ?
Same issue
Try this:
layout.xml
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner android:id="@+id/professional_spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Profissional" android:clickable="true" android:focusable="false" android:focusableInTouchMode="false" app:met_floatingLabel="highlight" app:met_textColorHint="@color/textColorPrimary" />
CustomAdapter
`private OnItemClickListener itemClickListener;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_professional, null);
final Professional professional = mFilteredValues.get(position);
if (professional != null) {
View row_professional_container = convertView.findViewById(R.id.row_professional_container);
TextView row_professional_textview_name = (TextView) convertView.findViewById(R.id.row_professional_textview_name);
ImageView row_professional_imageview_image = (ImageView) convertView.findViewById(R.id.row_professional_imageview_image);
ImageView row_professional_imageview_info = (ImageView) convertView.findViewById(R.id.row_professional_imageview_info);
//nome
row_professional_textview_name.setText(professional.nome);
//photo
if (!StringUtils.isNullOrEmpty(professional.foto)) {
Bitmap bitmap = BitmapUtils.decodeSampledBitmapFromEncodedString(professional.foto);
if (bitmap != null)
row_professional_imageview_image.setImageBitmap(bitmap);
}
//info
if (!StringUtils.isNullOrEmpty(professional.descricao)) {
row_professional_imageview_info.setVisibility(View.VISIBLE);
row_professional_imageview_info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertUtils.presentAlert(context, "Sobre o Profissional", professional.descricao);
}
});
} else row_professional_imageview_info.setVisibility(View.GONE);
row_professional_container.setTag(position);
row_professional_container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (itemClickListener != null) {
int positionItem = (int)view.getTag();
itemClickListener.onItemClick(view, positionItem, mFilteredValues.get(positionItem));
}
}
});
}
return convertView;
}
public void setOnItemClickListener(final OnItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}`
Activity / Fragment
professionalAdapter = new ProfessionalAdapter(getContext(), R.layout.row_professional,professionals); professionalSpinner.setAdapter(professionalAdapter); professionalAdapter.setOnItemClickListener(new ProfessionalAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position, Professional professional) { currentProfessional = professionals.get(position); professionalSpinner.setText(currentProfessional.getName()); } });
@pasqualinigustavo Care to give the full code of your adapter?
@smarques84 I already solve
I am also facing the same issue.
As far as I could find out the issue occurs when the you call setText() when the activity/fragment is in foreground (visible). Can be replicated by calling setText() on click of a button.
And the simple reason seems to be that MaterialBetterSpinner extends MaterialAutoCompleteTextView. So, it tries to find out the text that matches to the one we just set. And when it doesn't get any it just shows the one that we set now.
To verify my above point, I made 2 drop down entries "str" and "str1". When I do setText("str") and then check the dropdown it show both the options.
Got a solution for API level 17 and above - instead of setText(CharSequence) use setText(CharSequence, boolean),set boolean value to false Eg: setText("abc", false); The false parameter disables the filtering, hence all the dropdown items are shown
Hi!!
I don’t have the code anymore but I think your changes will fix the problem.
Thanks for the answer.
Best regards, Gustavo Pasqualini
On 21 Dec 2017, at 17:22, SUMIT5321 [email protected] wrote:
Got a solution for API level 17 and above - instead of setText(CharSequence) use setText(CharSequence, boolean),set boolean value to false Eg: setText("abc", false); The false parameter disables the filtering, hence all the dropdown items are shown
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Lesilva/BetterSpinner/issues/61#issuecomment-353435765, or mute the thread https://github.com/notifications/unsubscribe-auth/AKd9Qqo0f2Ag1IR56GFfYaDUjbQNSJABks5tCrARgaJpZM4KapLd.