2017-06-15 88 views
-1

我有一個返回JSON對象的REST方法,JSON文件大小几乎重7MB,並且有近4600個JSON對象。我無法一次將整個數據解析到recyclerView中,因爲它會導致OutOfMemory異常。如何使用大json數據填充RecyclerView

我試圖用兩種方式實現它。一種是使用http://loopj.com/android-async-http/,但這種方法不適用於大塊數據。第二種方法是使用GSON和Volley完成的。使用這種方法,我能夠從REST方法獲得更快的響應,但我無法將其填充到recyclerView中。請檢查我使用的代碼。

的問題,即我面對這裏是recyclerview示出了用於ROLL_NUM,CURRENT_CLASS空值,STUDENT_NAME

樣品JSON響應:{ 「RestResult」:[{ 「」 ROLL_NUM 「:7071,」 CURRENT_CLASS「: 「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); 
+0

使用分頁 –

回答

0

試試這個來實現分頁中recyclerview這樣創造網頁服務根據分頁要求

private boolean loading = true; 
    int pastVisiblesItems, visibleItemCount, totalItemCount; 

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() 
{ 
    @Override 
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
    { 
     if(dy > 0) //check for scroll down 
     { 
      visibleItemCount = mLayoutManager.getChildCount(); 
      totalItemCount = mLayoutManager.getItemCount(); 
      pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition(); 

      if (loading) 
      { 
       if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) 
       { 
        loading = false; 
        Log.v("...", "last element of recyclerview !"); 
        //Do pagination.. and fetch new data from here 
       } 
      } 
     } 
    } 
}); 
0

您可以使用滾動監聽器是這樣的:

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); 
} 

,並在你的片段(任何地方,你有你的recyclerView)的活動: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 
     } 
    };