2

I want to create spinner with multiple selection,like user can select more than one items and get that items in edittext,for that i am following this example but its not working properly http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in.html in my fragment class

check out this link here i got answer for activity class not but not for fragment Get the alert dialog selected value to edittext

Community
  • 1
  • 1
  • read this thread http://stackoverflow.com/questions/21132485/setting-multiple-values-to-spinner-in-android – rogerwar Nov 25 '14 at 16:30
  • 2
    Why is it not working properly?? what does it not do what you want it to do? Please be more clear otherwise your answer can't be answered correctly and SO is not just to handout pieces of codes that suits one person bests. – Dion Segijn Nov 28 '14 at 07:37
  • i edited my question see that –  Nov 28 '14 at 07:42
  • 3
    If you want help, explain **completely and precisely** what "its not working properly" means. – CommonsWare Nov 30 '14 at 23:37

1 Answers1

1

If you are following this tutorial "http://v4all123.blogspot.in/2013/09/spinner-with-multiple-selection-in.html" its working correctly in Activity.

If you are facing problem in creating same for Fragment, here is the code.

main_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </FrameLayout>



</RelativeLayout>

fragment_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.testandroid.MultiSelectionSpinner
        android:id="@+id/mySpinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/mySpinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="118dp"
        android:onClick="onClick"
        android:text="Get Items" />

    <EditText
        android:id="@+id/values"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

MyFragment.java class

public class MyFragment extends Fragment {

    MultiSelectionSpinner spinner;

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate (R.layout.fragment_layout, null);

        String[] array = { "one", "two", "three" };
        spinner = (MultiSelectionSpinner) v.findViewById (R.id.mySpinner1);
        spinner.setItems (array);


        final EditText values = (EditText) v.findViewById (R.id.values);

        Button button1 = (Button) v.findViewById (R.id.button1);
        button1.setOnClickListener (new OnClickListener() {

            @Override
            public void onClick (View v) {

                String s = spinner.getSelectedItemsAsString();  
                values.setText (s);

            }
        });
        return v;
    }
}

In your main activity

MyFragment fragment = new MyFragment ();
FragmentTransaction ft = getFragmentManager ().beginTransaction ();
ft.replace (R.id.container, fragment, "fragment");
ft.commit ();

Screen 1 enter image description here

Screen 2 enter image description here

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83