2016-08-09 2 views
0

저는 Android Studio를 처음 사용하고 있으며 간단한 카운트 다운 타이머 앱을 만들고 있습니다. 나는 2 가지 활동을하고있다. 첫 번째 활동 (로그인) 사용자가 날짜 선택 도구를 통해 날짜를 선택하면 두 번째 활동 (프로필)에 카운트 다운 타이머가 표시됩니다. 카운트 다운 타이머는 자바 클래스에서 날짜를 설정할 때 완벽하게 작동하지만 로그인 활동에서 날짜를 검색하는 데 문제가 있습니다. 두 가지 활동에 대한 내 코드는 아래와 같습니다.datepicker에서 android의 다른 활동으로 날짜를 가져 오는 방법

날짜를 변경해야한다는 것을 알고 있습니다. futureDate = dateFormat.parse ("2016-8-10"); 그러나 나는 어떻게 잘 모르겠다.

(자) Login.class

public class Login extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, OnClickListener { 
//private SharedPreferences sp; 

EditText enterusername; 
Button continuetoprofile; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 
    /*sp = getSharedPreferences("myPreference" , Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sp.edit(); 
    editor.putString("myKey" , "I am data to store"); 
    editor.commit(); 

    Writing data to SharedPreferences*/ 

    enterusername = (EditText) findViewById(R.id.enterusername); 
    continuetoprofile=(Button) findViewById(R.id.continuetoprofile); 
    continuetoprofile.setOnClickListener(this); 
} 

//SHARED PREFERENCES 
/*public void saveInfo(View view){ 
    SharedPreferences save = getSharedPreferences("name", Context.MODE_PRIVATE); 

    SharedPreferences.Editor editor = save.edit(); 
    editor.putString("username",enterusername.getText().toString()); 
    editor.apply(); 
}*/ 



public void onClick (View v){ 
    Intent intent = new Intent(this,Profile.class); 
    intent.putExtra("username", enterusername.getText().toString()); 
    startActivity(intent); 
} 
public void datePicker(View view){ 

    DatePickerFragment fragment = new DatePickerFragment(); 
    fragment.show(getSupportFragmentManager(), "date"); 
} 
private void setDate(final Calendar calendar) { 
    final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM); 
    ((TextView) findViewById(R.id.showDate)).setText(dateFormat.format(calendar.getTime())); 
} 

public void onDateSet(DatePicker view, int year, int month, int day) { 
    Calendar cal = new GregorianCalendar(year, month, day); 
    setDate(cal); 
} 
public static class DatePickerFragment extends DialogFragment { 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     final Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day = c.get(Calendar.DAY_OF_MONTH); 
     return new DatePickerDialog(getActivity(), 
       (DatePickerDialog.OnDateSetListener) 
         getActivity(), year, month, day); 
    } 

} 
Intent intent = getIntent(); 
public void privPol(View view){ 
    Intent intent = new Intent(this, PrivacyPolicy.class); 
    startActivity(intent); 
} 
public void TOU(View view){ 
    Intent intent = new Intent(this, TermsOfUse.class); 
    startActivity(intent); 
} 
public void toProfile(View view){ 
    Intent intent = new Intent(this, Profile.class); 
    startActivity(intent); 

} 

PROFILE.CLASS

public class Profile extends AppCompatActivity { 
//private SharedPreferences spref; 
//TextView nameofuser; 

TextView nameofuser; 
private TextView daystxt, hourstxt, minutestxt, secondstxt; 
private Handler handler; 
private Runnable runnable; 

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

    initUI(); 
    countDownStart(); 

    nameofuser = (TextView)findViewById(R.id.nameofuser); 
    Intent intent = getIntent(); 
    String username = intent.getStringExtra("username"); 
    nameofuser.setText("Welcome, " + username); 

} 

@SuppressLint("SimpleDateFormat") 
private void initUI() { 
    daystxt = (TextView) findViewById(R.id.days); 
    hourstxt = (TextView) findViewById(R.id.hours); 
    minutestxt = (TextView) findViewById(R.id.minutes); 
    secondstxt = (TextView) findViewById(R.id.seconds); 
} 
public void countDownStart() { 
    handler = new Handler(); 
    runnable = new Runnable() { 
     @Override 
     public void run() { 
      handler.postDelayed(this, 1000); 
      try { 
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 

       // Here Set your Event Date 
       Date futureDate = dateFormat.("2016-8-10"); 
       Date currentDate = new Date(); 
       if (!currentDate.after(futureDate)) { 
        long diff = futureDate.getTime() 
          - currentDate.getTime(); 
        long days = diff/(24 * 60 * 60 * 1000); 
        diff -= days * (24 * 60 * 60 * 1000); 
        long hours = diff/(60 * 60 * 1000); 
        diff -= hours * (60 * 60 * 1000); 
        long minutes = diff/(60 * 1000); 
        diff -= minutes * (60 * 1000); 
        long seconds = diff/1000; 
        daystxt.setText("" + String.format("%02d", days)); 
        hourstxt.setText("" + String.format("%02d", hours)); 
        minutestxt.setText("" + String.format("%02d", minutes)); 
        secondstxt.setText("" + String.format("%02d", seconds)); 
       } else { 
        handler.removeCallbacks(runnable); 
        // handler.removeMessages(0); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }; 
    handler.postDelayed(runnable, 0); 
} 

Intent intent = getIntent(); 
public void toMenu(View view) { 
    Intent intent = new Intent(this, Menu.class); 
    startActivity(intent); 
} 
public void tostartdate(View view) { 
    Intent intent = new Intent(this, EditStartdate.class); 
    startActivity(intent); 
} 
+0

당신은 문제를 파악하려고 무슨 짓을 한거야? 어떤 값이 반환되는지 확인하려면 코드를 단계별로 실행하려고 시도 했습니까? 예외가 있습니까? –

+0

@ DavidT.Macknet 나는 문자열 이름을 검색하는 데 사용했던 것과 같은 방법으로 인 텐트를 사용해 보았지만 작동하지 않았습니다. – sharms

답변

0

번들의 의도를 통해 전달할 putExtras() 메소드를 사용하여 시도.

다음 코드는 밖으로 도움이 될 것입니다 번들을 통해 데이터를 전송

새로운 활동

Bundle bundle = getIntent().getExtras(); 

String date = bundle.getString(“Date”); 

에서 데이터 가져 오기

Intent i = new Intent(this, ActivityTwo.class); 

Bundle bundle = new Bundle(); 
bundle.putExtras("Date",YOUR_DATE); 

i.putExtras(bundle); 

startActivity(i); 

이 도움이 될

1
//Get the date 
Date buttonDate = SimpleDateFormat.getDateInstance().parse(mDate.getText().toString()); 

//Create a bundle to pass the date and send your date as Long 
      Bundle currentDate = new Bundle(); 
      currentDate.putLong("setDate", buttonDate.getTime()); 

//Read the passed bundle from the next activity get Long and convert it to Date 
Bundle setDate = this.getArguments(); 
Long currDate = setDate.getLong("setDate"); 

참고 : 날짜 생성자 밀리 초,하지 초에 한 시간을 받아들입니다. 1000을 곱하여 오래 공급해야합니다.

변환

Date d = new Date(1220227200L * 1000); 
+0

두 번째 활동에이 모든 것을 넣을 수 있습니까? 내 로그인 활동에서 날짜를 어떻게 검색합니까? – sharms

+0

날짜를 가져 와서 번들을 준비하고 첫 번째 활동에서 보내고 두 번째 활동에서 번들을 가져 와서 다시 날짜 유형으로 변환하십시오. –

관련 문제