0

I would like to ask if there's a way to get all the current value of EditText from RecyclerView Adapter back to Main Activity. enter image description here

The Update button is coupled in the activity while the list of schedules are on the RecyclerView Adapter. The user can update the schedule and I would like to get all the edittext value in array when user submit the button.

Here's my Activity Code

public class ClinicScheduleListActivity extends BaseActivity implements 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");
    }
}

Here's my Adapter Code

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

vidalbenjoe
  • 921
  • 13
  • 37

2 Answers2

-1

You can set up either a BroadcastReceiver to receive the data or push the data through the intent used to start up the mainActivity!

Simply grab the data from the recyclerView adapter, push the data into an intent and push the intent to your activity with either sendBroadcast(intent) or startActivity(intentWithData)

You may need to overwrite the onNewIntent method for your activity if you haven’t closed it

Brandon
  • 1,158
  • 3
  • 12
  • 22
-1

OnEditTextChanged Interface

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

Then you need too include this in the constructor of the adapter.

In the 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) {}
    });
}

and call adapter in your activity like this

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