Linked Questions

Popular Questions

Custom adapter not display default text in spinner item

Asked by At

I want to display select all as default text in first spinner and for this I used custom adapter but I am unable to do this.

I set code of adapter to bind data in spinner like this:

private void getClientGroup() {

final APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);

        final FilterModel filterModel = new FilterModel("G", String.valueOf(clientid), String.valueOf(loginid), "A", String.valueOf(userrole));
        Call<FilterModel> call1 = apiInterface.createClientGroup(filterModel);
        call1.enqueue(new Callback<FilterModel>() {
            @Override
            public void onResponse(Call<FilterModel> call, Response<FilterModel> response) {

                FilterModel filterResponse = response.body();

                Log.e("Getclientgroup:", "success");

                for (int i = 0; i < filterResponse.getT0().size(); i++) {


                    int groupId = filterResponse.getT0().get(i).getID();
                    String groupText = filterResponse.getT0().get(i).getTEXT();

                    Log.e("Groupid==", String.valueOf(groupId));
                    Log.e("GroupText==", String.valueOf(groupText));

                    groupTextList.add(groupText);

                    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, android.R.id.text1, groupTextList);
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    spGroup.setAdapter(
                            new NothingSelectedSpinner(
                                    dataAdapter,
                                    R.layout.nothing_selected_spinner_item, 0,
                                    getActivity()));
                    // Drop down layout style - list view with radio button
                    dataAdapter.setDropDownViewResource(R.layout.nothing_selected_spinner_item);

                    // attaching data adapter to spinner
                    spGroup.setAdapter(dataAdapter);


                }

            }

            @Override
            public void onFailure(Call<FilterModel> call, Throwable t) {

                Log.e("Error is==", t.getMessage());
            }


        });

I am using Nothingselected adapter for this:

public class NothingSelectedSpinner implements SpinnerAdapter, ListAdapter {

        protected static final int EXTRA = 1;
        protected SpinnerAdapter adapter;
        protected Context context;
        protected int nothingSelectedLayout;
        protected int nothingSelectedDropdownLayout;
        protected LayoutInflater layoutInflater;


        public NothingSelectedSpinner(SpinnerAdapter spinnerAdapter,
                                             int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) {
            this.adapter = spinnerAdapter;
            this.context = context;
            this.nothingSelectedLayout = nothingSelectedLayout;
            this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
            layoutInflater = LayoutInflater.from(context);
        }




        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            return null;
        }




        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }





        @Override
        public final View getView(int position, View convertView, ViewGroup parent) {
            // This provides the View for the Selected Item in the Spinner, not
            // the dropdown (unless dropdownView is not set).
            if (position == 0) {
                return getNothingSelectedView(parent);
            }
            return adapter.getView(position - EXTRA, null, parent); // Could re-use
            // the convertView if possible.
        }

        protected View getNothingSelectedDropdownView(ViewGroup parent) {
            return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false);
        }



        @Override
        public int getItemViewType(int position) {
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public long getItemId(int position) {
            return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA;
        }

        @Override
        public boolean hasStableIds() {
            return adapter.hasStableIds();
        }

        @Override
        public boolean isEmpty() {
            return adapter.isEmpty();
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            adapter.registerDataSetObserver(observer);
        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            adapter.unregisterDataSetObserver(observer);
        }

        @Override
        public boolean areAllItemsEnabled() {
            return false;
        }

        @Override
        public boolean isEnabled(int position) {
            return position != 0; // Don't allow the 'nothing selected'
            // item to be picked.
        }
        protected View getNothingSelectedView(ViewGroup parent) {
            return layoutInflater.inflate(nothingSelectedLayout, parent, false);
        }

    }

and layout for this adapter is:

<TextView
    android:id="@+id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:padding="@dimen/dp10"
    android:textSize="@dimen/dp30"
    android:textColor="@color/colorBlack"
    android:text="Select all" />

and main layout of spinner is:

<LinearLayout
    android:id="@+id/ll_filter_report"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/dp20"
    android:layout_marginTop="@dimen/dp20"
    android:layout_marginRight="@dimen/dp20"
    android:background="@drawable/rounded_layout_white"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_sub_group"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp80"
        android:orientation="horizontal"
        android:padding="@dimen/dp20"
        android:weightSum="1">

        <TextView
            android:id="@+id/lbl_sub_group"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="left|center"
            android:text="@string/str_sub_group"
            android:textSize="@dimen/dp20" />

        <Spinner
            android:id="@+id/spn_sub_group"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"></Spinner>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_client"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp80"
        android:layout_marginTop="@dimen/dp10"
        android:orientation="horizontal"
        android:padding="@dimen/dp20"
        android:weightSum="1">

        <TextView
            android:id="@+id/lbl_client"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="left|center"
            android:text="@string/str_client"
            android:textSize="@dimen/dp20" />

        <Spinner
            android:id="@+id/spn_client"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"></Spinner>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_date"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp80"
        android:layout_marginTop="@dimen/dp10"
        android:orientation="horizontal"
        android:padding="@dimen/dp20"
        android:weightSum="1">

        <TextView
            android:id="@+id/lbl_date"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:gravity="left|center"
            android:text="@string/str_as_on"
            android:textSize="@dimen/dp20" />

        <EditText
            android:id="@+id/edt_date"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:hint="02/01/2019"
            android:padding="@dimen/dp10"></EditText>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp20"
        android:orientation="horizontal"
        android:padding="@dimen/dp10"
        android:weightSum="1">

        <Button
            android:id="@+id/btn_Search"
            android:layout_width="@dimen/dp0"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="@color/colorLightBlue"
            android:text="@string/str_search"
            android:textAllCaps="false"
            android:textColor="@color/colorWhite" />

        <Button
            android:id="@+id/btn_reset"
            android:layout_width="@dimen/dp0"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/dp10"
            android:layout_weight="0.5"
            android:background="@color/colorLightBlue"
            android:text="@string/str_reset"
            android:textAllCaps="false"
            android:textColor="@color/colorWhite" />
    </LinearLayout>
</LinearLayout>

I want to display all the items in second spinner if I select default text select all at index 0 of first spinner how I do this?

Related Questions