//users_list.json
{
"users": [
{
"id": "1087",
"name": "Abhishek Saini",
"email": "saini.abhishek@gmail.com",
"gender": "male",
"contact": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "1088",
"name": "Gourav",
"email": "gourav9188@gmail.com",
"gender": "male",
"contact": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "1089",
"name": "Akshay",
"email": "akshay@gmail.com",
"gender": "male",
"contact": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
private String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("users_list.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer,"UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return json;
}
//
ArrayList<String> arrayList = new ArrayList();
ArrayList<String> arrayMobileList = new ArrayList();
try {
JSONObject jobj = new JSONObject(loadJSONFromAsset());
JSONArray jsonArray = jobj.getJSONArray("users");
for(int i=0; i<jsonArray.length(); i++){
JSONObject jo = jsonArray.getJSONObject(i);
String name = jo.getString("name");
arrayList.add(name);
JSONObject contact = jo.getJSONObject("contact");
String mobile = contact.getString("mobile");
arrayMobileList.add(mobile);
}
Log.e("names",""+arrayList);
} catch (JSONException e) {
e.printStackTrace();
}
//
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
CustomAdspter ca = new CustomAdspter(this, arrayList, arrayMobileList);
recyclerView.setAdapter(ca);
//
public class CustomAdspter extends RecyclerView.Adapter<MyHolder> {
ArrayList<String> arrayListNames;
ArrayList<String> arrayMobileListNames;
Context context;
public CustomAdspter(Context ctx, ArrayList<String> arrayList,ArrayList<String> arrayMobileList) {
this.context= ctx;
this.arrayListNames =arrayList;
this.arrayMobileListNames =arrayMobileList;
}
@NonNull @Override public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent,false);
MyHolder myHolder = new MyHolder(v);
return myHolder;
}
@Override public void onBindViewHolder(@NonNull MyHolder holder, int position) {
holder.name.setText(arrayListNames.get(position));
holder.mobile.setText(arrayMobileListNames.get(position));
}
@Override public int getItemCount() {
return arrayListNames.size();
}
}
class MyHolder extends RecyclerView.ViewHolder {
TextView name, mobile;
public MyHolder(@NonNull View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
mobile = (TextView) itemView.findViewById(R.id.mobile);
}
}