0

When I execute the program, it shows a blank screen and in logcat it shows no adapter attached; skipping layout. When I debug the code the ArrayList is not getting the value (returning 0).

public class MainActivity extends AppCompatActivity implements Constants, NetworkOperation, URL {

    LinearLayoutManager manager;
    ArrayList<OfferModal> bestoffers;
    RecyclerViewAdapter adapter;
    RecyclerView rv;
    FetchData fetchData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.recyclerview1);

        fetchData = new FetchData(this, this, CLOUD_SECTION);
        fetchData.fromServer();
        manager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        rv.setLayoutManager(new LinearLayoutManager(this));

    }

    @Override
    public void started() {

    }

    @Override
    public void doingBackground() {

    }

    @Override
    public void completed(JSONObject jsonObject) {

        bestoffers = new ArrayList<OfferModal>();

        try {

            JSONArray array_item = new JSONArray();
            for (int i = 0; i < array_item.length(); i++) {

                JSONObject itemobj = array_item.getJSONObject(i);
                String id = itemobj.getString(ID);
                String le_id = itemobj.getString(LE_ID);
                String title = itemobj.getString(TITLE);
                String description = itemobj.getString(DESCRIPTION);
                String wid = itemobj.getString(WID);
                String hgt = itemobj.getString(HGT);
                String tn_path = itemobj.getString(TN_PATH);
                String create_time = itemobj.getString(CREATE_TIME);
                String update_time = itemobj.getString(UPDATE_TIME);
                String view_count = itemobj.getString(VIEW_COUNT);
                String slide_count = itemobj.getString(SLIDE_COUNT);
                String shared = itemobj.getString(SHARED);
                String publish_ver = itemobj.getString(PUBLISH_VER);
                String publish_time = itemobj.getString(PUBLISH_TIME);
                String user_name = itemobj.getString(USER_NAME);
                String avatar_path = itemobj.getString(AVATAR_PATH);
                String comment_count = itemobj.getString(COMMENT_COUNT);
                String fav = itemobj.getString(FAV);
                String fav_count = itemobj.getString(FAV_COUNT);

                OfferModal off = new OfferModal(id, le_id, title, description, wid, hgt, tn_path, create_time, update_time, view_count, slide_count, shared, publish_ver, publish_time, user_name, avatar_path, comment_count, fav, fav_count);

                off.setId(id);
                off.setId(le_id);
                off.setTitle(title);
                off.setDescription(description);
                off.setHgt(hgt);
                off.setTn_path(tn_path);
                off.setCreate_time(create_time);
                off.setUpdate_time(update_time);
                off.setView_count(view_count);
                off.setSlide_count(slide_count);
                off.setShared(shared);
                off.setPublish_ver(publish_ver);
                off.setPublish_time(publish_time);
                off.setUser_name(user_name);
                off.setAvatar_path(avatar_path);
                off.setComment_count(comment_count);
                off.setFav(fav);
                off.setFav_count(fav_count);
                bestoffers.add(off);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        adapter = new RecyclerViewAdapter(getApplicationContext(), bestoffers);
        rv.setAdapter(adapter);

    }

}

This is my json data:

[
  {
    "id": "42057",
    "le_id": "568fb0d29a5eb",
    "title": "Bag the Big Deal Lulu Hypermarket",
    "description": "",
    "wid": "0",
    "hgt": "0",
    "tn_path": "",
    "create_time": "1452257490",
    "update_time": "1452332748",
    "view_count": "00000000669",
    "slide_count": "9",
    "shared": "F",
    "publish_ver": "11",
    "publish_time": "1452332761",
    "user_name": " Lulu Retail",
    "avatar_path": null,
    "comment_count": "0",
    "fav": "F",
    "fav_count": 0
  },
  {
    "id": "42019",
    "le_id": "568f9e45625da",
    "title": "Bag the Big Deal Lulu Fashion Store",
    "description": "",
    "wid": "0",
    "hgt": "0",
    "tn_path": "",
    "create_time": "1452252741",
    "update_time": "1452584969",
    "view_count": "00000000659",
    "slide_count": "1",
    "shared": "F",
    "publish_ver": "5",
    "publish_time": "1452584972",
    "user_name": " Lulu Retail",
    "avatar_path": null,
    "comment_count": "0",
    "fav": "F",
    "fav_count": 0
  },
  {
    "id": "42017",
    "le_id": "568f9e30df0da",
    "title": "Bag the Big Deal Lulu Connect",
    "description": "",
    "wid": "0",
    "hgt": "0",
    "tn_path": "",
    "create_time": "1452252720",
    "update_time": "1452585162",
    "view_count": "00000000726",
    "slide_count": "7",
    "shared": "F",
    "publish_ver": "7",
    "publish_time": "1452275301",
    "user_name": " Lulu Retail",
    "avatar_path": null,
    "comment_count": "0",
    "fav": "F",
    "fav_count": 0
  }
]

This is my adapter class:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {

    ArrayList<OfferModal> mBestoffers;
    Context context;
    //  List<Offers>card_items;

    RequestQueue mRequestQueue;
    ImageLoader mImageLoader;

    public RecyclerViewAdapter(Context context, ArrayList<OfferModal> mBestoffers) {
        this.context = context;
        // this.card_items = card_items;
        this.mBestoffers = mBestoffers;

        mRequestQueue = Volley.newRequestQueue(context);
        mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);

            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }

            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });

    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardlayout, null);
        RecyclerViewHolder rvh = new RecyclerViewHolder(view);

        return rvh;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
       //      holder.imageView.setImageUrl(mBestoffers.get(position).getAvatar_path(), mImageLoader);
       holder.tv1.setText(mBestoffers.get(position).getTitle());
       //write code to download image and data from server and set here

    }

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

Please help me to solve this issue, any help will be greately appriciated.

1
  • From your code its clear that your JsonArray is always empty,If the JsonArray is within your JsonObject parse that values. Commented Jan 16, 2016 at 11:23

3 Answers 3

1

Please paste jsonObject.toString() in completed().

Array empty reason is here.

       JSONArray array_item = new JSONArray();
        for (int i = 0; i < array_item.length(); i++) {
Sign up to request clarification or add additional context in comments.

3 Comments

but there is no json object declared.
your completed callback function has a JsonObject ,may be it contains a JsonArray,use JSONArray array = jsonObject.getJSONArray("key")
Please post what value your jsonObject contains in completed callback method.Try to put a Log.d("json",jsonObject.toString()) before bestoffers = new ArrayList<OfferModal>();
0

Please use this code it will help you.

public class MainActivity extends AppCompatActivity implements Constants, NetworkOperation, URL {

LinearLayoutManager manager;
ArrayList<OfferModal> bestoffers;
RecyclerViewAdapter adapter;
RecyclerView rv;
FetchData fetchData;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rv = (RecyclerView) findViewById(R.id.recyclerview1);

    fetchData = new FetchData(this, this, CLOUD_SECTION);
    fetchData.fromServer();
    manager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
    rv.setLayoutManager(new LinearLayoutManager(this));

}

@Override
public void started() {

}

@Override
public void doingBackground() {

}

@Override
public void completed(JSONObject jsonObject) {

    bestoffers = new ArrayList<OfferModal>();

    try {

        JSONArray array_item = new JSONArray();
        for (int i = 0; i < array_item.length(); i++) {

            JSONObject itemobj = array_item.getJSONObject(i);
            String id = itemobj.getString(ID);
            String le_id = itemobj.getString(LE_ID);
            String title = itemobj.getString(TITLE);
            String description = itemobj.getString(DESCRIPTION);
            String wid = itemobj.getString(WID);
            String hgt = itemobj.getString(HGT);
            String tn_path = itemobj.getString(TN_PATH);
            String create_time = itemobj.getString(CREATE_TIME);
            String update_time = itemobj.getString(UPDATE_TIME);
            String view_count = itemobj.getString(VIEW_COUNT);
            String slide_count = itemobj.getString(SLIDE_COUNT);
            String shared = itemobj.getString(SHARED);
            String publish_ver = itemobj.getString(PUBLISH_VER);
            String publish_time = itemobj.getString(PUBLISH_TIME);
            String user_name = itemobj.getString(USER_NAME);
            String avatar_path = itemobj.getString(AVATAR_PATH);
            String comment_count = itemobj.getString(COMMENT_COUNT);
            String fav = itemobj.getString(FAV);
            String fav_count = itemobj.getString(FAV_COUNT);

            OfferModal off = new OfferModal(id, le_id, title, description, wid, hgt, tn_path, create_time, update_time, view_count, slide_count, shared, publish_ver, publish_time, user_name, avatar_path, comment_count, fav, fav_count);

            off.setId(id);
            off.setId(le_id);
            off.setTitle(title);
            off.setDescription(description);
            off.setHgt(hgt);
            off.setTn_path(tn_path);
            off.setCreate_time(create_time);
            off.setUpdate_time(update_time);
            off.setView_count(view_count);
            off.setSlide_count(slide_count);
            off.setShared(shared);
            off.setPublish_ver(publish_ver);
            off.setPublish_time(publish_time);
            off.setUser_name(user_name);
            off.setAvatar_path(avatar_path);
            off.setComment_count(comment_count);
            off.setFav(fav);
            off.setFav_count(fav_count);
            bestoffers.add(off);

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    adapter = new RecyclerViewAdapter(MainActivity.this,R.layout.cardlayout, bestoffers);
    rv.setAdapter(adapter);

adapter.notifyDataSetChanged()

}

}

1 Comment

thanks for ur help.but that didnot work for me.still have same error,(No adapter attached; skipping layout)
0

Your main problem lies in the JSONArray, you are initialising to a new JSONArray, which is empty, please check what is the size of the array in the jsonArray.length(); I'm sure it is 0. That is why your arraylist is also empty.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.