如何从“自定义”对话框获取数据到“活动”并将该数据传递给recyclerView

时间:2018-10-26 06:25:12

标签: android android-recyclerview recycler-adapter

如何从“定制对话框”中获取数据到Activity并将该数据传递到recyclerView

我尝试了以下链接,但没有成功, How can I pass values between a Dialog and an Activity?

下面是Activity

public class MutipleResourcesActivity extends AppCompatActivity implements View.OnClickListener, AddEachResourceDialog.OnMyDialogResult{

    RecyclerView list_resources;
    ImageView img_addResource;

    private ArrayList<AddResource> resources = new ArrayList<>();
    public AddResourceAdapter resourceAdapter;

    AddEachResourceDialog resourceDialog;

    String result1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.add_multiple_resources);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setContentInsetsRelative(0, toolbar.getContentInsetEnd());
        //toolbar.setContentInsetsAbsolute(0,0);
        setSupportActionBar(toolbar);
        toolbar.setTitle("Enter Timings");



        list_resources = (RecyclerView) findViewById(R.id.recyclerview_multiResources);
        img_addResource = (ImageView) findViewById(R.id.img_addResource);
        img_addResource.setOnClickListener(this);

    }

   private void setAdapter() {

    resourceAdapter = new AddResourceAdapter(resources);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    list_resources.setLayoutManager(mLayoutManager);
    list_resources.setItemAnimator(new DefaultItemAnimator());



    AddResource ar = new AddResource(mspinnerValue, mstartTime,mendTime);
    resources.add(ar);
    resourceAdapter.notifyDataSetChanged();

    list_resources.setAdapter(resourceAdapter);

    //resources.add(result1);
}


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.img_addResource:

                try {

                    AddEachResourceDialog addEachResource = new AddEachResourceDialog(this);
                    //addEachResource.show(getSupportFragmentManager(), "Dialog");
                    addEachResource.show();
                    addEachResource.setWindowSize();

                }catch (Exception e){
                    e.printStackTrace();
                }
                break;
        }

    }

    @Override
    public void finish(String spinnerValue, String startTime, String endTime) {

        System.out.println("####### spinnerValue : "+spinnerValue);
        System.out.println("####### startTime : "+startTime);
        System.out.println("####### endTime : "+endTime);

        setAdapter();
    }
}

下面是我的“自定义”对话框,其界面为“ OnMyDialogResult”

public class AddEachResourceDialog extends BaseDialog implements View.OnClickListener{

    private Context context;

    private static DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    private static DateFormat tf = new SimpleDateFormat("HH:mm");
    private Calendar startDateTime, finishDateTime;

    private DatePickerDialog startDatePicker, finishDatePicker;
    private TimePickerDialog startTimePicker, finishTimePicker;


    Spinner spinner_resources;
    EditText edt_startTime, edt_endTime;
    Button btn_save, btn_cancel;

    TextView tv_basicStartDate, tv_basicStartTime, tv_basicFinishDate, tv_basicFinishTime;

    OnMyDialogResult mDialogResult;

    AddResource ads = new AddResource();

    public AddEachResourceDialog(Context context) {
        super(context);

        this.context = context;
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            setContentView(R.layout.add_each_resource);

            setTitle("Add Timings");

            spinner_resources = (Spinner) findViewById(R.id.spinner_resources);

            tv_basicStartDate = (TextView) findViewById(R.id.txt_basicStartDate);
            tv_basicFinishDate = (TextView) findViewById(R.id.txt_basicFinishDate);
            tv_basicStartTime = (TextView) findViewById(R.id.txt_basicStartTime);
            tv_basicFinishTime = (TextView) findViewById(R.id.txt_basicFinishTime);

            tv_basicStartDate.setOnClickListener(this);
            tv_basicFinishDate.setOnClickListener(this);
            tv_basicStartTime.setOnClickListener(this);
            tv_basicFinishTime.setOnClickListener(this);

            startDateTime = (GregorianCalendar) GregorianCalendar.getInstance();
            finishDateTime = (GregorianCalendar) GregorianCalendar.getInstance();
            finishDateTime.add(Calendar.DAY_OF_MONTH, 1);

            tv_basicStartDate.setText(df.format(startDateTime.getTime()));
            tv_basicStartTime.setText(tf.format(startDateTime.getTime()));
            tv_basicFinishDate.setText(df.format(finishDateTime.getTime()));
            tv_basicFinishTime.setText(tf.format(finishDateTime.getTime()));


            btn_save = (Button) findViewById(R.id.saveBtn);
            btn_cancel = (Button) findViewById(R.id.cancelBtn);
            btn_save.setOnClickListener(this);
            btn_cancel.setOnClickListener(this);

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.saveBtn:


                    mDialogResult = new OnMyDialogResult() {
                        @Override
                        public void finish(String spinnerValue, String startTime, String endTime) {

                            ads.setName(String.valueOf(spinner_resources.getSelectedItem().toString()));
                            ads.setBasicStartDate(String.valueOf(tv_basicStartDate.getText().toString()));
                            ads.setBasicFinishDate(String.valueOf(tv_basicFinishDate.getText().toString()));

                            ads.setBasicStartTime(String.valueOf(tv_basicStartTime.getText().toString()));
                            ads.setBasicFinishTime(String.valueOf(tv_basicFinishTime.getText().toString()));


                            mDialogResult.finish(ads.getName(), ads.getBasicStartDate() +":"+ads.getBasicStartTime(), ads.getBasicFinishDate() +":"+ads.getBasicFinishTime());



                            mDialogResult.finish(ads.getName(), ads.getStartTime(), ads.getEndTime());

                        }
                    };                 

                AddEachResourceDialog.this.dismiss();

                break;

            case R.id.cancelBtn:
                //onBackPressed();
                AddEachResourceDialog.this.dismiss();


                break;

            default:
                break;
        }

    }

    public void setDialogResult(OnMyDialogResult dialogResult){
        mDialogResult = dialogResult;
    }

    public interface OnMyDialogResult{
        //void finish(AddEachResourceDialog result);
        void finish(String spinnerValue, String startTime, String endTime);
    }
}

没有将数据从Dialog传递到Activity,我必须将该数据传递到recyclerview。请帮我解决一下这个。预先感谢。

3 个答案:

答案 0 :(得分:0)

更改下一个代码:

public class AddEachResourceDialog extends BaseDialog implements View.OnClickListener{

    private Context context;

    private static DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
    private static DateFormat tf = new SimpleDateFormat("HH:mm");
    private Calendar startDateTime, finishDateTime;

    private DatePickerDialog startDatePicker, finishDatePicker;
    private TimePickerDialog startTimePicker, finishTimePicker;


    Spinner spinner_resources;
    EditText edt_startTime, edt_endTime;
    Button btn_save, btn_cancel;

    TextView tv_basicStartDate, tv_basicStartTime, tv_basicFinishDate, tv_basicFinishTime;

    OnMyDialogResult mDialogResult;

    AddResource ads = new AddResource();

    public AddEachResourceDialog(MutipleResourcesActivity activity) {
        super(activity);

        mDialogResult = activity;
    }




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            setContentView(R.layout.add_each_resource);

            setTitle("Add Timings");

            spinner_resources = (Spinner) findViewById(R.id.spinner_resources);

            tv_basicStartDate = (TextView) findViewById(R.id.txt_basicStartDate);
            tv_basicFinishDate = (TextView) findViewById(R.id.txt_basicFinishDate);
            tv_basicStartTime = (TextView) findViewById(R.id.txt_basicStartTime);
            tv_basicFinishTime = (TextView) findViewById(R.id.txt_basicFinishTime);

            tv_basicStartDate.setOnClickListener(this);
            tv_basicFinishDate.setOnClickListener(this);
            tv_basicStartTime.setOnClickListener(this);
            tv_basicFinishTime.setOnClickListener(this);

            startDateTime = (GregorianCalendar) GregorianCalendar.getInstance();
            finishDateTime = (GregorianCalendar) GregorianCalendar.getInstance();
            finishDateTime.add(Calendar.DAY_OF_MONTH, 1);

            tv_basicStartDate.setText(df.format(startDateTime.getTime()));
            tv_basicStartTime.setText(tf.format(startDateTime.getTime()));
            tv_basicFinishDate.setText(df.format(finishDateTime.getTime()));
            tv_basicFinishTime.setText(tf.format(finishDateTime.getTime()));


            btn_save = (Button) findViewById(R.id.saveBtn);
            btn_cancel = (Button) findViewById(R.id.cancelBtn);
            btn_save.setOnClickListener(this);
            btn_cancel.setOnClickListener(this);

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.saveBtn:

ads.setName(String.valueOf(spinner_resources.getSelectedItem().toString()));
                            ads.setBasicStartDate(String.valueOf(tv_basicStartDate.getText().toString()));
                            ads.setBasicFinishDate(String.valueOf(tv_basicFinishDate.getText().toString()));

                            ads.setBasicStartTime(String.valueOf(tv_basicStartTime.getText().toString()));
                            ads.setBasicFinishTime(String.valueOf(tv_basicFinishTime.getText().toString()));

//Which one you need?
                            mDialogResult.finish(ads.getName(), ads.getBasicStartDate() +":"+ads.getBasicStartTime(), ads.getBasicFinishDate() +":"+ads.getBasicFinishTime());

                            mDialogResult.finish(ads.getName(), ads.getStartTime(), ads.getEndTime());



                AddEachResourceDialog.this.dismiss();

                break;

            case R.id.cancelBtn:
                //onBackPressed();
                AddEachResourceDialog.this.dismiss();


                break;

            default:
                break;
        }

    }

    public void setDialogResult(OnMyDialogResult dialogResult){
        mDialogResult = dialogResult;
    }

    public interface OnMyDialogResult{
        //void finish(AddEachResourceDialog result);
        void finish(String spinnerValue, String startTime, String endTime);
    }
}

答案 1 :(得分:0)

更改对话框代码的以下部分,

    case R.id.saveBtn:

    ads.setName(String.valueOf(spinner_resources.getSelectedItem().toString()));
    ads.setBasicStartDate(String.valueOf(tv_basicStartDate.getText().toString()));
    ads.setBasicFinishDate(String.valueOf(tv_basicFinishDate.getText().toString()));
    ads.setBasicStartTime(String.valueOf(tv_basicStartTime.getText().toString()));
    ads.setBasicFinishTime(String.valueOf(tv_basicFinishTime.getText().toString()));

    //Use either this finish method or that one, i don't see why did you call 
    //finish() twice  
    mDialogResult.finish(ads.getName(), ads.getBasicStartDate() +":"+ads.getBasicStartTime(), ads.getBasicFinishDate() +":"+ads.getBasicFinishTime());

    mDialogResult.finish(ads.getName(), ads.getStartTime(), ads.getEndTime());


    AddEachResourceDialog.this.dismiss();
    break;

最后,在您的活动中,用此

替换onclick方法
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.img_addResource:

                try {

                    AddEachResourceDialog addEachResource = new AddEachResourceDialog(this);
                    //addEachResource.show(getSupportFragmentManager(), "Dialog");
                    addEachResource.setDialogResult(this);
                    addEachResource.show();
                    addEachResource.setWindowSize();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }

    }

你们都很好。

答案 2 :(得分:0)

多次讨论并仔细阅读代码后。我确定在自定义对话框中调用的.finish()方法不会在活动类中被覆盖。这就解释了为什么不传递数据。需要做的是获取活动的引用,并调用.finish()方法覆盖活动中的方法,而不是自定义对话框。

这将强制转换正在实现OnMyDialogResult接口的MutipleResourcesActivity中的初始上下文,并在活动中调用.finish的替代方法。请在自定义对话框类中使用它。 (如果当前上下文是实现OnMyDialogResult接口的活动,则可以进行强制转换)。

((AddEachResourceDialog.OnMyDialogResult) context).finish(ads.getName(), ads.getBasicStartDate() +":"+ads.getBasicStartTime(), ads.getBasicFinishDate() +":"+ads.getBasicFinishTime());