-3

I have a problem. I filled the fields of my cardviews with data from a JSON file and displays them in a recyclerview. Now , what i want is to use two differents type of cardview to display data.

Example of JSON

{
    {
        "id":"3",
        "title":"Title1",
        "place":"Stade omnisport de lom\u00e9",
        "imageUrl":".\/uploads\/thumbs\/20150907133004.jpg",
        "Description":"Lorem ipsum dolor erit ex, quis mollis massa sollicitudin eu. Mauris congue auctor placerat",
        "codeCity":"1"
    },
    {
        "id":"15",
        "title":"Title2",
        "place":"centre culturelle arena",
        "imageUrl":".\/uploads\/thumbs\/C3.PNG",
        "Description":"Lorem ultrices dapibus. Praesent feugiat hendrerit ex, quis mollis massa sollicitudin eu. Mauris congue auctor placerat",
        "codeCity":"1"
    }

    {
        "id":"85",
        "title":"Title3",
        "place":"centrena",
        "imageUrl":".\/uploads\/thumbs\/A4.PNG",
        "Description":"Lorem ultrices dapibus. Praesent feugiat hendrerit ex, quis mollis massa sollicitudin eu. Mauris congue auctor placerat",
        "codeCity":"2"
    }   
}

I want to use different views depending on the city code. For example, for the codecity "1" I use the view 1 and for codecity 2 I use the view 2. In which part of my code I have to do that and how?

2 Answers2

0

Refer to this link :https://guides.codepath.com/android/Heterogenous-Layouts-inside-RecyclerView. This is something that you could have found with a basic google search. Advice for next time, search thoroughly before creating new questions

0
@Override
public int getItemViewType( int position )
{
    String elementName = items.get( position );
    switch(elementName)
    {
        case "1": TYPE_ONE; break;
        case "2": TYPE_TWO; break;
        default 0; break;
    }
}

@Override
public ViewHolder onCreateViewHolder( ViewGroup viewGroup, int viewType )
{
     LayoutInflater mInflater = LayoutInflater.from( viewGroup.getContext( ) );
     ViewHolder viewGroupList;
     switch(viewType)
     {
         case "1": View view1 = mInflater.inflate( R.layout.view1, viewGroup, false );
            viewGroupList = new ViewHolder1( view1 );
            break;
         case "2": View view2 = mInflater.inflate( R.layout.view2, viewGroup, false );
            viewGroupList = new ViewHolder2( view2 );
            break;
     }
     return viewGroupList;
}