删除android中的项目后,列表不刷新

时间:2016-03-04 06:43:20

标签: android listview pull-to-refresh notifydatasetchanged

您好我是android的新用户并正在进行Custom ListView的演示,我制作了adapterListItem自定义并绑定到Listview },所有工作都很完美,但我想删除项目,当我从list

删除它

适配器

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(true);
        clear();
        if (data != null) {
            addAll(data);
            notifyDataSetChanged();
        }
        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);
        notifyDataSetChanged();
        return view;
    }
}

片段

@Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        total = 0;
        mPullToRefreshLayout = (PullToRefreshLayout) getActivity().findViewById(R.id.ptr_layout);
        ActionBarPullToRefresh.from(getActivity()).allChildrenArePullable().listener(this).setup(mPullToRefreshLayout);

        mProgressBarLoading = (ProgressBar) getActivity().findViewById(R.id.progressBarLoading);
        mTextViewNoItems = (TextView) getActivity().findViewById(R.id.textViewNoItems);

        mTimelineAdapter = new TimelineAdapter(getActivity());

        mListViewTimeline = (ListView) getActivity().findViewById(R.id.listViewTimeline);
        mListViewTimeline.setEmptyView(mProgressBarLoading);

        mListViewTimeline.setAdapter(mTimelineAdapter);
        mTimelineAdapter.notifyDataSetChanged();

        mListViewTimeline.setOnScrollListener(this);

        //  mListViewTimeline.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        mListViewTimeline.setVerticalScrollBarEnabled(false);

        Activity activity = getActivity();

        if(activity != null) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    getLoaderManager().initLoader(0, null, TimelineFragment.this);
                    //Do something after 100ms
                }
            }, 1000);


            mTextViewNoItems.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {
                    getLoaderManager().destroyLoader(0);

                    getLoaderManager().initLoader(0, null, TimelineFragment.this);
                    Log.d("CLICK", "REFRESH");

                }
            });
        }

    }

    @Override
    public Loader<List<Post>> onCreateLoader(int id, Bundle args) {

        mTimelineLoader = new TimelineLoader(getActivity(), mListViewTimeline);
        setHasOptionsMenu(true);
        return mTimelineLoader;
    }

    @Override
    public void onLoadFinished(Loader<List<Post>> arg0, List<Post> data) {


        mTimelineAdapter.setData(data);

        mTimelineAdapter.notifyDataSetChanged();


        mPullToRefreshLayout.setRefreshComplete();
        mTextViewNoItems.setVisibility(View.VISIBLE);
        mProgressBarLoading.setVisibility(View.INVISIBLE);
        mListViewTimeline.setEmptyView(mTextViewNoItems);

        if (data.isEmpty()) {
            getLoaderManager().destroyLoader(0);

            getLoaderManager().initLoader(0, null, TimelineFragment.this);
        }

    }


    @Override
    public void onLoaderReset(Loader<List<Post>> arg0) {

        mTimelineAdapter.setData(null);
        setHasOptionsMenu(true);
    }

列表项

if (which == 2) {

                    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;
                                        final AsyncTask<Void, Void, Boolean> deleted = 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();
                                                tfragment = new TimelineFragment();
                                                tfragment.mTimelineAdapter.notifyDataSetChanged();

                                            }
                                        }.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
                    }

2 个答案:

答案 0 :(得分:0)

实现onResume()方法并在onResume()方法中加载适配器并通知数据集已更改

答案 1 :(得分:0)

首先仔细使用adapter.notifiDataSetChanged()。当您从列表中添加或删除行时,应该使用它。

要从列表中删除项目,请在onItemclicklistener上设置ListView

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
        });

从中您将获得要删除的已点击项目的位置。

现在在适配器中定义一个方法

   public void deleteRowFromUi(int position) {
    dataList.remove(position);
    notifyDataSetChanged();
}

使用活动

中的适配器实例从活动中调用此方法