删除项目后刷新列表视图

时间:2019-05-15 05:06:35

标签: android

我有一个ListView,它通过数组(添加和删除)向我显示一些数据

每当我通过上下文菜单从ListView删除元素时,列表都不会刷新,但是会从数组中删除该元素。如何刷新列表以显示该项目目前已被删除?

代码

public class FavouritesLisViewAdapter extends ArrayAdapter<NewsModel> {



    public FavouritesLisViewAdapter(Context context, List<NewsModel> newsModel){
        super(context, R.layout.home_fragment,newsModel);

    }

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

        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View view = layoutInflater.inflate(R.layout.favourites_custom_row,parent,false);

        final NewsModel singleNews = getItem(position);
        TextView title = (TextView) view.findViewById(R.id.textViewFav);
        ImageView image = (ImageView) view.findViewById(R.id.idFavmage);
        ImageView filledHeart = (ImageView)view.findViewById(R.id.favImage);
        final ListView favListView = (ListView)view.findViewById(R.id.favouriteLV) ;

        filledHeart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //implementation to change status back to 1(negative)
                DBHelper db=new DBHelper(getContext());
                String id=singleNews.getId();
                db.reverseUpdateFavourites(id);
                Toast.makeText(getContext(),"Item removed from favourites",Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();

            }
        });


        title.setText(singleNews.getTitle());
        Picasso.get().load(singleNews.getUrlToImage()).into(image);

        return view;
    }
}

5 个答案:

答案 0 :(得分:0)

尝试此代码。

将您的ListAdapter扩展到BaseAdapter并将列表传递给该适配器。

    private class ListAdapter extends BaseAdapter {
        final Context context;
        List<NewsModel> newsModelList = new ArrayList();
        View view;
        LayoutInflater layoutInflater;

        private ListAdapter(Context context, List<NewsModel> newsModel) {
            this.context = context;
            this.newsModelList = newsModel;
        }

        @Override
        public int getCount() {
            return newsModelList.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.favourites_custom_row,parent,false);

        final NewsModel singleNews = getItem(position);
        TextView title = (TextView) view.findViewById(R.id.textViewFav);
        ImageView image = (ImageView) view.findViewById(R.id.idFavmage);
        ImageView filledHeart = (ImageView)view.findViewById(R.id.favImage);
        final ListView favListView = (ListView)view.findViewById(R.id.favouriteLV) ;

        filledHeart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //implementation to change status back to 1(negative)
                DBHelper db=new DBHelper(getContext());
                String id=singleNews.getId();
                db.reverseUpdateFavourites(id);
              ///// ADD THIS LINE  ///////
                newsModelList.remove(Integer.valueOf(id));
                Toast.makeText(getContext(),"Item removed from favourites",Toast.LENGTH_SHORT).show();
                notifyDataSetChanged();

            }
        });


        title.setText(singleNews.getTitle());
        Picasso.get().load(singleNews.getUrlToImage()).into(image);

        return view;
    }
}

答案 1 :(得分:0)

使用以下方法通知适配器已删除单个项目。

notifyItemRemoved(position);

使用以下方法通知适配器范围已删除(序列中的多个项目已删除)。

notifyItemRangeRemoved(position, count);

答案 2 :(得分:0)

在您的适配器对象上调用notifyDataSetChanged()。或使用Recyclerview,在RecyclerView.Adapter中,可以使用不同的方法来刷新列表UI。类似于notifyItemRemoved(int位置),notifyItemRangeRemoved(int,int),notifyItemChanged(int)等。选择适合您要求的一种。

答案 3 :(得分:0)

如果要从回收者视图中删除项目,最好的做法是从ViewModel / Presenter中的列表中删除该对象,然后在适配器中更新列表以更新列表UI。

答案 4 :(得分:-1)

您从数据库中删除数据,我认为您不会更新在适配器中传递的数据列表,请尝试使用更新的数据列表重新加载适配器。