Office Hours — Today, January 12

Thursday, January 10

Mark M.
has entered the room
Mark M.
turned on guest access
Jan 12
4:10 PM
ndrocchietto i.
has entered the room
Mark M.
hello, Ivano!
how can I help you today?
ndrocchietto i.
Hi Mark good afternoon
I have an adapter where I set data for three different recyclerview different. Inside the adapter I change the state depending by the three different list i want
my problem is that i need now to add an header on top of the recycler view
( a title)
I obtained that but the problem is that i have overriden
4:15 PM
ndrocchietto i.
View paste
  override fun getItemViewType(position: Int): Int {
        val item = resultList[position]

        return if (position == 0 && item.searchFilter == AddressSearchItem.RECENT_LOCATION) TYPE_HEADER
        else
            TYPE_ITEM
    }
4:15 PM
ndrocchietto i.
and where Adress Search Item retrieves the state so that the adapter knows that have to manage the third case
but the problem is : that everything works perfectly except the first item, where instead to have the header+the first item(adress)
I get only the header
i would like to have the header+the first item
stop
Mark M.
a given position has only one View, so if you want the first View to show a header plus the first item, the View has to handle that
ndrocchietto i.
yes but i want the header also for the other items
Mark M.
"my problem is that i need now to add an header on top of the recycler view" -- this implies that there is only one header
ndrocchietto i.
yes
i can share the small class
Mark M.
does this header scroll with the RecyclerView contents? or is this header fixed at the top?
ndrocchietto i.
the header is fixed no scrolls
View paste (111 more lines)
class PlaceAutoCompleteAdapter(val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var resultList = listOf<AddressInterface>()
    private val TYPE_HEADER = 0
    private val TYPE_ITEM = 1

    private val clickSubject = PublishSubject.create<searchResult>()
    val clickEvent: Observable<searchResult> = clickSubject

    fun setData(addressList: List<AddressInterface>) {
        resultList = addressList
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
...
4:20 PM
Mark M.
then why is it *in* the RecyclerView -- put it above the RecyclerView instead
ndrocchietto i.
i just want a normal list with the adapter visible if there is at least one item
I ca't
i can't
Mark M.
if it is inside the RecyclerView, it will scroll with the content
because it is part of the content, along with your items
ndrocchietto i.
omg you are right
but i can't separate from the recycler view
Mark M.
why not?
ndrocchietto i.
because i get the state only from inside the adapter, while the activity where i call the recycler view has this call
Mark M.
it is possible that some third-party RecyclerView layout managers offer some sort of sticky headers that you could use
that sounds like you have too much responsibility in this adapter
ndrocchietto i.
yes
this is the call
View paste (7 more lines)
 Observables.combineLatest(searchResults, realmResults().toObservable()) { first, second ->
                val realmResult = Realm.getDefaultInstance().copyFromRealm(second)
                if (first.size > 0) {
                    return@combineLatest first
                } else {
                    return@combineLatest realmResult.asReversed().take(5)
                }

            }
                .subscribe { values ->
                    adapter.setData(values)
                }
                .addTo(disposeBag)

            searchResults
...
where set data takes one of the three lists
Mark M.
add a second line to the subscribe() lambda that calls something other than your adapter and sets up the header
4:25 PM
Mark M.
so instead of .subscribe { values -> adapter.setData(values) } you have .subscribe { values -> adapter.setData(values); andSetUpMyHeaderPlease(values) }
ndrocchietto i.
the problem is that in this way, all the three list would take the header
ah no
mark
Mark M.
ok, so it is then .subscribe { values -> adapter.setData(values); if (theHeaderIsNeeded(values)) andSetUpMyHeaderPlease(values) }
ndrocchietto i.
I need to retrieve only 5 items
so the problem of the scroll does not exist
Mark M.
that depends entirely on the screen size
ndrocchietto i.
they will be visible from whatever phone bigger than 4 inches
Mark M.
I have two phones within reach that are smaller than that
ndrocchietto i.
so I can make it work from my adapter
because the boolean condition you mention cannot be easily taken from on subscribe
Mark M.
then you have to be willing to say that the header will scroll with the content on small screens, or you have to use a third-party layout manager that offers some sort of sticky header feature
or, change your code such that the boolean is not tied to this adapter (which, as I mentioned, seems to be doing too much work)
ndrocchietto i.
I would agree but there is a huge problem
one sec
4:30 PM
ndrocchietto i.
give me a couple of minutes please
Mark M.
sure
ndrocchietto i.
thanks
4:35 PM
ndrocchietto i.
here it is
I have the RealmObject that is
View paste (3 more lines)

@RealmClass
open class AddressRealm : RealmObject(), AddressInterface {

    override var id: String = "0"
    var name: String? = null
    var street: String? = null

    override val mainDescription: String
        get() = name ?: ""
    override val subDescription: String
        get() = street ?: ""
    override val result: String
        get() = street ?: ""

...
Mark M.
searchFilter seems like a very odd thing for a model object to hae
er, to have
ndrocchietto i.
i agree
but if I do
View paste
.subscribe { values ->
                    if(AddressRealm.scopeFilter=RECENT_ADRESS){
                        //visibile header
                    }
                    adapter.setData(values)
                }
scope filter is not recognized
unresolved reference
Mark M.
AddressRealm is a model object
my guess is that your RecyclerView is showing a list of these
if so, searchFilter has nothing to do with AddressRealm, and IMHO does not belong there
ndrocchietto i.
if only I could make recognize at the subscribe method that I am in the case RECENT ADRESS, it would work
Mark M.
the "case" is not tied to an individual address
at least, from what I am inferring, based on the particular class names and stuff that you are presenting here
ndrocchietto i.
my senior has extended adressRealm with the Adress Interface as you can see
so that I wrap the realm object
adding the state
(is a kinda of strategy pattern i guess)
4:40 PM
Mark M.
IMHO, that makes no sense
4:40 PM
ndrocchietto i.
ok
Mark M.
for example, if you have 5 AddressRealm objects in this list, you can have 5 different values for searchFilter
ndrocchietto i.
no
Mark M.
well, no, that's not true -- my apologies
ndrocchietto i.
always the same
Mark M.
you have it hard-coded
ndrocchietto i.
:)
yes
Mark M.
but if it is always the same, there is only one case, not three
ndrocchietto i.
i would like just to return the boolean
this is the wrap on the db realm
then we have retrofit
and a google search adress call
are three different typologies of adresses
Mark M.
use if (values[0].searchFilter == AddressSearchItem.RECENT_LOCATION)
ndrocchietto i.
with different layout
Mark M.
or, use if (values.any { it.searchFilter == AddressSearchItem.RECENT_LOCATION}), if the list might have a mix of searchFilter values
ndrocchietto i.
mark you are a genius
I want ot become like you
thank you, really
it works perfect
now I am going to add an header and play withinvisibility
thank you
Mark M.
you're welcome!
though, if I remember your time zone correctly... don't forget to sleep! :-)
ndrocchietto i.
bye I go to sleep I am in a meditation center, playing with the pc
better i close I am not supposed to do coding
4:45 PM
ndrocchietto i.
have a pleasant day
Mark M.
you too!
ndrocchietto i.
thanks
5:00 PM
ndrocchietto i.
has left the room
Mark M.
turned off guest access

Thursday, January 10

 

Office Hours

People in this transcript

  • Mark Murphy
  • ndrocchietto ivano