Spinner ArrayAdapter crashing with custom layout

痴心易碎 提交于 2019-12-04 21:31:29

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!