简体   繁体   English

微调器ArrayAdapter使用自定义布局崩溃

[英]Spinner ArrayAdapter crashing with custom layout

I am trying to experiment with a custom Spinner / ArrayAdapter. 我正在尝试使用自定义Spinner / ArrayAdapter。 Following is my code: 以下是我的代码:

    myspinner=(Spinner)findViewById(R.id.myspinner);
    myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);
    myadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    myspinner.setOnItemSelectedListener(this);
    myspinner.setAdapter(myadapter);

Following is the custom layout mytextview that contains mytv: 以下是包含mytv的自定义布局mytextview:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/mylayout" android:layout_width="match_parent" android:layout_height="wrap_content">
        <View android:layout_width="match_parent" android:layout_height="15dip" android:id="@+id/myview1" android:background="@color/bordertop"></View>
        <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="30dip" android:id="@+id/mytv"></TextView>
        <View android:layout_width="match_parent" android:layout_height="15dip" android:id="@+id/myview2" android:background="@color/borderbottom"></View>
    </LinearLayout>

When i'm press the spinner to bring the popup, Android crashes. 当我按下微调框以弹出窗口时,Android崩溃。 The came logic works perfectly fine, when I comment out the setDropDownViewResource method. 当我注释掉setDropDownViewResource方法时,逻辑逻辑就可以正常工作。 I still don't understand what view setDropDownViewResource actually sets. 我仍然不明白setDropDownViewResource实际上设置了什么视图。 Can anyone show me how it looks like by default. 谁能告诉我默认情况下的样子。 And, if one wants to override it with a custom layout - does the layout need to have a specific structure? 而且,如果要使用自定义布局覆盖它-布局是否需要具有特定的结构?

here is what i have typically used for spinners: 这是我通常用于微调器的内容:

Spinner distancespinner = (Spinner) findViewById(R.id.distance);
ArrayAdapter<CharSequence> distanceadapter = ArrayAdapter.createFromResource(
    this, R.array.distance, android.R.layout.simple_spinner_item);
distanceadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
distancespinner.setAdapter(distanceadapter);
distancespinner.setOnItemSelectedListener(new distanceSelector());

(obviously replace distance with whatever you want) (显然,将距离替换为您想要的任何东西)

<Spinner
    android:id="@+id/distance"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:prompt="@string/app_name"
    />

to me it looks like your problem is in this line : 对我来说,您的问题似乎在此行中:

myadapter=new ArrayAdapter<String>(this,R.layout.mytextview,R.id.mytv,sample_data);

but then i dont know, your ArrayAdapter is based on instead of as i have and as per Spinner "the android.R.layout.simple_spinner_item ID references a layout for the standard spinner appearance, defined by the platform. Then setDropDownViewResource(int) is called to define the appearance for each item when the widget is opened (simple_spinner_dropdown_item is another standard layout defined by the platform)." 但是然后我不知道,您的ArrayAdapter并不是基于我的,而是基于Spinner的: “ android.R.layout.simple_spinner_item ID引用了平台定义的标准Spinner外观的布局。然后setDropDownViewResource(int)是调用以定义小部件打开时每个项目的外观(simple_spinner_dropdown_item是平台定义的另一种标准布局)。”

i think if you want to customize your spinner, .setDropDownViewResource(); 我认为如果您想自定义您的微调器,请使用.setDropDownViewResource(); is where to do it. 是在哪里做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM