Refresh RecyclerView from another Activity

1

I have the following class

    public class FavoriteListAdapter extends RecyclerView.Adapter {
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.favorite_item, parent, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ListViewHolder) holder).bindView(position);
    }

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

    public void delete(int position) {
        favoriteVideos.remove(position);
        notifyDataSetChanged();
    }

    public void addItem(FavoriteVideo favoriteVideo){
        favoriteVideos.add(favoriteVideo);
        notifyDataSetChanged();
    }

    private class ListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener, View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {
        private TextView mVideoTitleTextView;
        private TextView mVideoTimeTextView;
        private Context mContext;
        String videoURL;
        String videoName;
        public ListViewHolder(View itemView) {
            super(itemView);
            mContext = itemView.getContext();
            mVideoTitleTextView = (TextView) itemView.findViewById(R.id.favoriteVideoTitleTextView);
            //mVideoTimeTextView = (TextView) itemView.findViewById(R.id.videoTimeTextView);
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
            itemView.setOnCreateContextMenuListener(this);
        }

        @Override
        public boolean onLongClick(View view) {

            return false;
        }

        public void bindView(int position) {
            mVideoTitleTextView.setText(favoriteVideos.get(position).getName());
            //mVideoTimeTextView.setText(Topic.list_time_videos[position]);
            videoName = favoriteVideos.get(position).getName();
            videoURL = favoriteVideos.get(position).getUrl();
        }

        @Override
        public void onClick(View view) {
            Intent videoIntent;
            if (getVideoOrigin(videoURL).equals("youtube")) {
                videoIntent = new Intent(mContext, YoutubePlayerActivity.class);
            } else {
                videoIntent = new Intent(mContext, VideoActivity.class);
            }
            videoIntent.putExtra("videoURL", videoURL.split(("/"))[videoURL.split(("/")).length - 1]);
            videoIntent.putExtra("videoName", videoName);

            mContext.startActivity(videoIntent);
        }

        private String getVideoOrigin(String video) {
            if (video.contains("youtube")) {
                return "youtube";
            } else {
                return "vimeo";
            }

        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
            menu.setHeaderTitle(mContext.getString(R.string.favorites));
            menu.add(0, view.getId(), 0, mContext.getString(R.string.remove_from_favorites)).setOnMenuItemClickListener(this);
        }

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setMessage(mContext.getString(R.string.confirmation))
                    .setPositiveButton(mContext.getString(R.string.yes), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            DataSource dataSource = new DataSource(mContext);
                            dataSource.deleteFavoriteVideo(videoURL);
                            delete(getAdapterPosition());
                            Toast.makeText(mContext, mContext.getString(R.string.removed_favorite_video), Toast.LENGTH_LONG).show();
                        }
                    })
                    .setNegativeButton(mContext.getString(R.string.no), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            // Create the AlertDialog object and return it
            AlertDialog dialog = builder.create();
            dialog.show();
            return true;
        }
    };
}

I need to be able to update this recycler view, from the click of a button on another activity.

I have already tried to use notifyDataSetChanged(); , but this only works if the update occurs within RecyclerView.

    
asked by anonymous 26.10.2016 / 13:48

1 answer

2

The easiest method is to use EventBus . The name itself says ... It's an event delivery (from anywhere to anywhere).

Just by EventBus.getDefault.register(this); in the recyclerview constructor and then create any method (inside still of recyclerview) that updates the list. Add the @Subscribe anottation upon method ... From activity, call EventBus.getDefault.post(new ClasseDeEvento(listagem)); (evidently at the click of a button, for example)
This event class is a class that you must create and pass in the EventBus post. Do not forget to set the new arraylist within that class ..... To call the method of recyclerView, its method MUST RECEIVE of classEvent.

Example:
(this is in RV)

@Subscribe
public void onEventAtualizarLista(ClasseDeEvento cde){
this.listagem = cde.getListagem();
this.notifyDataSetChanged();
}

Simply and magically the method with @Subscribe and that receives the same type in the parameter will be automatically called after the post ...

    
26.10.2016 / 14:23