OnPostExecute方法不会被调用

时间:2016-05-27 08:36:20

标签: android android-asynctask android-recyclerview recycler-adapter

我的AsyncTask DoInBachground和OnPreExecute方法正在调用并正在工作,但Onpostexecute没有被调用。

public class Push_notification_RecyclerView extends Fragment {
 intialise();
            new DBNotificationAsyncTask(myDBHelper).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        initialiseListeners();
        return view;
    }

    public void intialise() {
        mLayoutManager = new LinearLayoutManager(getContext());
        adapter = new PushNotificationAdapter(getContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(adapter);
        myDBHelper = new MyDBHelper(getContext());
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setTitle("Settings");
    }

    private void initialiseListeners() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().onBackPressed();
            }
        });
    }



    private class DBNotificationAsyncTask extends AsyncTask<Void, Void, Void> {
        MyDBHelper myDBHelper;

        public DBNotificationAsyncTask(MyDBHelper myDBHelper) {
            super();
            this.myDBHelper = myDBHelper;
        }


        @Override
        protected Void doInBackground(Void... params) {

            return null;
        }

        @Override
        protected void onPostExecute(Void notelist) {
            adapter.addapplication(notificationList());
            super.onPostExecute(notelist);

        }

        @Override
        protected void onPreExecute() {
            adapter.clearApplications();
            super.onPreExecute();
        }
    }


    public ArrayList<String> notificationList(){
        notelist = new ArrayList<>();

        Cursor cursor = myDBHelper.getAllnotification();
            int c = cursor.getCount();
                if (c>0) {
                if (cursor.moveToNext()) {
                    String message = myDBHelper.getNotificationMessageColumn(cursor) + myDBHelper.getNotificationMessageColumn(cursor);
                    notelist.add(message);
//                    cursor.moveToNext();
                }
            }else{
                    notelist.add("No Data");
                }

            cursor.close();
            myDBHelper.close();

        return notelist;
    }
}

执行后台和onpre mehtod之后,我的异步直接跳转到适配器getItemcount方法,该方法返回0.因此,在recyclerview中没有显示任何项目

Adapter.class

public class PushNotificationAdapter extends RecyclerView.Adapter<PushNotificationAdapter.ViewHolder> {

    private Context context;
    private ArrayList<String> notifyList = new ArrayList<>();
    public PushNotificationAdapter(Context context) {
        super();
        this.context = context;
    }

    public void addapplication(ArrayList<String> notifyList){
        this.notifyList=notifyList;
        this.notifyItemRangeInserted(0, notifyList.size() - 1);

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.push_notify_single_item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.textView.setText(notifyList.get(position));



    }

    @Override
    public int getItemCount() {
        return notifyList.size();
    }

    public void clearApplications() {
        int size = this.notifyList.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                notifyList.remove(0);
            }

            this.notifyItemRangeRemoved(0, size);
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView textView ;
        public ViewHolder(View itemView) {
            super(itemView);
            this.textView = (TextView)itemView.findViewById(R.id.push_textview);

        }
    }
}
  

请帮助我找到不让我的OnPostExecute出现的错误   叫。提前谢谢。

0 个答案:

没有答案