Hallo zusammen,
ich benutze DatePickerFragment und möchte gerne in einer TextView den ausgewählten Tage anzuzeigen.
Das ist mein code bis jetzt.
Java
package com.example.de.sasitravel;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.support.v4.app.DialogFragment;
import android.widget.TextView;
public class FlightActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{
UserLocalStore userLocalStore;
Boolean isReturn = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight);
userLocalStore = new UserLocalStore(this);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.top_NavView_bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(1);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.action_profile:
startActivity(new Intent(FlightActivity.this, ProfileActivity.class));
break;
case R.id.action_flight:
break;
case R.id.action_hotel:
startActivity(new Intent(FlightActivity.this, HotelActivity.class));
break;
}
return false;
}
});
}
@Override
protected void onStart() {
super.onStart();
//startActivity(new Intent(this, LoginActivity.class));
if(authenticate() == false){
}else {startActivity(new Intent(this, LoginActivity.class));}
}
private boolean authenticate(){
return userLocalStore.getUserLoggedIn();
}
public void onClickOneWay(View v){
setContentView(R.layout.activity_one_way);
}
public void onClickReturn(View v){
setContentView(R.layout.activity_flight);
}
public void datePicker(View view){
DatePickerFragment fragment = new DatePickerFragment();
fragment.show(getSupportFragmentManager(), "date");
}
public void datePickerReturn(View view){
isReturn = true;
DatePickerFragment fragment = new DatePickerFragment();
fragment.show(getSupportFragmentManager(), "date");
}
private void setDate(final Calendar calendar) {
final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
if (!isReturn) {
((TextView) findViewById(R.id.tvDate)).setText(dateFormat.format(calendar.getTime()));
//((TextView) findViewById(R.id.tvDay)).setText(dateFormat.format(calendar.getTime()));
}else{
((TextView) findViewById(R.id.tvDateReturn)).setText(dateFormat.format(calendar.getTime()));
}
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar calendar = new GregorianCalendar(year,month,day);
Log.d("VIVZ", "day of week "+day );
setDate(calendar);
}
public static class DatePickerFragment extends DialogFragment{
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(),year,month,day);
}
}
}
Alles anzeigen
ich würde gerne dahin den Tag reinschreiben.
//((TextView) findViewById(R.id.tvDay)).setText(dateFormat.format(calendar.getTime()));