自定义适配器不显示微调器项目中的默认文本

时间:2019-02-04 04:36:21

标签: android android-spinner android-adapter

我想在第一个微调器中显示全选作为默认文本,为此,我使用了自定义适配器,但无法执行此操作。

我设置适配器的代码来像这样在旋转器中绑定数据:

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


        });

我正在为此使用Nothingselected适配器:

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

    }

此适配器的布局为:

<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" />

微调器的主要布局是:

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

如果我选择默认文本,则要在第二个微调器中显示所有项目。如何选择第一个微调器的索引0处的所有项目?

0 个答案:

没有答案