我在Android中使用PagerDatePicker库来选择日期,并在相应日期的片段上做一些工作。但我遇到的问题是,当我点击某个日期时,我会在相应日期的文本视图中获得正确的日期,但在Toast或Logcat上却没有。 当我在屏幕上滑动时(因为它是一个viewpager,我可以滑动以移动到显示here所示的下一个日期),或点击任何日期,我在textView上获得正确的日期,但是在Logcat上或Toast,它显示所选日期之后一天或之前一天的日期 如何在textView上显示正确的文本,在Toast / Logcat上显示错误的字符串。
有3个Java文件 - MainActivity.java -
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fl_main_container, DatePickerDefaultFragment.newInstance())
.commit();
}
}
DatePickerDefaultFragment.java -
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import pl.rspective.pagerdatepicker.PagerDatePickerDateFormat;
import pl.rspective.pagerdatepicker.adapter.DatePagerFragmentAdapter;
import pl.rspective.pagerdatepicker.adapter.DefaultDateAdapter;
import pl.rspective.pagerdatepicker.model.DateItem;
import pl.rspective.pagerdatepicker.view.DateRecyclerView;
import pl.rspective.pagerdatepicker.view.RecyclerViewInsetDecoration;
public class DatePickerDefaultFragment extends Fragment {
private DateRecyclerView dateList;
private ViewPager pager;
LocalDate localDate;
DateTime dateTime;
public static DatePickerDefaultFragment newInstance() {
return new DatePickerDefaultFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_picker_default, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
pager = (ViewPager) view.findViewById(R.id.pager);
dateList = (DateRecyclerView) view.findViewById(R.id.date_list);
// [Setting today's date in the DatePicker.]
localDate = new LocalDate();
DateTimeFormatter fmt = DateTimeFormat.forPattern("dd-MM-yyyy");
Log.i(getClass().getName(), localDate.toString(fmt));
String DATE_START = localDate.toString(fmt);
String DATE_END = localDate.plusYears(5).toString(fmt);
Log.i(getClass().getName(), DATE_END);
// [END]
dateList.addItemDecoration(new RecyclerViewInsetDecoration(getActivity(), R.dimen.date_card_insets));
Date start = null;
Date end = null;
Date defaultDate = null;
try {
start = PagerDatePickerDateFormat.DATE_PICKER_DD_MM_YYYY_FORMAT.parse(DATE_START);
end = PagerDatePickerDateFormat.DATE_PICKER_DD_MM_YYYY_FORMAT.parse(DATE_END);
defaultDate = PagerDatePickerDateFormat.DATE_PICKER_DD_MM_YYYY_FORMAT.parse(DATE_START );
} catch (ParseException e) {
e.printStackTrace();
}
dateList.setAdapter(new DefaultDateAdapter(start, end, defaultDate));
DatePagerFragmentAdapter fragmentAdapter = new DatePagerFragmentAdapter(getFragmentManager(), dateList.getDateAdapter()) {
@Override
protected Fragment getFragment(int position, long date) {
return SimplePageFragment.newInstance(position, date);
}
};
pager.setAdapter(fragmentAdapter);
dateList.setPager(pager);
dateList.setDatePickerListener(new DateRecyclerView.DatePickerListener() {
@Override
public void onDatePickerItemClick(DateItem dateItem, int position) {
// Toast.makeText(getActivity(), "Clicked: " + position, Toast.LENGTH_SHORT).show();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
Toast.makeText(getActivity(), "Date - " + simpleDateFormat.format(dateItem.getDate()), Toast.LENGTH_SHORT).show();
}
@Override
public void onDatePickerPageSelected(int position) {
}
@Override
public void onDatePickerPageStateChanged(int state) {
}
@Override
public void onDatePickerPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
});
}
}
和SimplePageFragment.java -
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
public class SimplePageFragment extends Fragment {
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
private static final String DATE_PICKER_DATE_KEY = "date_picker_date_key";
private static final String DATE_PICKER_POSITION_KEY = "date_picker_position_key";
private final String TAG = getClass().getSimpleName();
private TextView tvDate;
private TextView tvPosition;
private int position;
private long date;
private DateTime dateTime;
public static SimplePageFragment newInstance(int position, long date) {
Bundle bundle = new Bundle();
bundle.putInt(DATE_PICKER_POSITION_KEY, position);
bundle.putLong(DATE_PICKER_DATE_KEY, date);
SimplePageFragment simplePageFragment = new SimplePageFragment();
simplePageFragment.setArguments(bundle);
return simplePageFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(DATE_PICKER_POSITION_KEY, -1);
date = getArguments().getLong(DATE_PICKER_DATE_KEY, -1);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_page_simple, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tvDate = (TextView) view.findViewById(R.id.tv_date_label);
tvPosition = (TextView) view.findViewById(R.id.tv_position_label);
dateTime = new DateTime(date);
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-yyyy");
Log.i(TAG, dateTime.toString(formatter));
// tvDate.setText("fun");
String ss = dateTime.toString(formatter);
tvDate.setText(ss);
// Log.e("DATE", "" + dateTime.toString(formatter));
Log.e("DATE2", ss);
// Toast.makeText(getActivity(), SIMPLE_DATE_FORMAT.format(date), Toast.LENGTH_SHORT).show();
// tvPosition.setText(String.valueOf(position));
}
}
并且在SimplePageFragment中使用了这个fragment_page_simple.xml布局 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff343434"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_date_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="20sp"
android:textColor="#0f0"/>
<TextView
android:id="@+id/tv_position_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:textSize="16sp"
android:textColor="#ffff3f49"/>
</LinearLayout>
答案 0 :(得分:0)
这只是一个快速拍摄,但你尝试使用标准的Java Date类。该库基于它们,您正在使用JodaTime库。这不应该有任何区别,但谁知道。你在textView中有正确的值而在logcat中有错误,这真的很奇怪。
我检查了演示应用程序,我在logcat / toast中获得了正确的值 - 我使用了标准的Java日期类。
答案 1 :(得分:0)
final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(&#34; yyyy-MM-dd&#34;);
dateList = (DateRecyclerView) view.findViewById(R.id.date_list);
dateList.setDatePickerListener(new DateRecyclerView.DatePickerListener() {
@Override
public void onDatePickerItemClick(DateItem dateItem, int position) {
Toast.makeText(context, "date = " + SIMPLE_DATE_FORMAT.format(dateItem.getDate()), Toast.LENGTH_LONG).show();
someTextView.setText(SIMPLE_DATE_FORMAT.format(dateItem.getDate()));
}
@Override
public void onDatePickerPageSelected(int position) {
}
@Override
public void onDatePickerPageStateChanged(int state) {
}
@Override
public void onDatePickerPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
});