简体   繁体   中英

RecyclerView: No adapter attached; skipping layout - However Adapter has been set

I have been reading the different answers here on stackoverflow and tried to implement their solutions but I am still getting the error: RecyclerView: No adapter attached; skipping layout

  • I have spent days on trying to troubleshoot but no luck any help will be appreciated.

public class GaneshMainActivity extends AppCompatActivity {
    private static final String URL_DATA = "https://mantraapp.000webhostapp.com/tag/ganesh-mantra?json=1";
    private RecyclerView recyclerView;
    private GaneshMyAdapter adapter;
    private List < GaneshListitem > ganeshListitem;



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

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

        ganeshListitem = new ArrayList < > ();


        load_data_from_server();
    }

    private void load_data_from_server() {
        StringRequest stringRequest = new StringRequest(Request.Method.GET,
            URL_DATA,
            new Response.Listener < String > () {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray array = jsonObject.getJSONArray("Ganesha Mantra");

                        for (int i = 0; i < array.length(); i++) {
                            JSONObject object = array.getJSONObject(i);
                            GaneshListitem data = new GaneshListitem(
                                object.getString("title"),
                                object.getString("content"),
                                object.getString("thumbnail")

                            );
                            ganeshListitem.add(data);
                        }

                    } catch (JSONException e) {
                        System.out.println("No content");
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_LONG).show();

                }
            });

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }
}

I am not sure what I am doing wrong below is GaneshMyadpater.java file

public class GaneshMyAdapter extends RecyclerView.Adapter < GaneshMyAdapter.ViewHolder > {
    private List < GaneshListitem > GaneshlistItems;
    private Context context;

    public GaneshMyAdapter(Context context, List < GaneshListitem > my_data) {
        this.context = context;
        this.GaneshlistItems = my_data;
    }


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

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


        holder.title.setText(GaneshlistItems.get(position).getHead());
        holder.content.setText(GaneshlistItems.get(position).getDesc());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {


        public TextView title;
        public TextView content;
        public ImageView thumbnail;

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

            title = (TextView) itemView.findViewById(R.id.textviewHead);
            content = (TextView) itemView.findViewById(R.id.textviewDesc);
            thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
        }
    }
}

Add this line in your onCreate

adapter = new GaneshAdapter(this , ganeshListitem);
recyclerView.setAdapter(adapter);

and call notifyDataSetChanged() when items are added in your list

adapter.notifyDataSetChanged(); 

this will update the recycleview with new data

ganeshListitem = new ArrayList < > ();

adapter = new GaneshAdapter(this , ganeshListitem);
recyclerView.setAdapter(adapter);

load_data_from_server();

make above changes at onCreate()

adapter.addData(ganeshListitem)

Add above line after ganeshListitem.add(data);

public void addData(List<GaneshListitem> list){
GaneshlistItems.addAll(list);
notifyDataSetChanged()
}

Add this above method to your adapter class

Add this code below “

recyclerView.setLayoutManager(new LinearLayoutManager(this));

” :

recycleView. setAdapter(adapter);

And add this code below “

JSONObject jsonObject = new JSONObject(response);
                        JSONArray array = jsonObject.getJSONArray("Ganesha Mantra");

                        for (int i = 0; 1 < array.length(); i++) {
                            JSONObject object = array.getJSONObject(i);
                            GaneshListitem data = new GaneshListitem(
                                object.getString("title"),
                                object.getString("content"),
                                object.getString("thumbnail")

                            );
                            ganeshListitem.add(data);
                        }

”:

adapter. notifyDataSetChanged();

You need to implement like this

  • you have to take arrayList type hashmap and initialise them ArrayList<HashMap<String,String>> allValues=new ArrayList<>();
  • then you've to put your response values in your hashmap like this
  • add this in your oncreate

    context = getActivity();
    RecyclerView.LayoutManager recyclerViewLayoutManager = new LinearLayoutManager(context);
    rv_lunch_break.setLayoutManager(recyclerViewLayoutManager);

 private void load_data_from_server() {
    StringRequest stringRequest = new StringRequest(Request.Method.GET,
        URL_DATA,
        new Response.Listener < String > () {
            @Override
            public void onResponse(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray array = jsonObject.getJSONArray("Ganesha Mantra");

                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = array.getJSONObject(i);
                       HashMap<String,String> hashMap=new HashMap<>();
                            hashMap.put("title",object.getString("title"));                                 
                         hashMap.put("content",object.getString("content"));                          
                     hashMap.put("thumbnail",object.getString("thumbnail"));
                           allValues.add(hashMap);
                             adapter=new yourAdapterName(context,allValues);
                             recyclerView.setAdapter(adapter);
                               adapter.notifyDataSetChanged();

                    }

                } catch (JSONException e) {
                    System.out.println("No content");
                }

            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), 
    volleyError.getMessage(), Toast.LENGTH_LONG).show();

            }
        });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

  • In your adapter class you have to take again arrayList type hashmap and hashmap and also initialise them also like this

    HashMap<String,String> hashMAp=new HashMap<>();
    private ArrayList<HashMap<String,String>> allList;

  • In your onBindViewHolder you need to extract value like this

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
          hashMAp=allList.get(position);
holder.your_TextView_Refrence.setText(hashmap.get("title"));
holder.your_TextView_Refrence.setText(hashmap.get("content"));
}

Happy to help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM