Update Recyclerview from another Activity

Asked

Viewed 661 times

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 view, from the click of a button on another Activity.

I’ve tried using the notifyDataSetChanged();, but this only works if the update occurs within Recyclerview.

1 answer

2

The easiest method is using the Eventbus. The name itself already says... It’s an event delivery (from anywhere to anywhere).

It’s just for EventBus.getDefault.register(this); in the recyclerview constructor and then create any method (within recyclerview) that updates the list. Add the anottation @Subscribe above the method... From Activity, call EventBus.getDefault.post(new ClasseDeEvento(listagem));(evidently at a button click, for example)
This Classedeevent is any class you should create and pass in the Eventbus post.Don’t forget to set the new arraylist within this class.... To call the recyclerView method, its method must RECEIVE the Classedeevent type.

Example:
(That’s in the RV)

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

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

  • 1

    A very good library to work with Eventbus is the Otto of square. Reference: http://square.github.io/otto/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.