我是Android新手,我正在尝试这样做。我有一个ArrayAdapter,一个使用这个适配器的Fragment和一个每15分钟运行一次计时器任务的类来命中api并更新我的缓存的超级项目。该超级对象包含字段列表。此外,我正在使用日历从此列表中过滤,我的片段显示已过滤的列表。因此,当我单击日历上的日期时,具有该创建日期的SampleObject将显示在片段中。我想在我的计时器任务更新超级项目时强制更新此列表。是否可能,如果可能,怎么样?
public class ScheduleAdapter extends ArrayAdapter<SampleObject> {
private List<SampleObject> objects;
public void setObjects(List<SampleObject> objects) {
this.objects = objects;
}
public ScheduleAdapter(Context context, List<SampleObject> objects) {
super(context, R.layout.schedule_list_item, R.id.schedule_list_layout, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = LayoutInflater.from(getContext()).inflate(R.layout.schedule_list_item, parent, false);
ViewHolder holder = new ViewHolder(row);
row.setTag(holder);
final SampleObject sam = objects.get(position);
if (row == null) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
row = inflater.inflate(R.layout.schedule_list_item, parent, false);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.customerView.setText(sampleObject.getStatus());
holder.addressView.setText(cleaningJob.getShortenedAddress());
holder.timingView.setText(sampleObject.getFormattedStartTime() + "-" + cleaningJob.getFormattedEndTime());
holder.iconView.setImageResource(R.drawable.ic_clear);
return row;
}
public static class ViewHolder {
public final ImageView iconView;
public final TextView customerView;
public final TextView addressView;
public final TextView timingView;
public ViewHolder(View view) {
iconView = (ImageView) view.findViewById(R.id.list_item_icon);
customerView = (TextView) view.findViewById(R.id.list_item_customer_textview);
addressView = (TextView) view.findViewById(R.id.list_item_address_textview);
timingView = (TextView) view.findViewById(R.id.list_item_timing_textview);
}
}
}
}
public class ScheduleFragment extends Fragment {
private static final String LOG_TAG = "ScheduleFragment";
private ScheduleAdapter mScheduleAdapter;
public static final String ARGS_CLEANING_JOB_ID = "id";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
Date requestedDate = null;
if (args != null) {
int day = args.getInt("date");
int month = args.getInt("month");
int year = args.getInt("year");
requestedDate = BasicUtility.constructDate(day, month, year);
}
List<SampleObject> sampleObjs = Collections.EMPTY_LIST;
if (requestedDate != null) {
List<SampleObject> sampleObjsFromController = ScheduleController.getSampleObjByDate(requestedDate);
if (CollectionUtils.isNotEmpty(sampleObjsFromController)) {
sampleObjs = sampleObjsFromController;
}
}
mScheduleAdapter =
new ScheduleAdapter(getActivity(), sampleObjs);
View scheduleListView = inflater.inflate(R.layout.schedule_list_fragment, container, false);
ListView listView = (ListView) scheduleListView.findViewById(R.id.schedule_list);
listView.setAdapter(mScheduleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
SampleObject job = mScheduleAdapter.getItem(position);
Bundle bundle = new Bundle();
bundle.putString(ARGS_CLEANING_JOB_ID, job.getUniqueIdentifier());
Intent intent = new Intent(getActivity(), DetailActivity.class).putExtras(bundle);
startActivity(intent);
}
});
return scheduleListView;
}
}
public class CalendarFragment extends Fragment {
private static final String LOG_TAG = "CalendarFragment";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.calendar_view, container, false);
CalendarView calendarView = (CalendarView) rootView.findViewById(R.id.calendarView);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2) {
Log.d(LOG_TAG, "Showing item for date " + i + "::" + i1+ "::" + i2);
Bundle args = new Bundle();
Fragment frag = new ScheduleFragment();
frag.setArguments(args);
args.putInt("date", i2);
args.putInt("month", i1 + 1);
args.putInt("year", i);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.schedule_list_container,frag, MainActivity.SCHEDULEFRAGMENT_TAG)
.commit();
}
});
return rootView;
}
}
public class ScheduleController {
private static Map<String, List<String>> sampleObjsByDate = new HashMap<>();
private static final String LOG_TAG = "ScheduleController";
static {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Log.i(LOG_TAG, "updating schedule...");
populateSchedule();
}
};
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(timerTask, 0, 15*60*1000);
}
public static void populateSchedule() {
//populate SuperObject here
}
public static List<SampleObject> getCleaningJobByDate(Date date) {
//filter list here
}
答案 0 :(得分:1)
我不应该首先使用timerTask。我现在使用IntentService与AlarmManager和ResultReceiver,它就像一个魅力。如果其他人正在寻找相同的功能,请按照教程https://guides.codepath.com/android/Starting-Background-Services
进行操作