Coder Social home page Coder Social logo

Comments (10)

douglasjunior avatar douglasjunior commented on August 18, 2024 1

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.

douglasjunior avatar douglasjunior commented on August 18, 2024 1

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.

Lswks avatar Lswks commented on August 18, 2024 1

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.

Lswks avatar Lswks commented on August 18, 2024

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.

douglasjunior avatar douglasjunior commented on August 18, 2024

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);

My custom adapter here.

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.

Lswks avatar Lswks commented on August 18, 2024

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.

byoms23 avatar byoms23 commented on August 18, 2024

@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.

douglasjunior avatar douglasjunior commented on August 18, 2024

@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.

Amit-Nandwana avatar Amit-Nandwana commented on August 18, 2024

@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.

douglasjunior avatar douglasjunior commented on August 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.