set data into recyclerview using gson in android

android json parsing and display with recyclerview
how to convert json to gson in android
how to fetch data from json file in android
android json parsing and display with recyclerview github
json to gson android
gson tutorial android
gson to json

I'm trying to set recyclerview data from this type of json response but data is not setting into recylerview

Response:

   {
    "vehicles": [
     {
        "id": 1,
        "vehicle_number": "gj03fn3235",
        "driver_id": 4,
        "vehicle_type": "3",

        "admindata": {
            "id": 7,
            "email": "chirag.pwt2@gmail.com"

        },
        "userdata": {
            "id": 4,
            "email": "keval.pwt@gmail.com"

        }
    },
    {
        "id": 2,
        "vehicle_number": "gj03fn3236",
        "driver_id": 4,
        "vehicle_type": "4",

        "admindata": {
            "id": 7,
            "email": "keval.pwt@gmail.com"

        },
        "userdata": {
            "id": 4,
            "email": "keval.pwt@gmail.com"

          }
      }
  ]
}

I have created a pojo class of response But my data is not setting into recylerview api call successfully but response print in logcat i tried to set vehicle_number and email in recylerview

Here is my java code:

   @Override
        public void onSuccess(int 
statusCode, Header[] headers, JSONObject 
response) {
            super.onSuccess(statusCode, 
     headers, response);

            try {
                Gson gson = new 
 GsonBuilder().create();

                List<VehicleList> list = 

   gson.fromJson(response.getJSONArray
   ("vehicles").toString(), new 
   TypeToken<List<VehicleList>>() {
                }.getType());

   Log.e("listsize",""+list.size());

                if (list.size() == 0) {

    txt_error.setVisibility(View.VISIBLE);

                } else {
                    VehiclesDriverAdpter 
  acceptedRequestAdapter = new 
  VehiclesDriverAdpter(list);
                    recyclerView.setAdapter
  (acceptedRequestAdapter);
  acceptedRequestAdapter.
  notifyDataSetChanged();
                }

            } catch (JSONException e) {

            }


        }

My Adapter class:

 public class VehiclesDriverAdpter extends 
  RecyclerView.Adapter<VehiclesDriverAdpter.Holder> {
 List<VehicleList> list;
 List<Vehicle> list1;
 FragmentActivity activity;

 public VehiclesDriverAdpter(List<VehicleList> list) {
    this.list = list;
 }



 @Override
 public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new 


 Holder(LayoutInflater.from(parent.getContext())
 .inflate(R.layout.adapter_vehicle, parent, false));
}

 @Override
 public void onBindViewHolder(final Holder holder, int position) {
    final Vehicle pojo1 = list1.get(position);

    Log.e("VehicleNumber",""+pojo1.getVehicleNumber());


    holder.txt_vehicle_number.setText(pojo1.getVehicleNumber());
    holder.txt_vehicle_driver_name.setText(pojo1.getUserdata().getEmail());
    holder.drivername.setText(pojo.getDriver_name());




    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data", pojo1);
            VehiclesInfoFragment detailFragment = new 
    VehiclesInfoFragment();
            detailFragment.setArguments(bundle);
            ((HomeActivity) holder.itemView.getContext()).
    changeFragment(detailFragment, "Passenger Information");
        }
    });
    BookFont(holder, holder.txt_vehilce_type);
    BookFont(holder, holder.txt_vehicle_driver_name);
    BookFont(holder, holder.txt_vehicle_driver_mobile_no);

    MediumFont(holder, holder.txt_vehicle_number);



}

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

public class Holder extends RecyclerView.ViewHolder {



    TextView txt_vehicle_number, txt_vehilce_type, txt_vehicle_driver_name, 
    txt_vehicle_driver_mobile_no;
    CircleImageView img_driver;

    public Holder(View itemView) {
        super(itemView);

        txt_vehicle_number = (TextView) 
    itemView.findViewById(R.id.txt_vehicle_number);
        txt_vehilce_type = (TextView) 
    itemView.findViewById(R.id.txt_vehilce_type);
         txt_vehicle_driver_name = (TextView) 
   itemView.findViewById(R.id.txt_vehicle_driver_name);
        txt_vehicle_driver_mobile_no = (TextView) 
    itemView.findViewById(R.id.txt_vehicle_driver_mobile_no);

    }
}

   public void BookFont(Holder holder, TextView view1) {
    Typeface font1 = 
   Typeface.createFromAsset(holder.itemView.getContext().getAssets(), 
"font/AvenirLTStd_Book.otf");
    view1.setTypeface(font1);
}

   public void MediumFont(Holder holder, TextView view) {
    Typeface font = 
    Typeface.createFromAsset(holder.itemView.getContext().getAssets(), 
   "font/AvenirLTStd_Medium.otf");
    view.setTypeface(font);
}

}

Try this one

YourActivity.this.runOnUiThread(new Runnable() {
         public void run() {
                 setRecyclerData();
         }
});

   private void setRecyclerData(){
        VehiclesDriverAdpter acceptedRequestAdapter = new VehiclesDriverAdpter(list);
        recyclerView.setAdapter(acceptedRequestAdapter);
        acceptedRequestAdapter.notifyDataSetChanged();
   }

Android Gson Tutorial: from JSON to RecyclerView, The resulting parsed object will be used to populate a RecyclerView. JSON stands for JavaScript Object Notation and is use for data interchange between 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.

I think you have forgotten to add layout manager on your recyclerView add it in code like this

    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getBaseContext());
    recyclerView.setLayoutManager(mLayoutManager);
    VehiclesDriverAdpter acceptedRequestAdapter = new  VehiclesDriverAdpter(list);
    recyclerView.setAdapter  (acceptedRequestAdapter);

Setting up Gson to parse JSON for a RecyclerView (Ecommerce #10 , We will also be using GSON to serialize and de-serialize our JSON object. in our Android application, we need to set the annotation style to GSON too. Creating the RecyclerView and adding RecyclerAdapter data binding. Android Json Parsing using Gson and RecyclerView In this tutorial, we are going to learn how to parse Json object in android application using Gson. The resulting parsed object will be used to populate a RecyclerView.

You put data to List<VehicleList> list list, but take it from List<Vehicle> list1; You can try(but only if you always have one item in VehicleList) something like this

public VehiclesDriverAdpter(List<VehicleList> list) {
  if(!list.isEmpty()){
   this.list1 = list.get(0);
  }
 }

Android Json Parsing using Gson and RecyclerView, RecyclerView has proven to be a good and handy android tool for displaying How to parse an image response data to a View with Picasso To set it up we'll need two things in this class, one is an adapter and the second  In this video we learn, how we can save a List of custom Objects to Shared Preferences using Google’s GSON Library. For this purpose we will turn the ArrayList into a JSON and save it as a String using the putString method.

Using Retrofit 2 and GSON for parsing JSON in Android, In this tutorial we are going to learn how to parse JSON data from URL and display it in RecyclerView using Retrofit. compile 'com.squareup.retrofit2:​converter-gson:2.0.0-beta4'. The other dependencies Finally a new DataAdapter object is created and set to RecyclerView using setAdapter() method. Hello everyone, This article about retrieve data from the server in recyclerview using a retrofit. In this article, I will show you how to retrieve data from the server and display it in Recyclerview. You also learned previews Tutorial about Fetching JSON data using volley library. In This Project, we are using Retrofit Library.

Parsing remote JSON to RecyclerView, Handling JSON using gson library in android with example. by getting data from JSONObject and setting it to java object you want to create. to list of coupon objects and supplied to recycler view to display it on the screen. Handling JSON Using Gson in Android. May 25, 2017 by Srinivas. The most widely used data interchange format for exchanging data between android app and app services on server is JSON format. So it is very important for android app developers to learn how to parse JSON string into java objects and how to create JSON string from java objects.

Android RecyclerView JSON Parsing using Retrofit, Kotlin – How to convert Object to/from JSON by Gson with Kotlin language Leveraging the Gson Library. y. kotlin retrofit android example with Recyclerview I have @Retention(value=RUNTIME) that may have been set on the Gson instance. Parsing JSON using GSON + JSON Parser Plugin + JSON Data Class  In our previous tutorials we learned to implement a simple RecyclerView and loading image in RecyclerView using Picasso. In this tutorial we are going to learn how to parse JSON data from URL and display it in RecyclerView using Retrofit.

Comments
  • Are u getting a blank layout?
  • yes, i get blank layout
  • @DharmiPatel please post your recycle activity code
  • if you are using retrofit using rxjava, than execute last notify code in UI thread, may be in runonuithread
  • i edit my code @RahulChokshi
  • It would've been a better approach to request the code within the MainActivity before making assumptions on what has and hasn't been implemented.
  • Yes you are right but in the provided code by Dharmi , It has only the layout manager issue with recyclerView , as he is already updating view after success response on main thread, I am not sure but i am assuming by his code
  • Can you please post complete code of your activity?
  • are you getting List<VehicleList> list is not empty ?, because in your json there are some fields missing from first json object