-1

I need to format date and time in the RemoteViewsFactory class. I know how to use DateFormat/SimpleDate Format. I'm doing it like in the thread below but the app keeps stopping when pressing on the widget on a physical device: remoteViews.setTextViewText(R.id.tvTaskDay, DateFormat.getDateInstance().format(task.getDay()));

Android - ListView items inside app widget not selectable

I'm also formatting in the parent activity and it works. Is it possible to reference the SimpleDateFormat code from there? Thank you in advance.

P.S. Error message. My RemoteViewsFactory class:

public class ScheduleWidgetViewFactory implements RemoteViewsService.RemoteViewsFactory
{
    private ArrayList<Schedule> mScheduleList;
    private Context mContext;

    public ScheduleWidgetViewFactory(Context context)
    {
        mContext = context;
    }

    @Override
    public void onCreate()
    {
    }

    @Override
    public void onDataSetChanged()
    {
      SharedPreferences sharedPreferences = 
      PreferenceManager.getDefaultSharedPreferences(mContext);

        Gson gson = new Gson();
        Type type = new TypeToken<List<Schedule>>() {}.getType();
        String gsonString = sharedPreferences.getString("ScheduleList_Widget", "");
        mScheduleList = gson.fromJson(gsonString, type);
    }

    @Override
    public int getCount()
    {
        return mScheduleList.size();
    }

    @Override
    public RemoteViews getViewAt(int position)
    {
        Schedule schedule = mScheduleList.get(position);

        RemoteViews itemView = new RemoteViews(mContext.getPackageName(), R.layout.schedule_widget_list_item);

        itemView.setTextViewText(R.id.schedule_widget_station_name, schedule.getStationScheduleName());
        itemView.setTextViewText(R.id.schedule_widget_arrival, DateFormat.getDateInstance().format(schedule.getExpectedArrival()));
        itemView.setTextViewText(R.id.schedule_widget_towards, schedule.getDirectionTowards());

        Intent intent = new Intent();
        intent.putExtra(ScheduleWidgetProvider.EXTRA_ITEM, schedule);
        itemView.setOnClickFillInIntent(R.id.schedule_widget_list, intent);

        return itemView;
    }

    @Override
    public int getViewTypeCount()
    {
        return 1;
    }

Parent Activity:

 @Override
    public void returnScheduleData(ArrayList<Schedule> simpleJsonScheduleData)
    {
        if (simpleJsonScheduleData.size() > 0)
        {
            scheduleAdapter = new ScheduleAdapter(simpleJsonScheduleData, StationScheduleActivity.this);
            scheduleArrayList = simpleJsonScheduleData;
            mScheduleRecyclerView.setAdapter(scheduleAdapter);
            scheduleAdapter.setScheduleList(scheduleArrayList);

            stationArrival = scheduleArrayList.get(0);

            stationShareStationName = stationArrival.getStationScheduleName();
            stationShareArrivalTime = stationArrival.getExpectedArrival();
            stationShareDirection = stationArrival.getDirectionTowards();

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            Date date = null;

            try {
                date = simpleDateFormat.parse(stationArrival.getExpectedArrival());
                date.toString();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            SimpleDateFormat newDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
            String finalDate = newDateFormat.format(date);

            stationShareArrivalTime = finalDate;

            //Store Schedule Info in SharedPreferences
            SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();

            Gson gson = new Gson();
            String json = gson.toJson(scheduleArrayList);
            prefsEditor.putString("ScheduleList_Widget", json);
            prefsEditor.apply();
        }
        else
        {
            emptySchedule.setVisibility(View.VISIBLE);
        }
LeadBox4
  • 83
  • 2
  • 11
  • 1
    Please [edit] your question to provide the [complete stack trace from the crash](https://stackoverflow.com/a/23353174). – Mike M. Jan 20 '19 at 03:21
  • @MikeM.Question updated. – LeadBox4 Jan 20 '19 at 03:31
  • 1
    If your app is crashing, then there will be a stack trace from the crash in the logcat. Have a look at [this Stack Overflow post](https://stackoverflow.com/a/23353174) and [this developer page](https://developer.android.com/studio/debug/am-logcat) for help in finding that. – Mike M. Jan 20 '19 at 03:32
  • @MikeM.Error message copied and pasted. – LeadBox4 Jan 20 '19 at 03:50
  • 1
    Those logs are likely unrelated to your crash. You're looking for [the stack trace from the crash](https://stackoverflow.com/a/23353174), which should be a large section of red lines, starting with `FATAL EXCEPTION`. You can use the filters above the log window to make it easier to find. Also, when you find it, please post the whole thing. – Mike M. Jan 20 '19 at 03:51
  • @MikeM.No stack trace. It happens when I select the widget from the widget menu. Only these messages in red: 2019-01-19 23:08:34.637 27793-27793/? E/Zygote: isWhitelistProcess - Process is Whitelisted 2019-01-19 23:08:34.639 27793-27793/? E/libpersona: scanKnoxPersonas 2019-01-19 23:08:34.639 27793-27793/? E/libpersona: Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory – LeadBox4 Jan 20 '19 at 04:13
  • 1
    Well, if you can't find it, then I would suggest that you start setting breakpoints in your code, and step through with the debugger to determine exactly where the crash is happening, and what the current local values are. If you still need assistance after that, you'll need to put together a [mcve] that demonstrates the issue. – Mike M. Jan 20 '19 at 04:17
  • @MikeM.I can't seem to debug for some reason, but I've figured out that the widget crashes due to the date format code. It works again when I change it back to: itemView.setTextViewText(R.id.schedule_widget_arrival, schedule.getExpectedArrival()); – LeadBox4 Jan 20 '19 at 04:24
  • 1
    If that works without crashing, then it would seem that `schedule.getExpectedArrival()` is returning a `String`, or some other `CharSequence`. If so, then you're passing the wrong thing to `DateFormat#format()`, which expects a `Number` or a `Date`. If you're trying to reformat a date/time string, you need to `parse()` it first to get a `Date`, then `format()` that. https://stackoverflow.com/a/12503542 – Mike M. Jan 20 '19 at 04:33
  • @MikeM.Thank you for the link. I know how to format date and time. As you see in the code sample above, I've used it in the parent activity. Here, I'm not sure how to implement the Date/SimpleDateFormat code in the RemoteViewsFactory class. – LeadBox4 Jan 20 '19 at 06:38
  • 1
    The fact that it's in a `RemoteViewsFactory` is irrelevant. It works the same everywhere. You need to parse it first, then format. – Mike M. Jan 20 '19 at 06:40
  • @MikeM.Solved! I'm just starting out with widgets. Can you please put your comments in an answer so that I can give you points? Thank you again. – LeadBox4 Jan 20 '19 at 06:58
  • 2
    Oh, I'm good. :-) It was only a few quick pointers. Nothing major. You actually had the right code already. You just forgot a step when you put it in the `RemoteViewsFactory`. Please feel free to post an answer yourself, if you like, or to delete this, if you'd rather. Thank you, though. I appreciate the offer. Glad you got it working. Cheers! – Mike M. Jan 20 '19 at 07:11

1 Answers1

0

In case anyone else has is stuck on this. As Mike M. pointed out in the comments, the DateFormat code structure in RemoteViewsFactory is the same as everywhere else.

 private String stationWidgetArrivalTime;

 @Override
    public RemoteViews getViewAt(int position)
    {
        Schedule schedule = mScheduleList.get(position);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        Date date = null;

        try {
            date = simpleDateFormat.parse(schedule.getExpectedArrival());
            date.toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat newDateFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
        String finalDate = newDateFormat.format(date);

        stationWidgetArrivalTime = finalDate;

        RemoteViews itemView = new RemoteViews(mContext.getPackageName(), R.layout.schedule_widget_list_item);

        itemView.setTextViewText(R.id.schedule_widget_station_name, schedule.getStationScheduleName());
        itemView.setTextViewText(R.id.schedule_widget_arrival, stationWidgetArrivalTime);
        itemView.setTextViewText(R.id.schedule_widget_towards, schedule.getDirectionTowards());

        Intent intent = new Intent();
        intent.putExtra(ScheduleWidgetProvider.EXTRA_ITEM, schedule);
        itemView.setOnClickFillInIntent(R.id.schedule_widget_list, intent);

        return itemView;
    }
LeadBox4
  • 83
  • 2
  • 11