clojure-android / neko

The Clojure/Android Toolkit
Other
297 stars 36 forks source link

Makes the InterchangeableListAdapter useable with set and map datatypes. #35

Closed ghost closed 9 years ago

ghost commented 10 years ago

I was going through this tutorial and thought it would be neat to be able to feed the (def listing (atom (sorted-map))) directly to a list-view. This of course crashed my application until I modified the adapter itself. Now you can do this:

(def listing (atom (sorted-map))) ; { "20140722" [["At your place" "Party] .........]
                                  ;   "20140723" [ .... ]
                                  ;   ...... }

(defn main-layout [activity]
  [:linear-layout {:orientation :vertical}
   [:list-view {:adapter (ref-adapter (fn [] (make-ui [:text-view {}]))
                                      (fn [pos view parent [date events]] ..... )
                                      listing)}]])
alexander-yakushev commented 10 years ago

Looks good, but I wonder if you could use optional access-fn that can be passed to ref-adapter. Say:

(ref-adapter (fn [] [:text-view {}]) ; It is better now to just return UI-tree than to call make-ui explicitly
             (fn [pos view parent [date events]] ..... )
             listing
             seq ; Here seq will be called on the value of an atom before setting the data
)
alexander-yakushev commented 10 years ago

As seen here: https://github.com/clojure-android/neko/wiki/Namespaces#ref-adapter

ghost commented 10 years ago

seq throws Exceptions for me. I think it's a Nullpointer one in InterchangeableListAdapter::getCount, but vec works like a charm.

alexander-yakushev commented 10 years ago

This is a little weird, because clojure.lang.PersistentTreeMap$Seq conforms to java.util.List interface. But well, whatever works. Do you still think there is a necessity of extending InterchangeableListAdapter?

ghost commented 10 years ago

I'm happy with using vec as the fourth argument to ref-adapter. That pretty much made the changes to InterchangeableListAdapter obsolete (for me). The only reason to keeping the changes might be that some one doesn't use the ref-adapter and wants to use the InterchangeableListAdapter out of Java and not Clojure, but that's highly unlikely I guess. At this point the extensions are more of a convenience than a necessity.

alexander-yakushev commented 10 years ago

There might be performance reasons behind modifying the class (as opposed to using access-fn). I will put off this PR for now, until I compare if there is any performance boost from this approach. Thank you for contributing!

ghost commented 10 years ago

YW. One thing to the NullpointerException: (seq (sorted-map)) == nil So if you've got an empty map, calling seq on it and passing it into Java classes might not be too healthy for your application.

alexander-yakushev commented 10 years ago

Gotcha. vec is more appropriate then.

alexander-yakushev commented 9 years ago

After all I decided to keep this. Wouldn't hurt I guess. Merged as 49f6731b0e53b0d920f13e74e290f868cbfe7faf.