スピナードロップダウンの最初の要素の高さをゼロに設定するために使用したカスタムアダプターを備えたスピナーがあります。これは、表示されていないため、ユーザーがクリックできないように、スピナー (最初の要素) にデフォルトのメッセージを表示するために行います。
package org.digitalhealthagency.elaj.util;
import java.util.ArrayList;
import org.digitalhealthagency.elaj.gui.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter implements SpinnerAdapter{
Context context;
int textViewResourceId;
ArrayList arrayList;
public CustomAdapter(Context context, int textViewResourceId, ArrayList arrayList) {
super(context, textViewResourceId, arrayList);
this.context = context;
this.textViewResourceId = textViewResourceId;
this.arrayList = arrayList;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//convertView = vi.inflate(android.R.layout.simple_spinner_dropdown_item, null);
convertView = vi.inflate(R.layout.spinner_item, null);
}
TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
textView.setText(arrayList.get(position).toString());//after changing from ArrayList<String> to ArrayList<Object>
if (position == 0) {
textView.setHeight(0);
}
else{
textView.setHeight(100);
}
return convertView;
}
}
のgetDropDownView
代わりに spinner_item.xml で膨らませようとしていandroid.R.layout.simple_spinner_dropdown_item
ます。私の spinner_item は次のようになります。
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:drawableLeft="?android:attr/listChoiceIndicatorSingle"
android:gravity="right|center_vertical"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingRight="10dp"
android:ellipsize="marquee" />
私のアクティビティコードでは、アダプターを次のように使用します。
CustomAdapter adapter = new CustomAdapter(this, R.layout.spinner, subSpecialities);
adapter.setDropDownViewResource(R.layout.spinner_item);
mSubSpecialtySpinner.setAdapter(adapter);
log cat に次のエラーが表示されます。
07-10 10:49:34.103: E/AndroidRuntime(518): FATAL EXCEPTION: main
07-10 10:49:34.103: E/AndroidRuntime(518): java.lang.NullPointerException
07-10 10:49:34.103: E/AndroidRuntime(518): at org.digitalhealthagency.elaj.util.CustomAdapter.getDropDownView(CustomAdapter.java:40)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.Spinner$DropDownAdapter.getDropDownView(Spinner.java:569)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.Spinner$DropDownAdapter.getView(Spinner.java:565)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.ListView.onMeasure(ListView.java:1155)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
07-10 10:49:34.103: E/AndroidRuntime(518): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.View.measure(View.java:12723)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.os.Looper.loop(Looper.java:137)
07-10 10:49:34.103: E/AndroidRuntime(518): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-10 10:49:34.103: E/AndroidRuntime(518): at java.lang.reflect.Method.invokeNative(Native Method)
07-10 10:49:34.103: E/AndroidRuntime(518): at java.lang.reflect.Method.invoke(Method.java:511)
07-10 10:49:34.103: E/AndroidRuntime(518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-10 10:49:34.103: E/AndroidRuntime(518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-10 10:49:34.103: E/AndroidRuntime(518): at dalvik.system.NativeStart.main(Native Method)
誰でも私を助けてもらえますか?