NotifyDatasetChanged在android中的customListView中不起作用

时间:2016-02-29 13:20:09

标签: android listview android-adapter

我为此制作了自定义ListView和适配器,我为delete功能制作了自定义相对布局类。现在,我希望在删除任何项目后更新我的ListView。我使用了notifydatasetChanged()方法,但它不起作用。请帮我。我的适配器如下

public class TimelineAdapter extends ArrayAdapter<Post> {

    private final LayoutInflater mInflater;

    public TimelineAdapter(Context context) {

        super(context, R.layout.list_item_post);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<Post> data) {
        setNotifyOnChange(false); 
        clear();
        if (data != null) {
            addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) {

        Post post = getItem(position);
        PostListItem view;
        if (convertView == null) {
            view =  (PostListItem) mInflater.inflate(R.layout.list_item_post, parent, false);
        } else {
            view = (PostListItem) convertView;
        }
        view.setPost(post);
        return view;
    }
}

CustomListItem

if (mPost.postUser.userID == sharedConnect.mCurrentUser.userID) {
    // Log.e("Remove Post", "Success");


    //Added by jigar..
    tfragment = new TimelineFragment();
    AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());

    builder1.setMessage("Are you sure?");
    builder1.setCancelable(true);

    builder1.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();

    //open = false;
                    new AsyncTask<Void, Void, Boolean>() {

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

                            Connect sharedConnect = Connect.getInstance(getContext());
                            sharedConnect.deletePost(mPost.postID);


                            return true;
                        }


                        protected void onPostExecute(Boolean result) {

                            Toast.makeText(getContext(), "Deleted",
                                    Toast.LENGTH_SHORT).show();




                        }
                    }.execute();


                }
            });

    builder1.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    //  open = false;
                }
            });

    AlertDialog alert11 = builder1.create();
    alert11.show();


    //end

0 个答案:

没有答案