-1

我有一个适用于 ListView 的类 Area 的适配器,但是当我想将此适配器用于微调器时,我得到一个错误。当我使用微调器进行活动时,它会显示列表的第一项,但是当我单击箭头以显示其他项目时,我也会收到错误消息。有人可以告诉我我做错了什么吗?

适配器类:

public class AreaAdapter extends ArrayAdapter<Area> {

    List<Area> areaList;

    public AreaAdapter(Context context, int textViewResouceId,
            List<Area> objects) {
        super(context, textViewResouceId, objects);
        areaList = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            Context c = getContext();
            LayoutInflater inflater = (LayoutInflater) c
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.activity_choose_area_adapter, null);
        }

        Area area = areaList.get(position);

        if (area != null) {
            TextView v = null;

            v = (TextView) convertView.findViewById(R.id.tareaname);
            v.setText(area.getAreaName());
        }

        return convertView;
    }
}

适配器的 XML:

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

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.72"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tareaname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

</LinearLayout>

包含微调器的活动:

public class ExtrasAddItemActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_extras_add_item);

        ActivitiesObjects ao = (ActivitiesObjects) this.getApplication();
        List<Area> listOfAreas = ao.getListOfAreas();

        Spinner spinnerArea = (Spinner)findViewById(R.id.spinnerExtraArea);
        AreaAdapter areaAdapter = new AreaAdapter(this,R.layout.activity_choose_area_adapter, listOfAreas);
        spinnerArea.setAdapter(areaAdapter);
    }
}

带有微调器的 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" >

    // other widgets ...

    <Spinner
        android:id="@+id/spinnerExtraArea"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

错误日志:

09-05 13:36:05.347: D/dalvikvm(274): GC_EXTERNAL_ALLOC freed 813 objects / 59328 bytes in 54ms
09-05 13:36:10.487: W/KeyCharacterMap(274): No keyboard for id 0
09-05 13:36:10.487: W/KeyCharacterMap(274): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-05 13:36:13.058: E/ArrayAdapter(274): You must supply a resource ID for a TextView
09-05 13:36:13.058: D/AndroidRuntime(274): Shutting down VM
09-05 13:36:13.068: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-05 13:36:13.101: E/AndroidRuntime(274): FATAL EXCEPTION: main
09-05 13:36:13.101: E/AndroidRuntime(274): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:347)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.ArrayAdapter.getDropDownView(ArrayAdapter.java:376)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:329)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:325)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.AbsListView.obtainView(AbsListView.java:1315)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.ListView.onMeasure(ListView.java:1109)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
09-05 13:36:13.101: E/AndroidRuntime(274):  at com.android.internal.widget.WeightedLinearLayout.onMeasure(WeightedLinearLayout.java:60)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.View.measure(View.java:8171)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.os.Looper.loop(Looper.java:123)
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 13:36:13.101: E/AndroidRuntime(274):  at java.lang.reflect.Method.invokeNative(Native Method)
09-05 13:36:13.101: E/AndroidRuntime(274):  at java.lang.reflect.Method.invoke(Method.java:521)
09-05 13:36:13.101: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 13:36:13.101: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 13:36:13.101: E/AndroidRuntime(274):  at dalvik.system.NativeStart.main(Native Method)
09-05 13:36:13.101: E/AndroidRuntime(274): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
09-05 13:36:13.101: E/AndroidRuntime(274):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:340)
09-05 13:36:13.101: E/AndroidRuntime(274):  ... 34 more

提前致谢, 亚历克斯

4

1 回答 1

1

问题在于 XML forR.layout.activity_choose_area_adapter不是文本视图:适配器很好,但 Spinner 明确要求它是文本视图,而不是复杂视图。如果你想要一个复杂的视图,你必须自己复制 Spinner 的行为。换成R.layout.activity_choose_area_adapter类似的东西android.R.layout.simple_list_item_1,你应该没问题。

Spinner spinnerArea = (Spinner)findViewById(R.id.spinnerExtraArea);
AreaAdapter areaAdapter = new AreaAdapter(this,android.R.layout.simple_list_item_1, listOfAreas);
spinnerArea.setAdapter(areaAdapter);

您可能还需要修改Adapter以使用传递的资源,而不是硬连线它。

于 2012-09-05T14:16:58.230 回答