sumit kumar sumit kumar - 4 months ago 51
Android Question

how to do i want to put 2-3 recycleview in single activity using volly library

please help to rectify this problem....i want to put 2-3 recycleview in single activity using volly library i implement but second recycleview is not showing

these class containe 2-3 recycleview with gridviewlayout...in which i want to show galleries with particular text
1st gallery show 5-10 images with header text,2nd gallery will also show images with gallery header text,same with 3rd one....but after doing this
1rd recylceview is showing and second one/3rd one is not showing

Gallery_byVolly.java

package com.example.gallery;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.example.admin.app_shewale.R;
import com.example.vikaskame.VikasGallery;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Gallery_byVolly extends AppCompatActivity {


RecyclerView gallerygridview1;
TextView gallerytext;
List<VikasGallery> gallerylist1;
private RequestQueue requestQueue,requestQueue2,requestQueue3;
CardAdapter adapter,adapter2,adapter3;
private int requestCount = 1;
public static final String DATA_URL="http://1biz.in/shevale_app/android/shevale_gallery.php?page=";
public static final String DATA_UR1= "http://1biz.in/shevale_app/android/shevale_gallery.php?page=";
public static final String DATA_URL2= "http://1biz.in/shevale_app/android/shevale_gallery3.php?page=";
private RecyclerView.LayoutManager layoutManager,layoutManager2,layoutManager3;
Context context;


RecyclerView vollygallerygridview2;
List<VikasGallery> vollygallerylist2;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_by_volly);

requestQueue = Volley.newRequestQueue(this);
gallerylist1 = new ArrayList<>();
gallerygridview1 = (RecyclerView) findViewById(R.id.vollygallerygridview);
gallerygridview1.setHasFixedSize(true);
layoutManager = new GridLayoutManager(this,2);
gallerygridview1.setLayoutManager(layoutManager);
getData();
adapter = new CardAdapter(gallerylist1, this);
//Adding adapter to recyclerview
gallerygridview1.setAdapter(adapter);
gallerygridview1.addOnItemTouchListener(

new Recylceviewitelclick(context, new Recylceviewitelclick.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
System.out.println("value of this is:" + position);
Toast.makeText(Gallery_byVolly.this, "value of this is" + position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), Second_Full_Gallery.class);
Bundle bundle = new Bundle();
bundle.putInt("vollygalleryposition", position);
bundle.putSerializable("listimages", (Serializable) gallerylist1);
i.putExtras(bundle);
System.out.println("value oofd getpostion" + position);
startActivity(i);
}
})
);
gallerygridview1.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isLastItemDisplaying(gallerygridview1)) {
//Calling the method getdata again
getData();
}
}
});
vollygallerygridview2=(RecyclerView)findViewById(R.id.vollygallerygridview2);
vollygallerygridview2.setHasFixedSize(true);
vollygallerylist2=new ArrayList<>();
layoutManager2 = new GridLayoutManager(this,2);
vollygallerygridview2.setLayoutManager(layoutManager2);
getData2();
adapter2 = new CardAdapter(vollygallerylist2, this);
//Adding adapter to recyclerview
vollygallerygridview2.setAdapter(adapter2);
vollygallerygridview2.setOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState)
{
super.onScrollStateChanged(recyclerView, newState);
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (isLastItemDisplaying(vollygallerygridview2))
{
//Calling the method getdata again
getData2();
}
}
});

}

private void getData2()
{
requestQueue.add(getDataFromServer2(requestCount));
requestCount++;
}

private JsonArrayRequest getDataFromServer2(int requestCount)
{


JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL2 + String.valueOf(requestCount),new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response)
{
//Calling method parseData to parse the json response
System.out.println("value of response2 is"+response);
parseData2(response);
//Hiding the progressbar
// progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
// progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(Gallery_byVolly.this, "No More Items Available", Toast.LENGTH_SHORT).show();
}
});

//Returning the request
return jsonArrayRequest;
}

private JsonArrayRequest getDataFromServer(int requestCount)
{
//Initializing ProgressBar
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//Displaying Progressbar
progressBar.setVisibility(View.VISIBLE);
setProgressBarIndeterminateVisibility(true);
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL + String.valueOf(requestCount),new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response)
{
//Calling method parseData to parse the json response
System.out.println("value of response is"+response);
parseData(response);
//Hiding the progressbar
progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(Gallery_byVolly.this, "No More Items Available", Toast.LENGTH_SHORT).show();
}
});

//Returning the request
return jsonArrayRequest;
}

private void parseData2(JSONArray response)
{
for (int i = 0; i < response.length(); i++)
{
//Creating the superhero object
VikasGallery superHero = new VikasGallery();
JSONObject json = null;
try {
//Getting json
json = response.getJSONObject(i);
//Adding data to the superhero object
superHero.setImagesurl(json.getString(Config.TAG_IMAGE_URL));
}
catch (JSONException e)
{
e.printStackTrace();
}
//Adding the superhero object to the list
vollygallerylist2.add(superHero);
System.out.println("value of list2" + vollygallerylist2);
}

//Notifying the adapter that data has been added or changed
adapter2.notifyDataSetChanged();

}

//This method will get data from the web api
private void getData()
{
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}

//This method will parse json data
private void parseData(JSONArray array)
{
for (int i = 0; i < array.length(); i++)
{
//Creating the superhero object
VikasGallery superHero = new VikasGallery();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);
//Adding data to the superhero object
superHero.setImagesurl(json.getString(Config.TAG_IMAGE_URL));
}
catch (JSONException e)
{
e.printStackTrace();
}
//Adding the superhero object to the list
gallerylist1.add(superHero);
System.out.println("value of list inside parse m" + gallerylist1);
}

//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}
private boolean isLastItemDisplaying(RecyclerView recyclerView)
{
if (recyclerView.getAdapter().getItemCount() != 0)
{

int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
return true;
}
return false;
//Overriden method to detect scrolling
// public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
// {
// //Ifscrolled at last then
// if (isLastItemDisplaying(gallerygridview1))
// {
//
// getData();
// }
// }
}


adapter class

this is the adapter class of gallery class its basically show images in recycleview ( in listitems) after click on images call next activity that show full images.

package com.example.gallery;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.admin.app_shewale.R;
import com.example.vikaskame.VikasGallery;

import java.util.List;

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>
{

//Imageloader to load image
private ImageLoader imageLoader;
private Context context;
//List to store all superheroes
List<VikasGallery> superHeroes;
//Constructor of this class
public CardAdapter(List<VikasGallery> superHeroes, Context context){
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
System.out.println("value of list adapetr" + superHeroes);

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.vollyitemlist, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{

//Getting the particular item from the list
VikasGallery superHero = superHeroes.get(position);
System.out.println("value of lis is sumit kumawat"+superHeroes);
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImagesurl(), ImageLoader.getImageListener(holder.imageView, R.drawable.vikas1, android.R.drawable.ic_dialog_alert));
//Showing data on the views
holder.imageView.setImageUrl(superHero.getImagesurl(), imageLoader);

}

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

class ViewHolder extends RecyclerView.ViewHolder
{
//Views
public NetworkImageView imageView;

//Initializing Views
public ViewHolder(View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.vollyimage);

}
}

}


Logcat
this is the logatcat that is show the error

07-27 07:20:47.113 760-3040/? I/ActivityManager: START u0 {cmp=com.example.admin.app_shewale/com.example.gallery.Gallery_byVolly} from uid 10167 on display 0
07-27 07:20:47.116 760-820/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
07-27 07:20:47.129 12543-12543/? I/System.out: value of list adapetr[]
07-27 07:20:47.204 12543-12562/? W/EGL_emulation: eglSurfaceAttrib not implemented
07-27 07:20:47.204 12543-12562/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xeb8c8c80, error=EGL_SUCCESS
07-27 07:20:47.219 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.219 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.589 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.589 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.621 760-786/? I/ActivityManager: Displayed com.example.admin.app_shewale/com.example.gallery.Gallery_byVolly: +503ms
07-27 07:20:47.654 12543-12543/? I/System.out: value of response is[{"image":"http:\/\/res.cloudinary.com\/djsjqj6l6\/image\/upload\/v1468322269\/gallery\/ashtvinayakdarshan\/gallery1.jpg"},{"image":"http:\/\/res.cloudinary.com\/djsjqj6l6\/image\/upload\/v1468322270\/gallery\/ashtvinayakdarshan\/gallery2.jpg"},{"image":"http:\/\/res.cloudinary.com\/djsjqj6l6\/image\/upload\/v1468322271\/gallery\/ashtvinayakdarshan\/gallery3.jpg"},{"image":"http:\/\/res.cloudinary.com\/djsjqj6l6\/image\/upload\/v1468322272\/gallery\/ashtvinayakdarshan\/gallery4.jpg"}]
07-27 07:20:47.654 12543-12543/? I/System.out: value of list inside parse m[com.example.vikaskame.VikasGallery@1e1d2d90]
07-27 07:20:47.654 12543-12543/? I/System.out: value of list inside parse m[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689]
07-27 07:20:47.654 12543-12543/? I/System.out: value of list inside parse m[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e]
07-27 07:20:47.654 12543-12543/? I/System.out: value of list inside parse m[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.655 12543-12543/? I/System.out: value of lis is sumit kumawat[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.656 12543-12543/? I/System.out: value of lis is sumit kumawat[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.656 12543-12543/? I/System.out: value of lis is sumit kumawat[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.657 12543-12543/? I/System.out: value of lis is sumit kumawat[com.example.vikaskame.VikasGallery@1e1d2d90, com.example.vikaskame.VikasGallery@3ae7689, com.example.vikaskame.VikasGallery@305c668e, com.example.vikaskame.VikasGallery@1e6b1eaf]
07-27 07:20:47.658 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.658 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.995 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:47.995 12543-12543/? E/RecyclerView: No adapter attached; skipping layout
07-27 07:20:48.108 12543-12562/? W/EGL_emulation: eglSurfaceAttrib not implemented
07-27 07:20:48.109 12543-12562/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe245b560, error=EGL_SUCCESS

Answer

First, you use awful variable naming style. Never use digits (1..2...3..) with your repeating variables.

Your logcat show you, that one of the recycler view have not an adapter. I found only one adapter, but I didn't see another. You call adapter2.notifyDataSetChanged(); but did not initialize it.

Edit.

 //initializing our adapter
        layoutManager = new GridLayoutManager(this,2);
        gallerygridview1.setLayoutManager(layoutManager);
        getData();
        adapter = new CardAdapter(gallerylist1, this);

Where is a second?

Comments