我有一个返回JSON对象的REST方法,JSON文件大小几乎为7MB,并且有近4600个JSON对象。我无法一次解析整个数据到recyclerView,因为它导致OutOfMemory异常。
我尝试以两种方式实现它。一个是使用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)
你可以使用这样的滚动监听器:
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的地方):
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
}
};