Skip to content Skip to sidebar Skip to footer

Volley Is Not Showing The Data In The Recyclerview

I am trying to get data from a JSON File using volley method and show it in RecyclerView but its not showing any data on RecyclerView. mysql Databse is configured on xampp. Code is

Solution 1:

In Listener: Make an Interface or Listener as:

publicinterfaceOnRowItemClick {
    voidonClick(int position, PojoClass pojo);
}

In Adpter class:

  1. declare click listener object:

    privateOnRowItemClick mOnRowItemClick;
    privateList<PojoClass> imageList;
    
  2. get the object in constructor of adapter:

    public HomeCategoriesAdapter(Context mContext, OnRowItemClick mOnRowItemClick,
                             ArrayList<PojoClass> imageArrayList) {
    this.mContext = mContext;
    this.mOnItemClickListener = mOnItemClickListener;
    this.imageList = imageArrayList;
    

    }

  3. In bindView method, where you set your details:

    PojoClasspojo= imageList.get(position);
    viewHolder.parentLayout.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            mOnItemClickListener.onClick(position, pojo);
        }
    });
    

In MainActivity:

  1. Declare your adapter class:

    privateList<PojoClass> imageList;
      privateMyItemAdapter myItemAdapter;
    
  2. Initialize your adapter class:

      List<YourModel> modelItemList;
      modelItemList = new ArrayList<>();
      myItemAdapter = newMyItemAdapter(mContext, this, modelItemList);
      recyclerView.setAdapter(myItemAdapter);
    

After Service Call:

   imageList =new List<PojoClass>;

   PojoClass pojo=new PojoClass();
   pojo.setImageUrl(imageUrl);
   pojo.setImageName(imageName);
   imageList.add(pojo);
   adapter.setDataChange(imageList);

Solution 2:

In Adapter class write:

privateArrayList<String> mImageList;
publicvoidsetDataChange(ArrayList<String> imageList) {
    mImageList = newArrayList<>(imageList);
    notifyDataSetChanged();
}

Call this method after getting successful result on Volley response method that is inside onResponse(). Parse the result as per your List<String>, Create a list and add it into your global list and call:

adapter.setDataChange(imageList);

Or another way:

  1. Create pojo of imageUrl and imageName;

  2. private List<PojoClass> imageList;

After getting result in onResponse(), do like this:

imageList =new List<PojoClass>;

PojoClass pojo=new PojoClass();
pojo.setImageUrl(imageUrl);
pojo.setImageName(imageName);

imageList.add(pojo);

pass it over adapter like this:

adapter.setDataChange(imageList);

Post a Comment for "Volley Is Not Showing The Data In The Recyclerview"