Comments (10)
Sorry, you could add more details about the problem? Maybe a piece of code where the problem occurs.
Please could you write in English?
from betterspinner.
The Material/BetterSpinner
extends Material/EditText
, so it does not have events of Android native Spinner
.
When you select an item in the Material/BetterSpinner
, the displayed text changes. So I use the TextChangedListener
implementation to capture this. Its a workaround. I just created a friendlier implementation for this listener.
betterSpinner.addTextChangedListener(new OnItemSelectedListener() {
@Override
protected void onItemSelected(String displayedText) {
// do something
}
});
And to get the item that is selected, you need to get the text being displayed and search your collection of objects.
List<People> peoples = ....;
String displayedText = betterSpinner.getText();
for (People p : peoples) {
if (p.toString().equals(displayedText)) {
// found the selected People!!!
}
}
So I also created a friendly implementation for the Adapter
. If you use my Adapter
, you can do like this:
String displayedText = betterSpinner.getText();
People selectedPeople = myAdapter.stringToItem(displayedText);
I will create a sample project soon, but today I can not. 😢
from betterspinner.
I am so sorry,It is my fault,because I use the MaterialBetterSpinner in a BottomSheet,I should use view.findViewById(resID),Thank for all of your help @douglasjunior
from betterspinner.
yeah ,of course
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="@+id/spinnermm"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="choose school"
app:met_floatingLabel="school" />
@Bind(R.id.spinnermm)
MaterialBetterSpinner spinner2;
....
String[] list = getResources().getStringArray(R.array.month);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, list);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
L.d(id);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
this just a test code,like the demo of the library.
I found the same answer in #15 but i can't get more info but
and finally find
spinner auto focus and auto set default value make this error, you should disabled xml auto focus to prevent it from happen, after you setAdapter, and then set default value.
Chage from onItemSelected to onItemClick.
my question isn't solve yet...
from betterspinner.
The setOnItemSelectedListener
does not work, because the Material/BetterSpinner
is a EditText
with auto-complete
. Not a really Spinner
.
My xml:
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="@+id/sp_cultura"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/plantio_novo_cultura"
android:textSize="@dimen/edittext_text"
app:met_bottomTextSize="@dimen/edittext_bottom_text"
app:met_floatingLabel="highlight"
app:met_floatingLabelTextSize="@dimen/edittext_floating_label_text"
app:met_helperTextColor="@color/textColorHelper"
app:met_primaryColor="@color/colorAccentDark"
app:met_underlineColor="@color/colorAccent" />
My activity:
spCulturas = (MaterialBetterSpinner) findViewById(R.id.sp_cultura);
culturaSpinnerAdapter = new SpinnerArrayAdapter<Cultura>(getContext()) {
@Override
public String itemToString(Cultura item) {
return item.getNome(); // make sure that is unique
}
};
spCulturas.setAdapter(culturaSpinnerAdapter);
Get selected item:
Cultura selected = culturaSpinnerAdapter.stringToItem(spCulturas.getText());
Select item event:
spCulturas.addTextChangedListener(new OnItemSelectedListener() {
@Override
protected void onItemSelected(String string) {
Cultura selected = culturaSpinnerAdapter.stringToItem(string);
// do something
}
});
My custom OnItemSelectedListener here.
These classes made easier my use. I hope this can help you.
from betterspinner.
sorry I can't really understand,can you send me the full project demo with Email. my email is [email protected]. thank you very very very much.
from betterspinner.
@douglasjunior Why is necesary to compare each element from the adapter? Is there a problem when keeping track of the las item clicked from the list adapter? I have tried the following code in some devices and I think it works, do you see a problem doing this way?
public class MySpinner extends MaterialBetterSpinner {
protected int selectedPosition = -1;
// ...
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
super.onItemClick(adapterView, view, i, l);
this.selectedPosition = i;
}
public int getSelectedPosition() {
return selectedPosition;
}
}
from betterspinner.
@byoms23 this works, but you need to be aware that the selectedPosition
value will not be updated if the item is selected programmatically.
from betterspinner.
@douglasjunior I am using BetterSpinner in my code but getting flowing error
Process: com.in2things.betterspinnerdemo,
PID: 9776 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence, android.widget.Filter$FilterListener)' on a null object reference at android.widget.AutoCompleteTextView.performFiltering(AutoCompleteTextView.java:862)
at com.in2things.betterspinnerdemo.CustomSpinner.onFocusChanged(CustomSpinner.java:46)
at android.view.View.handleFocusGainInternal(View.java:5481)
at android.view.View.requestFocusNoSearch(View.java:8479)
at android.view.View.requestFocus(View.java:8458)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2794)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2750)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2794)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2750)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2794)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2750)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2794)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2750)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2794)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2750)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2794)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2753)
at android.view.View.requestFocus(View.java:8425)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2027)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
I am attaching data to adapter from server but getting error.
can you help me for this.
from betterspinner.
@Amit-Nandwana, could you create a new Issue for your question? I also suggest putting the code snippet where you create your adapter and insert the items.
It helps keep the organization and makes it easier for people to find your problem. 😃
from betterspinner.
Related Issues (20)
- How to change floating label color? HOT 2
- How to set the selected item with particular position? HOT 1
- Please add search popup option.
- Annoying Double Tap issue HOT 3
- how to get value and position of selected item ...? HOT 1
- how to align the drop down icon close to the selected text
- Add an option to hide the dropdown arrow icon HOT 1
- How to change the material better spinner background color
- Please add wiki with explanation of usage MaterialEditText
- How to set typeface in label and hint
- My fix for bugs with empty data, focus, setText and setSelected/getSelected feature
- disable MaterialBetterSpinner HOT 1
- Wrap Long Text
- HOW TO set default value HOT 1
- How to change right icon on betterspinner HOT 1
- how to open spinner programmatically?
- How to validate if no value selected HOT 1
- MaterialBetterSpinner selection remove HOT 1
- Poor docs: Can't get selected item from spinner nulls
- how to know which better spinner is selected or clicked HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from betterspinner.