我需要在RemoteViewsFactory类中格式化日期和时间。我知道如何使用DateFormat / SimpleDate格式。我正在执行以下操作,但是按下物理设备上的小部件时,应用程序会继续停止:remoteViews.setTextViewText(R.id.tvTaskDay,DateFormat.getDateInstance()。format(task.getDay())) ;
Android - ListView items inside app widget not selectable
我也在父活动中进行格式化,并且可以正常工作。是否可以从那里引用SimpleDateFormat代码?预先谢谢你。
P.S。错误信息。 我的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);
}
答案 0 :(得分:0)
万一其他人陷入困境。正如Mike M.在评论中指出的那样,RemoteViewsFactory中的DateFormat代码结构与其他地方相同。
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;
}