获取上一个/第二天按钮上的日期按从上一个活动的日期之后使用SharedPreferences android [英] get previous/next day Date on button press after getting date from previous activity using SharedPreferences android

查看:207
本文介绍了获取上一个/第二天按钮上的日期按从上一个活动的日期之后使用SharedPreferences android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用此库进行自定义日历视图。点击任何日期,我通过共享首选项将该日期传递给下一个活动。我可以在下一个活动中显示日期。现在我想要的是有两个按钮,一个是上一个,另一个是下一个。如果我点击任何按钮日期必须根据要求更改。

In my application I am Using this library for Custom Calender View. On click any date i am passing that date to next activity through shared preferences. I am able to display date in next activity. Now what i want is there are two buttons, one is "Previous" and another one "Next". if i click on any button date has to change according to requirement.

Onclick日期代码是

Onclick date code is

calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() {
        @Override
        public void onDateClick(@NonNull Date selectedDate) {
            SimpleDateFormat df_new = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
            String newselectedDate=df_new.format(selectedDate);
            Toast.makeText(getApplicationContext(), "You selected" + newselectedDate + " : Date", Toast.LENGTH_SHORT).show();

            // SharedPreferences for sharing the Date with Next Activity, Added by Tara
            SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); String sdd=s_day.format(selectedDate);
            SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());String smm=s_month.format(selectedDate);
            SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());String syy=s_year.format(selectedDate);
            SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE);
            SharedPreferences.Editor spe = spf.edit();
          //  spe.putString("sdate", String.valueOf(selectedDate));
            spe.putString("sday", String.valueOf(sdd));
            spe.putString("smonth", String.valueOf(smm));
            spe.putString("syear", String.valueOf(syy));
            Log.d("Day is", String.valueOf(sdd));
            Log.d("Month is", String.valueOf(smm));
            Log.d("Year  is", String.valueOf(syy));

            spe.commit();


            Intent i_available_slot=new Intent(BookAnAppoinment.this, ChangeDate.class);
            startActivity(i_available_slot);
        }
    });

下一个通过共享首选项检索日期的活动是

SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);
    String id1 = spf.getString("sday", "no value");
    String id2 = spf.getString("smonth", "no value");
    String id3 = spf.getString("syear", "no value");

    String sdate=id1+"-"+id2+"-"+id3;
  //  d.setText(sdate);

    final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    final String formattedDate = df.format(sdate);

    d.setText(formattedDate);

    try {
        Date date=df.parse(sdate);
        Calendar cal = df.getCalendar();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    n.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    p.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

我写了try / catch块,但无法按照我的要求进行编码。请帮助我。

推荐答案

我做到了以下是预期结果的代码。以下是我选择日期的代码,并通过SharedPreferences将其传递给NextActivity。

I did it. Here is the code for Expected Result. Below is the code from where i am selecting Date and passing it to NextActivity through SharedPreferences.

calendarView.setOnDateClickListener(new CalendarView.OnDateClickListener() {
        @Override
        public void onDateClick(@NonNull Date selectedDate) {
            SimpleDateFormat df_new = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
            String newselectedDate=df_new.format(selectedDate);
            Toast.makeText(getApplicationContext(),"You selected" +newselectedDate +" : Date",Toast.LENGTH_SHORT).show();
            Log.v("You Selected Date is :",newselectedDate);

            // SharedPreferences for sharing the Date with Next Activity, Added by Tara
            SimpleDateFormat s_day=new SimpleDateFormat("dd",Locale.getDefault()); int sdd= Integer.parseInt(s_day.format(selectedDate));
            SimpleDateFormat s_month=new SimpleDateFormat("MM",Locale.getDefault());int smm= Integer.parseInt(s_month.format(selectedDate));
            SimpleDateFormat s_year=new SimpleDateFormat("yyyy",Locale.getDefault());int syy= Integer.parseInt(s_year.format(selectedDate));
            SharedPreferences spf= getSharedPreferences("myprfs", Context.MODE_PRIVATE);
            SharedPreferences.Editor spe = spf.edit();
            //  spe.putString("sdate", String.valueOf(selectedDate));
            spe.putInt("sday", sdd);
            spe.putInt("smonth", smm);
            spe.putInt("syear", syy);
            Log.d("Day is", String.valueOf(sdd));
            Log.d("Month is", String.valueOf(smm));
            Log.d("Year  is", String.valueOf(syy));

            spe.commit();

            Intent i_available_slot=new Intent(MainActivity.this, ChangeDate.class);
            startActivity(i_available_slot);
        }
    });

下面是使用SharedPreferences从上一个活动获取日期的活动代码>

Here is the next Activity code where getting the date from previous activity using SharedPreferences

 SharedPreferences spf=getSharedPreferences("myprfs", Context.MODE_PRIVATE);
    final int id1 = spf.getInt("sday", 0);
    final int id2 = spf.getInt("smonth", 0);
    final int id3 = spf.getInt("syear", 0);

    sdate= id1+"-"+id2+"-"+id3;
    date.setText(sdate);
    Log.v("Spf Day :", String.valueOf(id1));
    Log.v("Spf Month :", String.valueOf(id2));
    Log.v("Spf Year :", String.valueOf(id3));

    int month_index=id2-1;  // Java Maintains Index Number from 0 so reduce the month to 1 to get the selected month

    final Calendar c=Calendar.getInstance();
    System.out.println("Current time => " + c.getTime());
   // c.set(id1, id2, id3);
    c.set(Calendar.YEAR,id3);
    c.set(Calendar.MONTH,month_index);
    c.set(Calendar.DAY_OF_MONTH,id1);

    final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    formattedDate = df.format(c.getTime());
    Log.v("FormatedDate:",formattedDate);

    prev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            c.add(Calendar.DATE, -1);
            formattedDate = df.format(c.getTime());

            Log.v("PREVIOUS DATE : ", formattedDate);
            date.setText(formattedDate);

        }
    });
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            c.add(Calendar.DATE, 1);
            formattedDate = df.format(c.getTime());

            Log.v("PREVIOUS DATE : ", formattedDate);
            date.setText(formattedDate);

        }
    });

这篇关于获取上一个/第二天按钮上的日期按从上一个活动的日期之后使用SharedPreferences android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆