Spinner ArrayAdapter crashing with custom layout

2019-04-18 00:36发布

I am trying to experiment with a custom 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:

<?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. The came logic works perfectly fine, when I comment out the setDropDownViewResource method. I still don't understand what view setDropDownViewResource actually sets. 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?

1条回答
再贱就再见
2楼-- · 2019-04-18 01:05

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)."

i think if you want to customize your spinner, .setDropDownViewResource(); is where to do it.

查看更多
登录 后发表回答