SimplicityApks / ReminderDatePicker

A Google Keep-like Date and Time Picker for reminders
Apache License 2.0
74 stars 26 forks source link

New Method addAdapterItem() #34

Closed chrisonline closed 8 years ago

chrisonline commented 8 years ago

Hi!

I am just adding a feature for the user to change and add the entries in the Adapter of TimePicker. How can I remove "all" entries in the TimePicker ? Default are the entries from the XML.

I want to add all entries by myself without the XML because the user modifies the entries and I save it to the preferences. Afterwards I will add all entries via addAdapterItem() from my saved preferences.

I don't find any way to clear the defaults (XML entries) and add my entries with addAdapterItem().

At the moment I do this:

rdpTime = (TimeSpinner) findViewById(R.id.rdpTime);
ArrayList<TimeItem> items = pref.getSmartTimes();
for(TimeItem item : items) {
    rdpTime.addAdapterItem(item);
}

But I see always only the default ones coming from the XML.

chrisonline commented 8 years ago

I have now tried it via "insertAdapterItem" and I get no error, but the dropdown is still the default one. So what do I wrong or how should the "insertAdapterItem" work?

rdpTime = (TimeSpinner) findViewById(R.id.rdpTime);
ArrayList<TimeItem> items = pref.getSmartTimes();
int i = 0;
for(TimeItem item : items) {
     rdpTime.insertAdapterItem(item, i);
     i++;
     //rdpTime.addAdapterItem(item);
}
rdpTime.setOnTimeSelectedListener(this);
chrisonline commented 8 years ago

1 hour later and I* found the answer* why my own items not displayed. This is because the method setTimeFormat make a reset of all adapter items and so the items I inserted before are gone.

Now I get the adapter with getAdapter() and call the method clear(). Afterwards I add my items. Works now!

But I think it would be better to start without any items except the "Footer" item.

Now it is like:

  1. Insert 6 items into the adapter
  2. Remove all 6 items from the adapter
  3. Add again my own items to the adapter
SimplicityApks commented 8 years ago

:D well good you figured it out on your own. The simplest way to do this would be to subclass the TimeSpinner (if you haven't already) and override the getSpinnerItems method to return an empty list. Or even better, return the desired items you want to load from the shared prefs (I wrote it with exactly this in mind). You can only nicely do these things in a subclass as terrible things™ could happen if the spinner items aren't initialized correcty. If you really don't want to subclass you could simply provide an empty xml file and override R.xml.time_items with it, that way the spinner will not contain any items except for the footer until you add them dynamically.

The reason there is no clearAdapterItems() method is exactly that, also it would be trivial to implement as a simple for loop which just calls removeAdapterItem() on each item (and if you know what you're doing you could also just reinitialize the adapter...).

chrisonline commented 8 years ago

Thanks for your input. Will subclass the TimeSpinner.

SimplicityApks commented 8 years ago

I'll close this then, if you've got any more questions feel free to ask here or open another issue ;)