###前提・実現したいこと
現在作っているアプリで、ダイアログの中にスピナーを設置しました。
しかし、OnItemSelectedリスナーを設定したのにも関わらず、項目をタッチしたときこのメソッドが呼ばれませんでした。
どのようにすれば正しく反応してくれるのでしょうか。
OnResume()の時の一回だけは反応します。(無効化済み)
足りない点などがありましたら修正・追加します
###ソースコード
少し長くなってしまいました。
MainActivity.java
java
1package com.example.spinnertest; 2 3//import省略 4 5public class MainActivity extends AppCompatActivity { 6 public CustomizedDialog_questionbook customizedDialog_questionbook; 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 13 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 14 customizedDialog_questionbook = new CustomizedDialog_questionbook(); 15 fab.setOnClickListener(new View.OnClickListener() { 16 17 @Override 18 public void onClick(View view) { 19 customizedDialog_questionbook.show(getFragmentManager(), "dialog_fragment"); 20 } 21 }); 22 } 23 24} 25 26
CustomizedDialog_questionbook.java
java
1package com.example.spinnertest; 2 3//import省略 4 5public class CustomizedDialog_questionbook extends DialogFragment { 6 private Spinner tagSpinner; 7 private ArrayAdapter<String> tagSpinnerAdapter; 8 private Dialog dialog; 9 public DialogListener dialogListener; 10 @Override 11 public void onViewCreated(View view, Bundle savedInstanceState) { 12 super.onViewCreated(view, savedInstanceState); 13 14 view.findViewById(R.id.ok_Button).setOnClickListener(new View.OnClickListener() { 15 @Override 16 public void onClick(View v) { 17 if (dialogListener != null) { 18 dialogListener.onClickOk(); 19 } 20 } 21 }); 22 } 23 24 @Override 25 public void onAttach(Context context) { 26 super.onAttach(context); 27 if (context instanceof DialogListener) { 28 dialogListener = (DialogListener) context; 29 } 30 } 31 32 @Override 33 public void onDetach() { 34 super.onDetach(); 35 dialogListener = null; 36 } 37 38 @Override 39 public Dialog onCreateDialog(final Bundle savedInstanceState) { 40 LayoutInflater inflater = getActivity().getLayoutInflater(); 41 View view = inflater.inflate(R.layout.inputdialog, null, false); 42 43 tagSpinnerAdapter = new ArrayAdapter<>(MyApplication.getAppContext(), android.R.layout.simple_spinner_item); 44 tagSpinnerAdapter.add("【新規作成】"); 45 tagSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 46 47 tagSpinner = (Spinner) view.findViewById(R.id.tagSpinner); 48 tagSpinner.setAdapter(tagSpinnerAdapter); 49 tagSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 50 @Override 51 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 52 53 if (tagSpinner.isFocusable() == false) {//OnResume()の時かどうか判定 54 tagSpinner.setFocusable(true); 55 Log.d("onqbook","初回起動"); //←呼ばれる 56 } else { 57 if (position == 0) { 58 //ダイアログ作成 59 Log.d("onqbook","ダイアログ作成"); //←呼ばれない!!! 60 } else { 61 Log.d("onqbook","ファイルに書き込み");//←呼ばれなくて良い 62 } 63 } 64 } 65 @Override 66 public void onNothingSelected(AdapterView<?> parent) { 67 // do nothing 68 } 69 }); 70 tagSpinner.setSelection(0,false); 71 tagSpinnerAdapter.notifyDataSetChanged(); 72 tagSpinner.setFocusable(false); 73 74 Button okButton = (Button) view.findViewById(R.id.ok_Button); 75 Button closeButton = (Button) view.findViewById(R.id.close_Button); 76 dialog = new Dialog(getActivity()); 77 dialog.setTitle("編集画面"); 78 dialog.setContentView(view); 79 80 okButton.setOnClickListener(new View.OnClickListener(){ 81 public void onClick(View v){ 82 // do something 83 dialog.dismiss(); 84 } 85 }); 86 closeButton.setOnClickListener(new View.OnClickListener(){ 87 public void onClick(View v){ 88 dialog.dismiss(); 89 } 90 }); 91 return dialog; 92 } 93 @Override 94 public void onActivityCreated(Bundle savedInstansState){ 95 super.onActivityCreated(savedInstansState); 96 WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); 97 DisplayMetrics metrics = getResources().getDisplayMetrics(); 98 lp.width = (int) (metrics.widthPixels*1.0); 99 dialog.getWindow().setAttributes(lp); 100 } 101 102}
MyApplication.java
java
1package com.example.spinnertest; 2 3import android.app.Application; 4import android.content.Context; 5 6public class MyApplication extends Application { 7 public static Context context; 8 9 public void onCreate(){ 10 super.onCreate(); 11 MyApplication.context = getApplicationContext(); 12 } 13 14 public static Context getAppContext() { 15 return MyApplication.context; 16 } 17} 18
DialogListener.java(インターフェース)
java
1package com.example.spinnertest; 2 3 4 interface DialogListener { 5void onClickOk(); 6} 7
activity_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.spinnertest.MainActivity"> 8 9 <android.support.design.widget.AppBarLayout 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:theme="@style/AppTheme.AppBarOverlay"> 13 14 </android.support.design.widget.AppBarLayout> 15 16 <include layout="@layout/content_main" /> 17 18 <android.support.design.widget.FloatingActionButton 19 android:id="@+id/fab" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_gravity="bottom|end" 23 android:layout_margin="@dimen/fab_margin" 24 app:srcCompat="@android:drawable/ic_dialog_email" /> 25 26</android.support.design.widget.CoordinatorLayout> 27
content_main.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 app:layout_behavior="@string/appbar_scrolling_view_behavior" 8 tools:context="com.example.spinnertest.MainActivity" 9 tools:showIn="@layout/activity_main"> 10 11 12 13</android.support.constraint.ConstraintLayout>
inputdialog.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:id="@+id/customDialog" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:weightSum="1"> 8 9 10 <Spinner 11 android:id="@+id/tagSpinner" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:layout_weight="0.10" 15 /> 16 17 <LinearLayout 18 android:layout_weight="0.60" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content"> 21 22 <Button 23 android:id="@+id/close_Button" 24 style="?android:attr/buttonBarButtonStyle" 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" 27 android:layout_weight="1" 28 android:text="close" /> 29 30 <Button 31 style="?android:attr/buttonBarButtonStyle" 32 android:id="@+id/ok_Button" 33 android:layout_width="match_parent" 34 android:layout_height="match_parent" 35 android:layout_weight="1" 36 android:text="Ok" /> 37 </LinearLayout> 38</LinearLayout>
###試したこと
spinner.setselection(0, false)
arrayadapter.notifyDataSetChanged()
###補足情報
Android Studio 2.3.3
実機:Android 6.0.0
###追記
修正版 CustomizedDialog_questionbook.java
java
1tagSpinner.setAdapter(tagSpinnerAdapter); 2tagSpinner.setSelection(0,false); 3 tagSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 4 //略 5 }); 6 tagSpinnerAdapter.notifyDataSetChanged(); 7 //tagSpinner.setFocusable(false);
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。