How to set JSON Response data into RecycleView using adapter in an android

In this tutorial,Today we will learn how to set JSON Response data into RecycleView using adapter.This is very important tutorial,every android developer should have knowledge about this.

After setting the data into the adapter,we will also get the data into the adapter section.

Suppose we have JSON Response

[
{
“name”:”suman”,
“website”:”placement”,
“tutorialList”:
[
{“technology”:”Data science”,”language”,”Python”},{“technology”:”IOS”,”language”:”swift”}
]
},
{
“name”:”amit”,
“website”:”preparation”,
“tutorialList":
[
 {“technology”:”Android”,”language”:” Python”},{“technology”:”.Net”,”language”:”V.Basic”} 
]
}
.
.
.
]

The above JSON Response will set  into the Recyclerview using adapter step by step:

Step 1:

In this step,we will create two POJO class /getter & setter  class.


User.java
In this step,we will create two POJO class /getter & setter  class.

public class User
{
Private String  name;
private  String  website;
private List<TutorialList> tutorialList;


public List<TutorialList> getTutorialList()
{
return tutorialList;
}

public void setTutorialList( List<TutorialList> tutorialList)
{
this.tutorialList=tutorialList;
}

public String getName( )
{
return name;
}

public void setName(String name)
{
this.name=name;
}

public String getWebsite( )
{
return website;
}

public void setWebsite(String website)
{
this.website=website;
}
}

After that,you have to create the POJO class of TutorialList.

TutorialList.java

public class TutorialList
{
Private String technology;
Private String language;

public String getTechnology( )
{
return technology;
}

public void SetTechnology(String technology)
{
this.technology=technology;
}

public String getLanguage( )
{
return language;
}

public void setLanguage(String language)
{
this.language=language;
} 
}

Step 2:

In this step,we will create MyItemAdapter.java class where we will get the JSON Response data.

MyItemAdapter.java 

public class MyItemAdapter extends RecycleView.Adapter<MyItemAdapter.MyViewHolder>
{
List<User> user;
Context context;

@Override
public class MyItemAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,int viewType)
{
View v=LayoutInflater.from(parent.getContext(    )).inflate(R.layout.adapter_item_layout,parent,false);
return new MyItemAdapter.MyViewHolder(v);
}
@Override
public void onBindViewHolder(final MyItemAdapter.MyViewHolder holder,int position)
{
final User userItem=user.get(position);

//Here you can get the item according to your requirement
String name=userItem.getName( );
String website=userItem.getWebsite( );

List<TutorialList> list=userItem.getTutorialList(  );// It will show all tutorial list

}

public MyItemAdapter(List<User> user,Context context)
{
this.user=user;
this.context=context;
}


public class MyViewHolder extends RecycleView.ViewHolder
{
public MyViewHolder(View itemView)
{
private name,website;
super(itemView);
name=itemView.findViewById(R.id.name);
website=itemView.findViewById(R.id.website);
}

}

@Override
public int getItemCount( )
{
return user.size( );
}
}

Step 3:

In this step,we will create MainActivity.java class where we will set the JSON Response data.

import android.support.v7.app.*;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity
{
private List<User> user;
private List<TutorialList> tutorialList_adapter;
Private MyItemAdapter myItemAdapter;
Private RecycleView recycleView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.recycleview_layout);
recyclerview=findViewById(R.layout.recyclerview);

//Now suppose you have response data using API with the help of  volley (I consider).

@Override
public void onResponse(JSONArray response)
{
user=new ArrayList<>( );
tutorialList=new ArrayList<>( );

if(response.length( ) &amp;&amp; response!=null)
{
for(int i=0;i<response.length( );i++)
{
try
{

JSONObject jsonobject=response.get(i);
User user1=new User( );
user1.setName(jsonobject.getString(“name”));
user1.setWebsite(jsonobject.getString(“website”));

//Here setting JSON Response in tutorial list adapter
if(jsonobject.getString(“tutorialList”).length( )>0 &amp;&amp;sonobject.getString(“tutorialList”)!=null)
{
JSONArray jsonArray=jsonobject.getJSONArray(“tutorialList”);

for(int i=0;i<jsonArray.length( );i++)
{
TutorialList tutorial=new TutorialList( );
JSONObject jsonobjectList=response.get(i);
tutorial.setTechnology(jsonobjectList.getString(“technology”);
tutorial.setLanguage(jsonobjectList.getString(“language”);
tutorialList_adapter.add(tutorial);
}
user1.setTutorialList(tutorialList_adapter);
user.add( user1); 
}
}

catch(JSONException e)
{
e.printStackTrace( );
}

}

myItemAdapter=new MyItemAdapter(user,getApplicationContext( ));
recycleview.setAdapter(myItemAdapter);

}
}

Thanks and Regards 

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *