如何将文本从item导入dialogfragment textview?

k4emjkb1  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(336)

这个 RecyclerView 有文本的组件,我需要通过点击一个有文本的组件,例如“chess”,这个 DialogFragment 打开并从 RecyclerView 组件被传输到 TextView 就在这里面 DialogFragment .
从我的车里拿钥匙 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();
    }
}
eblbsuwk

eblbsuwk1#

您可以访问参数 BundleFragment 或者 DialogFragment 通过调用方法 getArguments() . 既然你放了一个 String 以前写过
bundle.putstring(“item”,string.valueof(dialogsrs));
您应该使用 getString("item") . 然后您可以使用 TextView 具体如下:

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;
}

相关问题