【发布时间】:2019-01-20 15:35:14
【问题描述】:
我需要在 RemoteViewsFactory 类中格式化日期和时间。我知道如何使用 DateFormat/SimpleDate 格式。我正在像下面的线程中那样做,但是当按下物理设备上的小部件时应用程序一直停止:remoteViews.setTextViewText(R.id.tvTaskDay, DateFormat.getDateInstance().format(task.getDay())) ;
Android - ListView items inside app widget not selectable
我也在父活动中进行格式化,它可以工作。是否可以从那里引用 SimpleDateFormat 代码?提前谢谢你。
附:错误信息。 我的 RemoteViewsFactory 类:
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;
}
家长活动:
@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);
}
【问题讨论】:
-
如果您的应用程序正在崩溃,那么 logcat 中将有一个来自崩溃的堆栈跟踪。查看this Stack Overflow post 和this developer page 以获取帮助。
-
这些日志可能与您的崩溃无关。你在找the stack trace from the crash,应该是一大段红线,以
FATAL EXCEPTION开头。您可以使用日志窗口上方的过滤器使其更易于查找。另外,当你找到它时,请发布整个内容。 -
好吧,如果你找不到它,那么我建议你开始在代码中设置断点,并使用调试器逐步确定崩溃发生的确切位置,以及当前的当地价值观是。如果在那之后您仍然需要帮助,您需要整理一个 minimal reproducible example 来证明该问题。
-
哦,我很好。 :-) 这只是一些快速的指示。没什么大不了的。你实际上已经有了正确的代码。当您将其放入
RemoteViewsFactory时,您只是忘记了一个步骤。如果您愿意,请随时自行发布答案,或者如果您愿意,请删除此答案。不过,谢谢。我很感激这个提议。真高兴你做到了。干杯!
标签: android widget simpledateformat date-format remoteview