View Binder中的Android重新格式化日期

时间:2013-06-13 14:59:14

标签: android

这是我正在尝试做的事情,我有一个viewbinder,我想在这里重新格式化日期并设置文本。我不知道我做错了什么,但我收到了很多警告,它只是锁定了。这是代码:

  private class CustomViewBinder implements ViewBinder {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

        if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_DATE)) {
            String date = cursor.getString(columnIndex);
            DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getActivity());
            String formatedDate = dateFormat.format(date);

             TextView tv = (TextView) view;
             tv.setText(formatedDate);
            return true;
        }
}

警告:

    06-13 10:19:07.453: W/System.err(21766): java.lang.IllegalArgumentException
    06-13 10:19:07.513: W/System.err(21766):    at java.text.DateFormat.format(DateFormat.java:365)
    06-13 10:19:07.513: W/System.err(21766):    at java.text.Format.format(Format.java:93)
    06-13 10:19:07.513: W/System.err(21766):    at com.projectcaruso.naturalfamilyplaning.HistoryFragment$CustomViewBinder.setViewValue(HistoryFragment.java:101)
    06-13 10:19:07.513: W/System.err(21766):    at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:146)
    06-13 10:19:07.513: W/System.err(21766):    at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
    06-13 10:19:07.513: W/System.err(21766):    at android.widget.AbsListView.obtainView(AbsListView.java:2465)
    06-13 10:19:07.513: W/System.err(21766):    at android.widget.ListView.makeAndAddView(ListView.java:1775)
    06-13 10:19:07.513: W/System.err(21766):    at android.widget.ListView.fillDown(ListView.java:678)
    06-13 10:19:07.513: W/System.err(21766):    at android.widget.ListView.fillFromTop(ListView.java:739)
    06-13 10:19:07.513: W/System.err(21766):    at android.widget.ListView.layoutChildren(ListView.java:1628)
    06-13 10:19:07.513: W/System.err(21766):    at android.widget.AbsListView.onLayout(AbsListView.java:2300)
    06-13 10:19:07.513: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.523: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.523: W/System.err(21766):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
    06-13 10:19:07.523: W/System.err(21766):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
    06-13 10:19:07.523: W/System.err(21766):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
    06-13 10:19:07.533: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.533: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.533: W/System.err(21766):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    06-13 10:19:07.533: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.533: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.533: W/System.err(21766):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    06-13 10:19:07.533: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.543: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.543: W/System.err(21766):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    06-13 10:19:07.543: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.543: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.543: W/System.err(21766):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
    06-13 10:19:07.553: W/System.err(21766):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
    06-13 10:19:07.553: W/System.err(21766):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
    06-13 10:19:07.553: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.553: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.553: W/System.err(21766):    at com.slidingmenu.lib.CustomViewAbove.onLayout(CustomViewAbove.java:476)
    06-13 10:19:07.563: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.563: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.563: W/System.err(21766):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
    06-13 10:19:07.563: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.563: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.583: W/System.err(21766):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.View.layout(View.java:14008)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.ViewGroup.layout(ViewGroup.java:4656)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2005)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1826)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1121)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4598)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.Choreographer.doFrame(Choreographer.java:525)
    06-13 10:19:07.583: W/System.err(21766):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    06-13 10:19:07.583: W/System.err(21766):    at android.os.Handler.handleCallback(Handler.java:615)
    06-13 10:19:07.593: W/System.err(21766):    at android.os.Handler.dispatchMessage(Handler.java:92)
    06-13 10:19:07.593: W/System.err(21766):    at android.os.Looper.loop(Looper.java:137)
    06-13 10:19:07.593: W/System.err(21766):    at android.app.ActivityThread.main(ActivityThread.java:4950)
    06-13 10:19:07.593: W/System.err(21766):    at java.lang.reflect.Method.invokeNative(Native Method)
    06-13 10:19:07.593: W/System.err(21766):    at java.lang.reflect.Method.invoke(Method.java:511)
    06-13 10:19:07.593: W/System.err(21766):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
    06-13 10:19:07.593: W/System.err(21766):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
    06-13 10:19:07.593: W/System.err(21766):    at dalvik.system.NativeStart.main(Native Method)
    06-13 10:19:07.773: W/IInputConnectionWrapper(21766): getSelectedText on inactive InputConnection
    06-13 10:19:07.773: W/IInputConnectionWrapper(21766): setComposingText on inactive InputConnection

XML CLIP:

<TextView
    android:id="@+id/txtCreatedAt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="24dp"
    android:layout_toRightOf="@+id/image"
    android:gravity="left"
    android:paddingLeft="20sp"
    android:text="@string/defDate" />

已改为:

  private Date parseDate(String date) {
        SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd");
        Date dateObj = new Date();

        try {
            dateObj = curFormater.parse(date);
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return dateObj;
    }

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

         if (columnIndex == cursor.getColumnIndex(StatusData.KEY_CHARTING_DATE)) {
             SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
             String date = cursor.getString(columnIndex);
             Date dateObj = parseDate(date);

             String formatedDate = format.format(dateObj);

             TextView tv = (TextView) view;
             tv.setText(formatedDate);
             return true;
         }

1 个答案:

答案 0 :(得分:1)

您将非法参数传递给dateFormat.format()。它应该是Date对象。有关其他有效参数,请参阅the docs