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