0

Im fetching json into recyclerview using volley but It says /RecyclerView: No adapter attached; skipping layout, but it shows one row on the emulator which is the last json object. I run the Debug and the setter and getter variabe just received the last object.

I dont know how to resolve this. Been dealing with this all day, im a begginer,

this is my code on Main

public class MainActivity extends Activity {



     TextView text;
        String image_sm;
        private List<SuperHeroes> listSuperHeroes;
       // ArrayList<String> image_smm;
        //Creating Views
        private RecyclerView recyclerView;
       RecyclerView.LayoutManager layoutManager;
        private RecyclerView.Adapter adapter;
        SuperHeroes superHero = new SuperHeroes();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            recyclerView.setHasFixedSize(true);
            layoutManager = new LinearLayoutManager(this);


            listSuperHeroes = new ArrayList<>();

            getData();
        }


        private void getData(){
            //Showing a progress dialog



            JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,Config.DATA_URL,null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject req) {
                            //Dismissing progress dialog
                            final ProgressDialog loading = ProgressDialog.show(MainActivity.this,"Loading Data", "Please wait...",false,false);
                            loading.dismiss();

                            //calling method to parse json array
                            parseData(req);

                            listSuperHeroes.add(superHero);
                            recyclerView.setLayoutManager(layoutManager);
                            adapter = new CardAdapter(listSuperHeroes, MainActivity.this);
                            recyclerView.setAdapter(adapter);
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d("HELLO BIGBENTA", "Error: " + error.getMessage());

                        }
                    });

    //        //Creating request queue
    //        RequestQueue requestQueue = Volley.newRequestQueue(this);
    //
    //        //Adding request to the queue
    //        requestQueue.add(req);

            AppController.getInstance().addToRequestQueue(req);
        }


        private void parseData(JSONObject req) {

            try {
                // Parsing json array response
                // loop through each json object


                JSONArray json = req
                        .getJSONArray("worldpopulation");


                for (int i = 0; i < json.length(); i++) {

                    JSONObject jsonOb = json
                            .getJSONObject(i);

                    superHero.setrank(jsonOb.getInt(Config.TAG_rank));
                    superHero.setcountry(jsonOb.getString(Config.TAG_country));
                    superHero.setpopulation(jsonOb.getString(Config.TAG_population));
                    superHero.setflag(jsonOb.getString(Config.TAG_flag));
                    //superHero.setFirstAppearance(json.getString(Config.TAG_image_sm));


                }




            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }

This is my Setter and Getters Class

public class SuperHeroes {


    private int rank;
    private String country;
    private String population;
    private String flag;


    public int getrank() {
        return rank;
    }

    public void setrank(int rank) {
        this.rank = rank;
    }



    public String getcountry() {
        return country;
    }

    public void setcountry(String country) {
        this.country = country;
    }

    public String getpopulation() {
        return population;
    }

    public void setpopulation(String population) {
        this.population = population;
    }

    public String getflag() {
        return flag;
    }

    public void setflag(String flag) {
        this.flag = flag;
    }


}

This is the Config class

     public class Config {
    public static final String DATA_URL = "http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";

    //Tags for my JSON

    public static final String TAG_rank= "rank";
    public static final String TAG_country = "country";
    public static final String TAG_population = "population";
    public static final String TAG_flag = "flag";
    public static final String TAG_image_sm= "image_sm";

}

And this is the adapter class

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

    private ImageLoader imageLoader;
    private Context context;

    //List of superHeroes
    List<SuperHeroes> superHeroes;

    public CardAdapter(List<SuperHeroes> superHeroes, Context context){
        super();
        //Getting all the superheroes
        this.superHeroes = superHeroes;
        this.context = context;
    }

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

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

        SuperHeroes superHero =  superHeroes.get(position);

        imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
        imageLoader.get(superHero.getflag(), ImageLoader.getImageListener(holder.imageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));

        holder.imageView.setImageUrl(superHero.getflag(), imageLoader);

        holder.textViewRank.setText(String.valueOf(superHero.getrank()));
        holder.textViewRealName.setText(superHero.getcountry());
        holder.textViewCreatedBy.setText(superHero.getpopulation());

    }

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

    class ViewHolder extends RecyclerView.ViewHolder{
        public NetworkImageView imageView;
        public TextView textViewName;
        public TextView textViewRank;
        public TextView textViewRealName;
        public TextView textViewCreatedBy;
        public TextView textViewFirstAppearance;
        public TextView  textViewPowers;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
            textViewName = (TextView) itemView.findViewById(R.id.textViewName);
            textViewRank= (TextView) itemView.findViewById(R.id.textViewRank);
            textViewRealName= (TextView) itemView.findViewById(R.id.textViewRealName);
            textViewCreatedBy= (TextView) itemView.findViewById(R.id.textViewCreatedBy);
            textViewFirstAppearance= (TextView) itemView.findViewById(R.id.textViewFirstAppearance);
            textViewPowers= (TextView) itemView.findViewById(R.id.textViewPowers);
        }
    }

1 Answers1

1

Try to set an empty adapter first then update it when you have your data

For further explanation please visit :

recyclerview No adapter attached; skipping layout

No adapter attached; skipping layout

Community
  • 1
  • 1
Ahlem Jarrar
  • 1,129
  • 1
  • 12
  • 33
  • I tried using adapter.notifyDataSetChanged(); but nothing is showing up on the emulator. I run a debug and this "listSuperHeroes.add(superHero);" code on MainActivity, the "superhero" there only has one object, JSON fetching has no problem, it fetches ALL the objects but it only saves ONE object on that variable "superhero" which is the last object – Edmer Alarte Aug 18 '16 at 16:04
  • it just saves one object on "superhero" variable – Edmer Alarte Aug 18 '16 at 16:13
  • visit the links below i think they will be very useful for you ! – Ahlem Jarrar Aug 18 '16 at 16:17
  • ive read that already tried notifyDataSetChanged() but didnt work, not rows at all – Edmer Alarte Aug 18 '16 at 16:50