尝试在recyclerview中调用notifydatasetchanged()时,视图持有者适配器无效

时间:2016-02-22 08:28:22

标签: android android-recyclerview

我的Recyclerview工作正常,我做的很简单,我从webervice获取数据并使用recyclerView显示它

这是我加载更多数据的代码

public void loadmoredata(int limit) {
    limit++;
    String url = "http://oilpeople.talenetic.com/api/SearchJob?limit="+limit+ "&jobkeyword=" + key + "&countrytext=" + coun + "&location=" +loc+ "&apikey=1111111111&siteid=1";
    JsonObjectRequest loadmore = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray(loadArray);
                jobTitle = new String[jsonArray.length()];
                salary = new String[jsonArray.length()];
                companyName = new String[jsonArray.length()];

                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject c = jsonArray.getJSONObject(i);

                    // Storing each json item in variable
                    if(loadArray.equals("Indeedjobslist")) {
                        String salaryLoc = c.getString("location");

                        jobTitle[i] = c.getString("jobtitle");
                        companyName[i] = c.getString("companyname");
                        salary[i] = salaryLoc;
                    } else {
                        String salaryLoc = c.getString("salary")+ " \u2022 " +c.getString("location");

                        jobTitle[i] = c.getString("jobtitle");
                        companyName[i] = c.getString("companyname");
                        salary[i] = salaryLoc;
                    }

                    // show the values in our logcat

                    m.setCompanyName(companyName);
                    m.setJobTitle(jobTitle);
                    m.setSalary(salary);

                    adapter.notifyDataSetChanged();
                }
            } catch(JSONException e) {

            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    Volley.newRequestQueue(SearchResults.this).add(loadmore);
}

adapter是一个全局变量。我正在尝试加载更多这样的数据:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.fTextView.setText(jobTitle[position]);
    holder.mTextView.setText(companyName[position]);
    holder.sTextView.setText(jobs[position]);

    if (position % 10 == 0) {
        int limit = position + 5;
        loadmoredata(limit);
    }
}

每当位置的百分比为0时,我会加载更多数据,但是我得到indexoutofBounds错误。

1 个答案:

答案 0 :(得分:0)

删除

if (position % 10 == 0) {
    int limit = position + 5;
    loadmoredata(limit);
}

并使用此方法调用以从您的主要活动中知道计数然后调用您的上面的鳕鱼,但我认为这将导致无限循环

public int getItemCount() {
    return mDataset.length;
}