0

I have a REST method that returns JSON object,the JSON file size almost weighs 7MB and has almost 4600 JSON object. I am unable to parse the entire data at a time into recyclerView as it causes OutOfMemory Exception.

I tried to implement it in 2 ways. One is using http://loopj.com/android-async-http/ but this method is not feasible to large chunk of data. The second method was done using GSON and Volley. Using this method i am able to get the response much faster from the REST method but i'm unable to populate it into recyclerView. Please do check out the code that i used.

The problem that i'm facing here is that recyclerview shows null values for ROLL_NUM, CURRENT_CLASS, STUDENT_NAME

sample JSON response: {"RestResult":[{""ROLL_NUM":7071,"CURRENT_CLASS":"Grade LKG C","STUDENT_NAME":"A. VEDANJALI AKULA VEDANJALI"}

ListItem.class

String STUDENT_NAME;

String CURRENT_CLASS;

String ROLL_NUMBER;

public String getSTUDENT_NAME() {
    return STUDENT_NAME;
}

public String getCURRENT_CLASS() {
    return CURRENT_CLASS;
}

public String getROLL_NUMBER() {
    return ROLL_NUMBER;
}

public GetSet(String STUDENT_NAME, String CURRENT_CLASS, String ROLL_NUMBER) {
    this.STUDENT_NAME = STUDENT_NAME;
    this.CURRENT_CLASS = CURRENT_CLASS;
    this.ROLL_NUMBER = ROLL_NUMBER;
}

RecyclerAdapter.class

 private List<GetSet> itemList;
private Context context;

public RecyclerViewAdapter(Context context, List<GetSet> itemList) {
    this.itemList = itemList;
    this.context = context;
}

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
    RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
    return rcv;
}

@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
    holder.songTitle.setText("Student Name: " + itemList.get(position).getSTUDENT_NAME());
    holder.songYear.setText("Current Class: " + itemList.get(position).getCURRENT_CLASS());
    holder.songAuthor.setText("Roll Number: " + itemList.get(position).getROLL_NUMBER());
}

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

ActivityMain.class

RequestQueue queue = Volley.newRequestQueue(this);
    String url ="http://webservices.educatemax.com/vidyaMandir.svc/RetrieveStudentsList?iGroupID=1&iSchoolID=1&iBoardID=1&iClassID=1";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progressDialog.hide();
            Log.d(TAG, "Response " + response);
            GsonBuilder builder = new GsonBuilder();
            Gson mGson = builder.create();
            List<GetSet> posts = new ArrayList<>();


            posts = Arrays.asList(mGson.fromJson(response, GetSet.class));
            adapter = new RecyclerViewAdapter(ActivityMain.this, posts);
            recyclerView.setAdapter(adapter);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Error " + error.getMessage());
        }
    });
    queue.add(stringRequest);
Rahul
  • 45
  • 9

1 Answers1

0

you can use an scroll listener like this :

public abstract class RecyclerViewOnScrollListener extends RecyclerView.OnScrollListener {
private final int mVisibleThreshold = 1;
private int mPreviousTotal = 0;
private boolean isLoading = true;
private int mFirstVisibleItem;
private int mVisibleItemsCount;
private int mTotalItemsCount;

private int mCurrentPage = 0;
private LinearLayoutManager mLayoutManager;

public RecyclerViewOnScrollListener(LinearLayoutManager manager) {
    this.mLayoutManager = manager;
}

public void init() {
    this.mPreviousTotal = 0;
    this.isLoading = true;
    this.mCurrentPage = 0;
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    mVisibleItemsCount = recyclerView.getChildCount();
    mTotalItemsCount = mLayoutManager.getItemCount();
    mFirstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();

    if (isLoading && mTotalItemsCount > mPreviousTotal) {
        isLoading = false;
        mPreviousTotal = mTotalItemsCount;
    }
    if (!isLoading && (mTotalItemsCount - mVisibleItemsCount) <= (mFirstVisibleItem + mVisibleThreshold)) {
        mCurrentPage = mCurrentPage + 10;
        onLoadMore(mCurrentPage);
        isLoading = true;
    }
}

public abstract void onLoadMore(int currentPage);
}

and in your Activity of Fragment(anyWhere you have your recyclerView):

        mRecyclerOnScrollListener = new RecyclerViewOnScrollListener(new GridLayoutManager(MyActivity.this, getResources().getInteger(R.integer.num_columns))) {
        @Override
        public void onLoadMore(int currentPage) {
            //here you can manage content and add to your adaprer 
        }
    };
Meikiem
  • 1,876
  • 2
  • 11
  • 19