设置微调内容

时间:2011-11-16 18:58:27

标签: android spinner android-arrayadapter

关于如何设置微调器的内容,我在这里已经关注了很多问题,但没有一个对我有用,我仍然会收到错误。

这是布局

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/libDispText"
        android:textAppearance="?android:attr/textAppearanceMedium" >
</TextView>

    <Spinner
        android:id="@+id/librosDispSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
</Spinner>
    <TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content">

        <Button
            android:id="@+id/seleccionarBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Seleccionar" >
</Button>

        <Button
            android:id="@+id/cancelarBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancelar" >
</Button>
    </TableRow>

    <TextView
        android:id="@+id/elementosTxtVw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:text="TextView"
        android:visibility="invisible" 
        android:focusable="false"/>

</LinearLayout>

没有什么花哨的东西,只有textview作为微调器的标签,微调器本身和一对按钮,要么选择微调器选项,要么取消选择和ArrayAdapter构造函数上请求的textview。不知道为什么,我看到的每个教程,ArrayAdapter构造函数都询问上下文,Spinner ID和内容,但是对于我得到的,请求Context,TextViewID和内容

    package com.aguahillo.sislec;

    import org.ksoap2.serialization.SoapObject;

    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.Spinner;
    import android.view.View;
    import android.view.View.OnClickListener;

    public class FormaLibrosDisponibles
    {
        SisLec sisLec;
        private Spinner librosDisponibles;
        private ArrayAdapter<String> elementosArrayAdapter;
        private Button selecBtn;
        private Button cancelarBtn;
        private OnClickListener selecClk;
        private OnClickListener cancelarClk;
        private LinearLayout disenio;

        public FormaLibrosDisponibles(SisLec _sisLec)
        {
            sisLec = _sisLec;
            disenio = (LinearLayout) View.inflate(sisLec, R.layout.librosdisponibles, null);
            selecClk = new OnClickListener() 
            {           
                public void onClick(View v) 
                {           
                    seleccionarBtnClick(v);
                }
            };
            cancelarClk = new OnClickListener()
            {           
                public void onClick(View v) 
                {
                    cancelarBtnClick(v);
                }
            };
            selecBtn = (Button) disenio.findViewById(R.id.seleccionarBtn);
            selecBtn.setOnClickListener(selecClk);
            cancelarBtn = (Button) disenio.findViewById(R.id.cancelarBtn);
            cancelarBtn.setOnClickListener(cancelarClk);
        }

        public void mostrar()
        {
            sisLec.setContentView(R.layout.librosdisponibles);
        }

        public void enlistarLibros() throws Exception
        {       
            sisLec.cuerpoRespuesta = sisLec.serviciosWeb.obtenerRutas(sisLec.usuario, sisLec.contrasena);
            if(sisLec.cuerpoRespuesta.getPropertyCount() > 0)
            {
                sisLec.tabla = (SoapObject) sisLec.cuerpoRespuesta.getProperty(0);
            ArrayAdapter<String> elementosArrayAdapter = new ArrayAdapter<String>(sisLec, android.R.layout.simple_spinner_item);
            elementosArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            librosDisponibles = (Spinner) disenio.findViewById(R.id.librosDispSpinner);
            librosDisponibles.setAdapter(elementosArrayAdapter);
                for(int i = 0; i < sisLec.tabla.getPropertyCount(); i++)
                {
                    sisLec.lineaTabla = (SoapObject) sisLec.tabla.getProperty(i);
                    if(sisLec.lineaTabla.getProperty("RUTA").toString().equals("0"))
                    {
                        sisLec.mensaje("No hay libros asginados");
                        return;
                    }
elementosArrayAdapter.add(obtenerElemento(sisLec.lineaTabla.getProperty("RUTA").toString(), sisLec.lineaTabla.getProperty("ITINERARIO").toString(), sisLec.lineaTabla.getProperty("ESTADO").toString()));
                }
            librosDisponibles.setSelection(0);
            }
            else
            {           
                sisLec.frmLogin.mostrar();
                sisLec.mensaje("No hay libros disponibles por el momento");
            }
        }

        /**
         * Función sencilla para crear las entradas de la lista de libros disponibles
         * y sus estados
         */
        private String obtenerElemento(String ruta, String itinerario, String estado)
        {
            if (estado.equals("ERI03")) 
                return "Ruta " + ruta + " Itin " + itinerario + " Env";
            else if (estado.equals("ERI04"))
                return "Ruta " + ruta + " Itin " + itinerario + " Proc";
            else if (estado.equals("ERI06"))
                return "Ruta " + ruta + " Itin " + itinerario + " Sobr";
            else if (estado.equals("ERI07"))
                return "Ruta " + ruta + " Itin " + itinerario + " Sobr Proc";
            return "";            
        }

        public void seleccionarBtnClick(View v)
        {
            sisLec.mensaje("Se selecciono: " + Integer.toString(librosDisponibles.getSelectedItemPosition()) + " " + librosDisponibles.getSelectedItem().toString());
        }

        public void cancelarBtnClick(View v)
        {
            sisLec.frmLogin.mostrar();
        }
    }

代码只是从webService中提取信息,将其存储在String数组中,然后传递给ArrayAdapter以将其设置为Spinner。

这是我得到的错误(直接来自LogCat

> 11-16 18:45:58.859: E/AndroidRuntime(700): FATAL EXCEPTION: main 11-16
> 18:45:58.859: E/AndroidRuntime(700):
> android.content.res.Resources$NotFoundException: Resource ID
> #0x7f050007 type #0x12 is not valid 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.content.res.Resources.getLayout(Resources.java:731) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.view.LayoutInflater.inflate(LayoutInflater.java:318) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.view.View.measure(View.java:8171) 11-16 18:45:58.859:
> E/AndroidRuntime(700):    at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.view.View.measure(View.java:8171) 11-16 18:45:58.859:
> E/AndroidRuntime(700):    at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.view.View.measure(View.java:8171) 11-16 18:45:58.859:
> E/AndroidRuntime(700):    at
> android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.view.View.measure(View.java:8171) 11-16 18:45:58.859:
> E/AndroidRuntime(700):    at
> android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.view.View.measure(View.java:8171) 11-16 18:45:58.859:
> E/AndroidRuntime(700):    at
> android.view.ViewRoot.performTraversals(ViewRoot.java:801) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.os.Handler.dispatchMessage(Handler.java:99) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> android.os.Looper.loop(Looper.java:123) 11-16 18:45:58.859:
> E/AndroidRuntime(700):    at
> android.app.ActivityThread.main(ActivityThread.java:4627) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> java.lang.reflect.Method.invokeNative(Native Method) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> java.lang.reflect.Method.invoke(Method.java:521) 11-16 18:45:58.859:
> E/AndroidRuntime(700):    at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
> 11-16 18:45:58.859: E/AndroidRuntime(700):    at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-16
> 18:45:58.859: E/AndroidRuntime(700):  at
> dalvik.system.NativeStart.main(Native Method)

希望你能帮助我,因为我在互联网上找到的一切都没有帮助我

编辑1:嗯,我至少设法显示现在的布局,但现在它向我展示了旋转器空,以及bouth按钮什么都不做。

1 个答案:

答案 0 :(得分:1)

这是我的微调器标签的样子:

<Spinner 
  android:id="@+id/my_transtype_spinner"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:drawSelectorOnTop="true"
  android:textSize="12sp"
  android:prompt="Choose a value" />

然后您的类需要扩展Android API Activity类。 在onCreate中,执行以下操作:

// spinner boxes code
spnrMySpinner = (Spinner) findViewById(R.id.my_transtype_spinner);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.trans_types, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spnrMySpinner.setAdapter(adapter);