The RecyclerView
has components with text, I need that by clicking on a component that has text for example "Chess", the DialogFragment
opens and the text "Chess" from the RecyclerView
component is transferred to the TextView
which is inside this DialogFragment
.
To get a key from my item
layout file, I write this:
Bundle bundle = new Bundle();
bundle.putString("item", String.valueOf(dialogSRS));
dialogSRS.setArguments(bundle);
But what needs to be written in the DialogFragment
so that our text is transferred there?
I am writing the following in my DialogFragment
but it doesn't work
public class DialogSkatingRinkSchedule extends DialogFragment{
TextView number_srs_2;
TextView start_srs_2;
TextView end_srs_2;
TextView cost_srs_2_1;
TextView cost_srs_2_2;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_skatingrinkschedule, container, false);
number_srs_2 = view.findViewById(R.id.number_srs_2);
start_srs_2 = view.findViewById(R.id.start_srs_2);
end_srs_2 = view.findViewById(R.id.end_srs_2);
cost_srs_2_1 = view.findViewById(R.id.cost_srs_2_1);
cost_srs_2_2 = view.findViewById(R.id.cost_srs_2_2);
Bundle getArguments = new Bundle();
number_srs_2 = (TextView) getArguments.getSerializable("item");
return view;
}
}
My adapter
public class AdapterSkatingRinkSchedule extends RecyclerView.Adapter<AdapterSkatingRinkSchedule.SkatingRinkScheduleViewHolder> {
public ArrayList<ItemSkatingRinkSchedule> mSkatingRinkScheduleList;
public List<ItemSkatingRinkSchedule> mSearch;
private AppCompatActivity mActivity;
public AdapterSkatingRinkSchedule(ArrayList<ItemSkatingRinkSchedule> SkatingRinkScheduleList) {
mSkatingRinkScheduleList = SkatingRinkScheduleList;
mSearch = new ArrayList(mSkatingRinkScheduleList);
}
@NonNull
@Override
public SkatingRinkScheduleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_srs, parent, false);
return new SkatingRinkScheduleViewHolder(v);
}
public static class SkatingRinkScheduleViewHolder extends RecyclerView.ViewHolder {
public TextView number_srs, start_time_srs, end_time_srs, cost_1_srs, cost_2_srs;
public TextView number_srs_2;
public LinearLayout ll_main;
public SkatingRinkScheduleViewHolder(View itemView) {
super(itemView);
number_srs = itemView.findViewById(R.id.number_srs);
start_time_srs = itemView.findViewById(R.id.start_time_srs);
end_time_srs = itemView.findViewById(R.id.end_time_srs);
cost_1_srs = itemView.findViewById(R.id.cost_1_srs);
cost_2_srs = itemView.findViewById(R.id.cost_2_srs);
ll_main = itemView.findViewById(R.id.ll_main);
}
}
@SuppressLint("LongLogTag")
@Override
public void onBindViewHolder(SkatingRinkScheduleViewHolder holder, int position) {
ItemSkatingRinkSchedule currentItem = mSkatingRinkScheduleList.get(position);
holder.number_srs.setText(currentItem.get_session_number());
holder.start_time_srs.setText(currentItem.get_session_start());
holder.end_time_srs.setText(currentItem.get_session_end());
holder.cost_1_srs.setText(currentItem.get_weekdays());
holder.cost_2_srs.setText(currentItem.get_weekends_and_holidays());
holder.ll_main.setOnClickListener(v -> {
DialogSkatingRinkSchedule dialogSRS = new DialogSkatingRinkSchedule();
dialogSRS.show(((AppCompatActivity) v.getContext()).getSupportFragmentManager(), "item");
Bundle bundle = new Bundle();
bundle.putString("item", String.valueOf(dialogSRS));
dialogSRS.setArguments(bundle);
});
holder.getAdapterPosition();
}
@Override
public int getItemCount() {
return mSkatingRinkScheduleList.size();
}
}
You can access the arguments Bundle
of a Fragment
or DialogFragment
by calling the method getArguments()
. Since you put a String
there previously by writing
bundle.putString("item", String.valueOf(dialogSRS));
you should retrieve the value by using getString("item")
. Then you can display the text from the Bundle with the TextView
as follows:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_skatingrinkschedule, container, false);
number_srs_2 = view.findViewById(R.id.number_srs_2);
// assign other Views like before
Bundle args = getArguments();
String myText = args.getString("item");
number_srs_2.setText(myText);
return view;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.