更新后适配器不更新列表视图

时间:2014-11-14 06:07:15

标签: android android-fragments

AsyncTask更新适配器内容但不更新列表视图。所有代码都运行正常,但是当我更新我的适配器时,我的listview没有反映适配器的任何更新。我也搜索了谷歌这个问题,但没有找到任何适当的解决方案。它显示如下例外: -

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification.

这是我的代码,请看看我应该改变什么: -

public class SearchResultScreen extends Fragment {

    ListView searchResult;
    SearchResultAdapter adapter;
    TextView total_jobs,displaying_jobs;
    int currentPage=0,threshold=10;
    ProgressDialog progress;
    JobService jobService;
    TextView load_more_jobs;
    int flag = 0;

    public SearchResultScreen(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.search_result_screen, container, false);
        searchResult = (ListView) rootView.findViewById(R.id.search_result);

        jobService = new JobService();

        jobService.setCurrent_page(currentPage);

        load_more_jobs = new TextView(getActivity());
        load_more_jobs.setText("Load More Jobs");
        load_more_jobs.setGravity(Gravity.CENTER_HORIZONTAL);
        load_more_jobs.setPadding(5,5,5,5);
        load_more_jobs.setBackgroundColor(Color.GRAY);


        Map params = new HashMap();
        params.put("skills", getArguments().getString("skills"));
        params.put("exp_from", getArguments().getString("exp_from"));
        params.put("exp_to", getArguments().getString("exp_to"));
        params.put("location", getArguments().getString("location"));
        params.put("func_area", getArguments().getString("functional_area"));
        params.put("start", jobService.getCurrent_page());


        total_jobs = (TextView) rootView.findViewById(R.id.total_jobs_label);
        displaying_jobs = (TextView) rootView.findViewById(R.id.displaying_jobs);
        progress = new ProgressDialog(getActivity());
        progress.setTitle("Please Wait!!");
        progress.setMessage("Wait!!");
        progress.setCancelable(false);
        progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);




        new MyAsyncTask(params, progress, searchResult, getActivity(), jobService, total_jobs, displaying_jobs, load_more_jobs, flag, adapter).execute("rest/search_jobs.rest");
        currentPage++;

        load_more_jobs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(currentPage < jobService.getTotal_page()) {
                    jobService.setCurrent_page(currentPage);

                    Map params = new HashMap();
                    params.put("skills", getArguments().getString("skills"));
                    params.put("exp_from", getArguments().getString("exp_from"));
                    params.put("exp_to", getArguments().getString("exp_to"));
                    params.put("location", getArguments().getString("location"));
                    params.put("func_area", getArguments().getString("functional_area"));
                    params.put("start", jobService.getCurrent_page());

                    flag = 1;
                    new MyAsyncTask(params, progress, searchResult, getActivity(), jobService, total_jobs, displaying_jobs, load_more_jobs, flag, adapter).execute("rest/search_jobs.rest");

                    currentPage++;
                }
            }
        });

        searchResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                JobSearchModel jobs = (JobSearchModel) adapterView.getItemAtPosition(i);
                FragmentManager fragmentManager = getFragmentManager();
                Fragment search_result_screen = new SearchResultScreen();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                JobDescription jobDescription = new JobDescription();

                Bundle args = new Bundle();
                args.putSerializable("jobs", jobs);

                jobDescription.setArguments(args);

                fragmentTransaction.replace(R.id.content_frame, jobDescription);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                //Toast.makeText(getActivity(), "Id is : " + jobs.getJob_id(), Toast.LENGTH_LONG).show();
            }
        });



        return rootView;
    }

    static class MyAsyncTask extends AsyncTask<String, Integer, String> {

        private Map params;
        private ProgressDialog progress;
        private ListView list;
        private Activity context;
        private JobService jobService;
        private TextView total_jobs;
        private TextView displaying_jobs;
        private TextView load_more_jobs;
        private int flag;
        private SearchResultAdapter adapter;

        Object[] object_response;

        public MyAsyncTask(Map params, ProgressDialog progress, ListView searchResult, Activity context, JobService jobService, TextView total_jobs, TextView displaying_jobs, TextView load_more_jobs, int flag, SearchResultAdapter adapter) {
            this.params = params;
            this.progress = progress;
            this.list = searchResult;
            this.context = context;
            this.jobService = jobService;
            this.total_jobs = total_jobs;
            this.displaying_jobs = displaying_jobs;
            this.load_more_jobs = load_more_jobs;
            this.flag = flag;
            this.adapter = adapter;
        }

        @Override
        public void onPreExecute() {
               this.progress.show();
        }

        public void onProgressUpdate(Integer filesDownloaded) {

        }

        protected String doInBackground(String... parameters) {
            postData(parameters[0]);
            return "done";
        }

        protected void onPostExecute(String result){
                  context.runOnUiThread(new Runnable() {
                      @Override
                      public void run() {

                          if(flag == 0) {
                              adapter = new SearchResultAdapter(context, jobService.getModel(object_response));
                              list.addFooterView(load_more_jobs, null, false);
                              list.setAdapter(adapter);
                          } else {
                              adapter = new SearchResultAdapter(context, jobService.getModel(object_response));
                          }

                          total_jobs.setText(""+jobService.getTotal_jobs());
                          displaying_jobs.setText(""+jobService.displayingJobs());
                      }
                  });
                  adapter.notifyDataSetChanged();
                  this.progress.dismiss();
        }
        protected void onProgressUpdate(String progress){

        }

        public void postData(final String request) {
                    RestTemplate restTemplate = new RestTemplate();
                    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
                    object_response = restTemplate.postForObject(UtilClass.baseURL+request, params, JobSearchModel[].class);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以在添加以下行

时修复错误
adpater.notifyDataSetChanged();

当列表更改时,您必须调用上述函数。

注意:确保在UI线程中调用notifyDataSetChanged()

答案 1 :(得分:0)

例外是:

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification.

因此您需要通知listview,适配器数据已更改。

致电

listView.invalidateViews();
在你打电话之后

adapter.notifyDataSetChanged();

希望它有所帮助。