0

I am a beginner at the android studio. I am developing an app which shows news, images, and videos from a JSON in a server. I want to implement these using recycler view. by using multiple view types in the recycler view. my view type data comes from JSON. but I can't, add view type from JSON to the list. The code to add view type is

list.add(new CityEvent(Jsondata1, CityEvent.CITY_TYPE));

how can add the type example CITY_TYPE From JSON? Please help.

Nasim
  • 125
  • 2
  • 12
  • please mention your question clearly with code examples – Anubhav Gupta Sep 10 '18 at 17:58
  • If I understand you correct you need different ViewTypes in RecyclerView, so please check my answer into another question -> https://stackoverflow.com/questions/52252285/how-to-display-multiple-models-on-recycler-view-using-same-adapater/52252583#52252583 . If you just ask how to parse your ViewType from the JSON response, you can use Gson library and use same Enums in you app and server – MrVasilev Sep 11 '18 at 14:00

1 Answers1

0

Here is an example for your adapter but there is more you need different view holders and layouts and you will need to inflate different layouts when you detect different view types and so on...

in the adapter

companion object{
        private val VIEW_TYPE_CITY = 0
        private val VIEW_TYPE_OTHER = 1
    }


override fun getItemViewType(position: Int) :Int  {
        return if (json[position].CityEvent ==  CITY_TYPE ){
            VIEW_TYPE_CITY
        }else{
            VIEW_TYPE_OTHER
        }
    }

I hope this helps but you might need to ask more questions

Andre Andriole
  • 335
  • 6
  • 11