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.
A very good library to work with Eventbus is the Otto of square. Reference: http://square.github.io/otto/
– Jefferson Rodrigues dos Santos