前提・実現したいこと
以下のリンク先のソースをもとにカレンダーを作成しています。
https://qiita.com/Sab_swiftlin/items/0993c489e7ef1c0f969d
日付を押下したときにフラグメントで画面が表示されるようにしたいと思っています。
そのため、Calender_cell.xmlとCalendarAdapter.javaを以下のように書き換えました。
ソースコード
CalendarAdapter.java (一部抜粋)
Java
1public class CalendarAdapter extends BaseAdapter { 2 private List<Date> dateArary = new ArrayList<>(); 3 private Context mContext; 4 private DateManeger mDateManager; 5 private LayoutInflater mLayoutInflater; 6 7 //カスタムセルを拡張したらここでWigetを定義 8 private static class ViewHolder{ 9 public TextView dateText; 10 public Button dateButton; 11 } 12 13 public CalendarAdapter(Context context){ 14 mContext = context; 15 mLayoutInflater = LayoutInflater.from(mContext); 16 mDateManager = new DateManeger(); 17 dateArary = mDateManager.getDays(); 18 } 19 20 @Override 21 public int getCount(){ 22 return dateArary.size(); 23 } 24 25 @Override 26 public View getView(int position, View convertView, ViewGroup parent){ 27 ViewHolder holder; 28 if(convertView == null){ 29 convertView = mLayoutInflater.inflate(R.layout.calender_cell,null); 30 holder = new ViewHolder(); 31 holder.dateText = convertView.findViewById(R.id.dateText); 32 holder.dateButton = convertView.findViewById(R.id.cellButton); 33 convertView.setTag(holder); 34 }else{ 35 holder = (ViewHolder)convertView.getTag(); 36 } 37 38 //セルのサイズを指定 39 float dp = mContext.getResources().getDisplayMetrics().density; 40 AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp, (parent.getHeight()-(int)dp* mDateManager.getWeeks())/mDateManager.getWeeks()); 41 convertView.setLayoutParams(params); 42 43 //日付のみ表示させる 44 SimpleDateFormat dateFormat = new SimpleDateFormat("dd", Locale.US); 45 holder.dateText.setText(dateFormat.format(dateArary.get(position))); 46 47 //当月以外乗せるをグレーアウト 48 if(mDateManager.isCurrentMonth(dateArary.get(position))){ 49 convertView.setBackgroundColor(Color.WHITE); 50 }else{ 51 convertView.setBackgroundColor(Color.LTGRAY); 52 } 53 54 //日曜日を赤、土曜日を青に 55 int coloId; 56 switch (mDateManager.getDayOfWeek(dateArary.get(position))){ 57 case 1: 58 coloId = Color.RED; 59 break; 60 case 7: 61 coloId = Color.BLUE; 62 break; 63 default: 64 coloId = Color.BLACK; 65 break; 66 67/*******************追記部分↓********************/ 68 holder.dateButton.setOnClickListener(v -> { 69 System.out.println("1"); 70 FragmentManager fragmentManager = getSupportFragmentManager(); //エラーが発生する箇所 71 FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 72 73 // BackStackを設定 74 fragmentTransaction.addToBackStack(null); 75 76 // パラメータを設定 77 fragmentTransaction.replace(R.id.container,DateScheduleFragment.newInstance("Fragment")); 78 fragmentTransaction.commit(); 79 }); 80/********************追記部分↑********************/ 81 82 holder.dateText.setTextColor(coloId); 83 84 return convertView; 85 }
calender_cell.xml
XML
1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TextView 7 android:id="@+id/dateText" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:textAlignment="center" /> 11 12 <Button 13 android:id="@+id/cellButton" 14 android:layout_width="match_parent" 15 android:layout_height="match_parent" 16 android:background="@drawable/clear_button_bg" /> 17 18</RelativeLayout>
Fragmentのソースは以下のように作成しました。
DateScheduleFragment.java(package名省略)
Java
1import android.app.Fragment; 2import android.os.Bundle; 3import android.view.LayoutInflater; 4import android.view.View; 5import android.view.ViewGroup; 6import android.widget.TextView; 7 8import androidx.annotation.NonNull; 9 10public class DateScheduleFragment extends Fragment { 11 12 public static DateScheduleFragment newInstance(String str){ 13 // Fragemnt01 インスタンス生成 14 DateScheduleFragment fragment = new DateScheduleFragment (); 15 // Bundle にパラメータを設定 16 Bundle barg = new Bundle(); 17 barg.putString("Message", str); 18 fragment.setArguments(barg); 19 20 return fragment; 21 } 22 23 // FragmentのViewを生成して返す 24 @Override 25 public View onCreateView(@NonNull LayoutInflater inflater, 26 ViewGroup container, 27 Bundle savedInstanceState) { 28 29 return inflater.inflate(R.layout.fragment_date_schedule, 30 container, false); 31 } 32 33 @Override 34 public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { 35 super.onViewCreated(view, savedInstanceState); 36 37 Bundle args = getArguments(); 38 if(args != null ){ 39 String str = args.getString("Message"); 40 TextView textView = view.findViewById(R.id.text_fragment); 41 textView.setText(str); 42 } 43 } 44 45}
fragment_date_schedule.xml
XML
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <TextView 8 android:id="@+id/text_fragment" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:textSize="30sp" 12 tools:ignore="MissingConstraints" /> 13 14</androidx.constraintlayout.widget.ConstraintLayout>
発生している問題・エラーメッセージ
上記の状態で実行してみると、CalendarAdapter.java の getSupportFragmentManager()実行時にエラーが出ているようでした。
※エラーの内容は文字化けして読めませんでした。
どのように記載すればAdapterからFragmentが呼び出せるのかご教授願います。
あまり詳しくないので、質問の内容で分からないことや説明が足りないところが
あるかもしれませんが宜しくお願いいたします。
※自分で調べたところ、getSupportFragmentManager() がActivityでは呼び出せて、
Adapterの中では呼び出すことができなさそうなので、別のメソッドを利用する必要が
あるのかなと考えています。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/01/25 12:40