如何将文本从项目导入到 DialogFragment TextView?
How can I import text from item to DialogFragment TextView?
RecyclerView
包含带有文本的组件,我需要通过单击包含文本的组件(例如“国际象棋”),DialogFragment
打开并显示 [= 中的文本“国际象棋” 13=] 组件被转移到这个 DialogFragment
.
里面的 TextView
为了从我的 item
布局文件中获取密钥,我这样写:
Bundle bundle = new Bundle();
bundle.putString("item", String.valueOf(dialogSRS));
dialogSRS.setArguments(bundle);
但是 DialogFragment
中需要写些什么才能将我们的文本传输到那里?
我正在 DialogFragment
中写入以下内容,但它不起作用
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;
}
}
我的适配器
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();
}
}
您可以通过调用方法 getArguments()
访问 Fragment
或 DialogFragment
的参数 Bundle
。由于您之前通过编写
将 String
放在那里
bundle.putString("item", String.valueOf(dialogSRS));
您应该使用 getString("item")
检索值。然后您可以显示带有 TextView
的 Bundle 中的文本,如下所示:
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;
}
RecyclerView
包含带有文本的组件,我需要通过单击包含文本的组件(例如“国际象棋”),DialogFragment
打开并显示 [= 中的文本“国际象棋” 13=] 组件被转移到这个 DialogFragment
.
TextView
为了从我的 item
布局文件中获取密钥,我这样写:
Bundle bundle = new Bundle();
bundle.putString("item", String.valueOf(dialogSRS));
dialogSRS.setArguments(bundle);
但是 DialogFragment
中需要写些什么才能将我们的文本传输到那里?
我正在 DialogFragment
中写入以下内容,但它不起作用
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;
}
}
我的适配器
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();
}
}
您可以通过调用方法 getArguments()
访问 Fragment
或 DialogFragment
的参数 Bundle
。由于您之前通过编写
String
放在那里
bundle.putString("item", String.valueOf(dialogSRS));
您应该使用 getString("item")
检索值。然后您可以显示带有 TextView
的 Bundle 中的文本,如下所示:
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;
}