Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在片段中实现无休止的listview?_Android_Listview_Android Fragments_Fragment_Endlessscroll - Fatal编程技术网

Android 如何在片段中实现无休止的listview?

Android 如何在片段中实现无休止的listview?,android,listview,android-fragments,fragment,endlessscroll,Android,Listview,Android Fragments,Fragment,Endlessscroll,我正在片段中实现无限的listview。当我为我的listview编写setOnScrollListener的代码时,我的应用程序因错误而崩溃,适配器的内容已更改,但listview未收到通知。确保适配器的内容不是从后台线程修改的,而是仅从UI线程修改的。确保您的适配器在其内容更改时调用notifyDataSetChanged()。我已经尝试了几乎所有的方法在我的适配器上应用了notifyDataSetChanged()。请帮我解决这个问题 下面是我的代码 @Override publ

我正在片段中实现无限的listview。当我为我的listview编写
setOnScrollListener
的代码时,我的应用程序因错误
而崩溃,适配器的内容已更改,但listview未收到通知。确保适配器的内容不是从后台线程修改的,而是仅从UI线程修改的。确保您的适配器在其内容更改时调用notifyDataSetChanged()。
我已经尝试了几乎所有的方法在我的适配器上应用了
notifyDataSetChanged()
。请帮我解决这个问题

下面是我的代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_all_campaign, container, false);
        allCampaignList = (ListView) rootView.findViewById(R.id.allCampaignList);
        adapter = new CampaignListAdapter(getActivity(), CampaignDataArrayList);
        loadCampaignsData(offsetValue);

        allCampaignList.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScroll(AbsListView view,
                                 int firstVisibleItem, int visibleItemCount,
                                 int totalItemCount) {
                //Algorithm to check if the last item is visible or not
                final int lastItem = firstVisibleItem + visibleItemCount;
                if (lastItem == totalItemCount) {
                    // you have reached end of list, load more data
                    loadCampaignsData(offsetValue + 1);
                    offsetValue++;
                }
            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                //blank, not using this
            }
        });

        return rootView;
    }
下面是LoadActivityData()的方法:

下面是为获取webservice而编写的asynctask

private class GetAllCampaign extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(Constants.DADAMI_URL + Constants.ALL_CAMPAIGN);

                List<NameValuePair> list = new ArrayList<NameValuePair>();
                list.add(new BasicNameValuePair("cat_id", "0"));
                list.add(new BasicNameValuePair("user_id", ""));
                list.add(new BasicNameValuePair("offset", params[0]));

                httpPost.setEntity(new UrlEncodedFormEntity(list));
                HttpResponse httpResponse = httpClient.execute(httpPost);

                HttpEntity httpEntity = httpResponse.getEntity();
                return readResponse(httpResponse);
                //return null;
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            hideDialog();
            if (result == null) {
                Toast.makeText(context, "Something went wrong.. Please try again..!!", Toast.LENGTH_LONG).show();
            } else {
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {

                        allCampaignList.setAdapter(adapter);

                    }
                });


            }
        }
    }

    private ArrayList<HashMap<String, String>> readResponse(HttpResponse res) {
        InputStream is = null;
        try {
            is = res.getEntity().getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }

            JSONObject mainObj = new JSONObject(sb.toString());
            JSONArray fundraiser_data = null;

            fundraiser_data = mainObj.getJSONArray("fundraiser_data");

            for (int i = 0; i < fundraiser_data.length(); i++) {
                JSONObject elem = fundraiser_data.getJSONObject(i);

                String fundraiser_photo = elem.getString("fundraiser_photo");
                String title = elem.getString("title");
                String fullname = elem.getString("fullname");


                HashMap<String, String> campaignData = new HashMap<>();

                campaignData.put("fundraiser_photo", Constants.DADAMI_IMAGE_URL + fundraiser_photo);
                campaignData.put("title", title);
                campaignData.put("fullname", fullname);


                CampaignDataArrayList.add(campaignData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return CampaignDataArrayList;

    }
私有类GetAllCampaign扩展异步任务{
@凌驾
受保护的ArrayList doInBackground(字符串…参数){
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(Constants.DADAMI_URL+Constants.ALL_活动);
列表=新的ArrayList();
添加(新的BasicNameValuePair(“类别id”,“0”);
添加(新的BasicNameValuePair(“用户id”);
添加(新的BasicNameValuePair(“偏移量”,参数[0]);
setEntity(新的UrlEncodedFormEntity(列表));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
返回readResponse(httpResponse);
//返回null;
}捕获(异常){
异常。printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
hideDialog();
如果(结果==null){
Toast.makeText(上下文,“出现问题..请重试..!!”,Toast.LENGTH_LONG.show();
}否则{
getActivity().runOnUiThread(新的Runnable()){
公开募捐{
AllActivityList.setAdapter(适配器);
}
});
}
}
}
专用ArrayList readResponse(HttpResponse res){
InputStream=null;
试一试{
is=res.getEntity().getContent();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(is));
StringBuffer sb=新的StringBuffer();
字符串行=”;
而((line=bufferedReader.readLine())!=null){
某人附加(行);
}
JSONObject mainObj=新的JSONObject(sb.toString());
JSONArray筹款人_data=null;
募捐者数据=mainObj.getJSONArray(“募捐者数据”);
对于(int i=0;i
使用Recyclerview以更好地接近
处理程序中的所有代码必须在UI线程中运行,您可以使用
runOnUIThread
或使用
新处理程序(Looper.getMainLooper())创建处理程序对象使处理程序成为UI处理程序
private class GetAllCampaign extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(Constants.DADAMI_URL + Constants.ALL_CAMPAIGN);

                List<NameValuePair> list = new ArrayList<NameValuePair>();
                list.add(new BasicNameValuePair("cat_id", "0"));
                list.add(new BasicNameValuePair("user_id", ""));
                list.add(new BasicNameValuePair("offset", params[0]));

                httpPost.setEntity(new UrlEncodedFormEntity(list));
                HttpResponse httpResponse = httpClient.execute(httpPost);

                HttpEntity httpEntity = httpResponse.getEntity();
                return readResponse(httpResponse);
                //return null;
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            hideDialog();
            if (result == null) {
                Toast.makeText(context, "Something went wrong.. Please try again..!!", Toast.LENGTH_LONG).show();
            } else {
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {

                        allCampaignList.setAdapter(adapter);

                    }
                });


            }
        }
    }

    private ArrayList<HashMap<String, String>> readResponse(HttpResponse res) {
        InputStream is = null;
        try {
            is = res.getEntity().getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line);
            }

            JSONObject mainObj = new JSONObject(sb.toString());
            JSONArray fundraiser_data = null;

            fundraiser_data = mainObj.getJSONArray("fundraiser_data");

            for (int i = 0; i < fundraiser_data.length(); i++) {
                JSONObject elem = fundraiser_data.getJSONObject(i);

                String fundraiser_photo = elem.getString("fundraiser_photo");
                String title = elem.getString("title");
                String fullname = elem.getString("fullname");


                HashMap<String, String> campaignData = new HashMap<>();

                campaignData.put("fundraiser_photo", Constants.DADAMI_IMAGE_URL + fundraiser_photo);
                campaignData.put("title", title);
                campaignData.put("fullname", fullname);


                CampaignDataArrayList.add(campaignData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return CampaignDataArrayList;

    }