在RecyclerView Adapter中将所有Edittext值重新获取回Main Activity

时间:2019-11-05 13:25:24

标签: android android-recyclerview

我想问一下是否有一种方法可以将RecyclerView Adapter的所有EditText当前值返回到Main Activity。 enter image description here

在日程表列表位于RecyclerView适配器上的同时,“更新”按钮与活动结合在一起。用户可以更新时间表,当用户提交按钮时,我想获取数组中的所有edittext值。

这是我的活动代码

公共类ClinicScheduleListActivity扩展了BaseActivity实现的UpdateClinicListener {

    @BindView(R.id.clinicScheduleToolbar)
    Toolbar clinicScheduleToolbar;

    @BindView(R.id.clinicScheduleRecyclerView)
    RecyclerView clinicScheduleRecyclerView;


    @BindView(R.id.updateClinicScheduleBt)
    Button updateClinicScheduleBt;

    ClinicScheduleAdapter clinicScheduleAdapter;
    ClinicResult clinicResult;

    ClinicPresenterImpl clinicPresenterImpl;

//    UpdateScheduleModel updateScheduleModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clinic_schedules_main_layout);
        ButterKnife.bind(this);

//        updateScheduleModel = new UpdateScheduleModel();

        setUpToolbar();
        clinicResult = Parcels.unwrap(getIntent().getParcelableExtra("clinic_schedules"));
        setUpView();

        if (clinicPresenterImpl == null) {
            clinicPresenterImpl = new ClinicPresenterImpl(this);
        }




    }



    void setUpView() {
        clinicScheduleAdapter = new ClinicScheduleAdapter(this, clinicResult, this);
        clinicScheduleRecyclerView.setAdapter(clinicScheduleAdapter);
        clinicScheduleAdapter.notifyDataSetChanged();

        Utilities.setUpRecyclerView(this, clinicScheduleRecyclerView);
    }

    void setUpToolbar() {
        setSupportActionBar(clinicScheduleToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setTitle("Clinic Schedule");
    }
}

这是我的适配器代码

public class ClinicScheduleAdapter extends RecyclerView.Adapter {
    ClinicScheduleListActivity mContext;
    List<ClinicSchedules> schedulesList;

    private static UpdateClinicListener mCallback;
    public static String day, startTime, endTime;
    static boolean isOpen;

    ClinicScheduleAdapterViewHolder scheduleAdapterViewHolder;
    static UpdateScheduleModel updateScheduleModel = new UpdateScheduleModel();



    public ClinicScheduleAdapter(ClinicScheduleListActivity context, ClinicResult clinicResult, UpdateClinicListener listener) {
        mContext = context;
        schedulesList = clinicResult.getSchedules();
        mCallback = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.clinic_schedules_item_layout, parent, false);
        scheduleAdapterViewHolder = new ClinicScheduleAdapterViewHolder(view);

        return scheduleAdapterViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        scheduleAdapterViewHolder = (ClinicScheduleAdapterViewHolder) holder;
        ClinicSchedules result = schedulesList.get(position);


        scheduleAdapterViewHolder.scheduleSwitchButton.setChecked(result.getIsOpen() == true ? true : false);

        scheduleAdapterViewHolder.dateTextView.setText(result.getDay());
        scheduleAdapterViewHolder.startDateEditText.setText(Utilities.formatTime(result.getStartTime()));
        scheduleAdapterViewHolder.endDateEditText.setText(Utilities.formatTime(result.getEndTime()));


        updateData();


        scheduleAdapterViewHolder.setIsRecyclable(true);


        scheduleAdapterViewHolder.bindData();


    }


    void updateData() {


        updateScheduleModel.setPrimaryClinic("sad");
        updateScheduleModel.setSecretaryName("Test");
        updateScheduleModel.setSecretaryContact("092832123");

        List<ClinicSchedules> schedulesAttribute = new ArrayList<>();

        for (int i = 0; i < schedulesList.size(); i++) {


            ClinicSchedules schedule = new ClinicSchedules();
            schedule.setId(i);
            schedule.setDay(scheduleAdapterViewHolder.dateTextView.getText().toString());
            schedule.setStartTime(scheduleAdapterViewHolder.startDateEditText.getText().toString());
            schedule.setEndTime( scheduleAdapterViewHolder.endDateEditText.getText().toString());
            schedule.setIsOpen(scheduleAdapterViewHolder.scheduleSwitchButton.isChecked() ? true : false);
            schedulesAttribute.add(schedule);
        }

        updateScheduleModel.setSchedulesAttributes(schedulesAttribute);


        Gson gson = new Gson();
        String j = gson.toJson(updateScheduleModel, UpdateScheduleModel.class);

    }




    @Override
    public int getItemCount() {
        return schedulesList.size();
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ClinicScheduleAdapterViewHolder extends RecyclerView.ViewHolder {
        LinearLayout schedLayout;
        Switch scheduleSwitchButton;
        TextView dateTextView;
        EditText startDateEditText, endDateEditText;

        public ClinicScheduleAdapterViewHolder(View v) {
            super(v);
            schedLayout = v.findViewById(R.id.schedLayout);
            scheduleSwitchButton = v.findViewById(R.id.scheduleSwitchButton);
            dateTextView = v.findViewById(R.id.dateTextView);
            startDateEditText = v.findViewById(R.id.startDateEditText);
            endDateEditText = v.findViewById(R.id.endDateEditText);

        }


        void bindData(){
            mContext.updateClinicScheduleBt.setOnClickListener(v -> {

                updateData();



            });
        }


    }
}

TIA

2 个答案:

答案 0 :(得分:-1)

您可以设置一个BroadcastReceiver来接收数据,也可以通过用于启动mainActivity的意图推送数据!

只需从recyclerView适配器中获取数据,将数据推送到意图中,然后使用以下任一方法将意图推动到您的活动中     sendBroadcast(intent)或startActivity(intentWithData)

如果您尚未关闭活动,则可能需要覆盖其onNewIntent方法

答案 1 :(得分:-1)

OnEditTextChanged接口

public interface OnEditTextChanged {
    void onTextChanged(int position, String charSeq);
}

然后,您也需要在适配器的构造函数中添加它。

在Adapter.java中

private List<DataHolder> mDataSet;
private OnEditTextChanged onEditTextChanged;

public Adapter(List<DataHolder> myData, OnEditTextChanged onEditTextChanged) {
    mDataSet = myData;
    this.onEditTextChanged = onEditTextChanged;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    holder.anameTxtView.setText(mDataSet.get(position).getDname());
    holder.abalanceTxtView.setText(mDataSet.get(position).getDbalance());

    holder.editTextLeft.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            onEditTextChanged.onTextChanged(position, charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {}
    });

      holder.editTextRight.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            onEditTextChanged.onTextChanged(position, charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {}
    });
}

并在您的活动中调用适配器

mAdapter = new Adapter(holderList, new OnEditTextChanged() {
        @Override
        public void onTextChanged(int position, String charSeq) {
            //change list data of particular position
        }
    });