为什么datepicker的值在将值传递给其他活动时无法改变

时间:2018-04-25 12:11:59

标签: android datepicker

我创建了一个日期选择器,但是当我将该值传递给其他活动时,其他活动的值不会改变,尽管我更改了第一个活动的值。怎么解决这个?

我想将ListInstrumen.java中的值传递给ListPengecekan.java 这是我的代码

  

ListInstrumen.java

 mDisplayDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Calendar cal = Calendar.getInstance();
            final int year = cal.get(Calendar.YEAR);
            final int month = cal.get(Calendar.MONTH);
            final int day = cal.get(Calendar.DAY_OF_MONTH);

            DatePickerDialog dialog = new DatePickerDialog(ListInstrumen.this,android.R.style.Theme_Holo_InputMethod,
                    mDateSetListener,
                    year,month,day);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialog.show();

            lvInstrumen.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    int idInstrumen = (int) view.getTag();
                    Intent j = new Intent (ListInstrumen.this, ListPengecekan.class);
                    j.putExtra("day",day);
                    j.putExtra("month",month);
                    j.putExtra("year",year);
                    j.putExtra("id", String.valueOf(id));
                    j.putExtra("ins", ((TextView) view.findViewById(R.id.tv_instrumenn)).getText().toString());
                    startActivity(j);


                }
            });
        }
    });


    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month = month + 1;
            Log.d(TAG, "onDateSet: yyy/mm/dd: " + year + "-" + month  + "-" + day);

            String date = year + "-" + month  + "-" + day;
            mDisplayDate.setText(date);


        }
    };
  

ListPengecekan.java

    int day = getIntent().getIntExtra("day",0);
    int month = getIntent().getIntExtra("month",0);
    int year = getIntent().getIntExtra("year",0);
    TextView tgl = (TextView)findViewById(R.id.tanggall);

    month = month + 1;
    Log.d(TAG, "onDateSet: yyy/mm/dd: " + year + "-" + month  + "-" + day);
    final String date = year + "-" + month  + "-" + day;
    setContentView(R.layout.activity_pengecekan);
    tgl.setText(date);

1 个答案:

答案 0 :(得分:0)

因为你传递旧的值而不是像这样选择日期更改代码后传递.. 将此变量作为类级别的本地变量。

    private int sday,smonth,syear;
    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
        month = month + 1;
        Log.d(TAG, "onDateSet: yyy/mm/dd: " + year + "-" + month  + "-" + day);
        sday=day;
        syear=year;
        smonth=month;
        String date = year + "-" + month  + "-" + day;
        mDisplayDate.setText(date);


    }

然后更改意图代码..

Intent j = new Intent (ListInstrumen.this, ListPengecekan.class);
                j.putExtra("day",sday);
                j.putExtra("month",smonth);
                j.putExtra("year",syear);
                j.putExtra("id", String.valueOf(id));
                j.putExtra("ins", ((TextView) view.findViewById(R.id.tv_instrumenn)).getText().toString());
                startActivity(j);