Android自定义微调器问题

时间:2015-09-15 11:55:18

标签: android android-fragments spinner

我在发布问题之前搜索了很长时间......

我使用以下Android版本:

  • compilesdkversion:21
  • minSdkVersione:16
  • targetSdkVersion:21

我在自定义Spinner下拉布局时遇到了一些问题

我在服务器上调用REST服务,它返回给我一个这个对象的列表:

我的微调器模型类

public class TipoAllarmeMercePericolosa implements Serializable {

    private static final long serialVersionUID = 5833474803233889506L;
    private String codiceAllarme;
    private String descrizioneAllarme;

    public TipoAllarmeMercePericolosa(String codiceAllarme, String descrizioneAllarme) {
        this.codiceAllarme = codiceAllarme;
        this.descrizioneAllarme = descrizioneAllarme;
    }

    public TipoAllarmeMercePericolosa() {
    }

    public String getCodiceAllarme() {
        return codiceAllarme;
    }

    public void setCodiceAllarme(String codiceAllarme) {
        this.codiceAllarme = codiceAllarme;
    }

    public String getDescrizioneAllarme() {
        return descrizioneAllarme;
    }

    public void setDescrizioneAllarme(String descrizioneAllarme) {
        this.descrizioneAllarme = descrizioneAllarme;
    }

    @Override
    public String toString() {
        return descrizioneAllarme;
    }
}

我创建了自己的自定义适配器:

CustomAdapter

public class TipoAllarmeSpinnerAdapter extends ArrayAdapter<TipoAllarmeMercePericolosa> {
    private final List<TipoAllarmeMercePericolosa> tipiAllarmi;
    private final Context ctx;
    public TipoAllarmeSpinnerAdapter(Context context, int resource, List<TipoAllarmeMercePericolosa> tipiAllarmi) {
        super(context, resource);
        this.tipiAllarmi = tipiAllarmi;
        this.ctx = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Restituiamo la view normale
        return super.getView(position,convertView,parent);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        return initView(position, convertView, parent);
    }

    private View initView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) this.ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView label = (TextView)inflater.inflate(R.layout.tipo_allarme_spinner_layout, parent, false);
        TipoAllarmeMercePericolosa tamp = tipiAllarmi.get(position);
        if( tamp != null )
        {
            label.setText(tamp.getDescrizioneAllarme());
        }
        else
        {
            label.setText(ctx.getString(R.string.selezione_tipo_allarme));
        }
        return label;
    }
}

我定义了我的片段视图:

片段视图ricerca_allarmi_tab.xml

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

    <TextView
        android:id="@+id/ricercaAllarmiTabViewId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:text="@string/ricerca_tipo_allarmi"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tipo_allarme_spinner"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/ricercaAllarmiTabViewId"
        android:layout_toRightOf="@+id/ricercaAllarmiTabViewId" />

</RelativeLayout>

我定义了我的微调器行视图

微调器自定义行视图tipo_allarme_spinner_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:id="@+id/spinner_descrizione_tipo_allarme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Elenco notifiche"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

在我的片段中,我执行以下操作:

public class RicercaAllarmiTab extends Fragment {
    private static final String TAG_LOG = RicercaAllarmiTab.class.getSimpleName();
    Spinner tipoAllarmiSpinner;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.ricerca_allarmi_tab, container, false);
        tipoAllarmiSpinner = (Spinner)v.findViewById(R.id.tipo_allarme_spinner);
        populateSpinner();
        return v;
    }

    private void populateSpinner()
    {
        final Context ctx = this.getActivity();
        final Spinner tipoAllSpin = this.tipoAllarmiSpinner;
        MerciPericoloseRestClient.getTipiAllarme(MerciPericoloseRestClient.getTipoMerceAbsoluteUrl(), null, new TipiAllarmiHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, TipoAllarmeMerceAjaxResponse response) {
                try {
                    List<TipoAllarmeMercePericolosa> tipiAllarmiJson = response.getPayload();
                    int dimensione = tipiAllarmiJson != null ? tipiAllarmiJson.size()+1:1;
                    final List<TipoAllarmeMercePericolosa> tipiAllarmi = new ArrayList<TipoAllarmeMercePericolosa>(dimensione);
                    tipiAllarmi.add(new TipoAllarmeMercePericolosa(IConstants.NO_SPINNER_SELECTION, ctx.getString(R.string.selezione_tipo_allarme)));
                    tipiAllarmi.addAll(tipiAllarmiJson);
                    //ArrayAdapter<TipoAllarmeMercePericolosa> spinnerAdapter = new ArrayAdapter<TipoAllarmeMercePericolosa>(ctx, android.R.layout.simple_dropdown_item_1line, tipiAllarmi);

                    TipoAllarmeSpinnerAdapter spinnerAdapter = new TipoAllarmeSpinnerAdapter(ctx, android.R.layout.simple_spinner_item, tipiAllarmi);
                    spinnerAdapter.setDropDownViewResource(R.layout.tipo_allarme_spinner_layout);
                    tipoAllSpin.setAdapter(spinnerAdapter);
                    tipoAllSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                            TipoAllarmeMercePericolosa tipo = tipiAllarmi.get(position);
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            Log.i(TAG_LOG, "Nulla scelto");
                        }
                    });
                }
                catch (Exception e)
                {
                    Log.e(TAG_LOG, "Errore nella costruzione dello spinner "+e.getMessage(), e);
                    (new AlertDialogBuilder(ctx)).buildDialog(ctx.getString(R.string.elenco_tipi_allarmi_dialog_title), getString(R.string.elenco_tipi_allarmi_dialog_message));
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, TipoAllarmeMerceAjaxResponse errorResponse) {
                Log.e(TAG_LOG, "Errore nel caricamento dei tipi allarmi; " + throwable.getMessage(), throwable);
                (new AlertDialogBuilder(ctx)).buildDialog(ctx.getString(R.string.elenco_tipi_allarmi_dialog_title), getString(R.string.elenco_tipi_allarmi_dialog_message));
            }
        });
    }
}

ajax调用正常;事实上,如果我使用StringAdapter(populateSpinner方法中的注释行,我能够在spinner中显示我的元素的描述(因为我重写了类TipoAllarmeMercePericolosa的toString方法)

如果我使用上面代码中显示的自定义appender(未注释的appender),则永远不会填充微调器;我也不例外......只是我的锭床工人从未填充过

我真的无法理解为什么会这样......可能有人告诉我我失踪了什么吗?

谢谢你 安吉洛

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,两天后我发现问题出在自定义微调器xml中使用RelativeLayout。

如果您执行以下操作

<?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">
    <TextView
        android:id="@+id/spinner_descrizione_tipo_allarme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Elenco notifiche"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

它会正常工作