Reputation: 171
I'm developing an Android Music Streaming application. I have Certain card views and each card view has some pop up menus (as shown in image) . I want to get the id of the card that is clicked. The albumadapter class extends recyclerview and i can't set the id of the card that is clicked. it is showing error that is red lines.
The code i have set which is having error is inside the lines..
AlbumsAdapter.java..
public class AlbumsAdapter extends RecyclerView.Adapter<AlbumsAdapter.MyViewHolder> {
private Context mContext;
private List<Album> albumList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, count;
public ImageView thumbnail, overflow;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
count = (TextView) view.findViewById(R.id.count);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
overflow = (ImageView) view.findViewById(R.id.overflow);
}
}
public AlbumsAdapter(Context mContext, List<Album> albumList) {
this.mContext = mContext;
this.albumList = albumList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.album_card, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Album album = albumList.get(position);
holder.title.setText(album.getName());
holder.count.setText(album.getNumOfSongs() + " songs");
// loading album cover using Glide library
Glide.with(mContext).load(album.getThumbnail()).into(holder.thumbnail);
holder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(holder.overflow);
}
});
}
/**
* Showing popup menu when tapping on 3 dots
*/
private void showPopupMenu(View view) {
// inflate menu
PopupMenu popup = new PopupMenu(mContext, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_album, popup.getMenu());
popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
popup.show();
}
/**
* Click listener for popup menu items
*/
class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {
public MyMenuItemClickListener() {
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_add_favourite:
Toast.makeText(mContext, "Add to favourite", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_play_next:
Toast.makeText(mContext, "Play next", Toast.LENGTH_SHORT).show();
return true;
//------------------------------------------------------------------------------------------------
case R.id.action_save_file:
ViewGroup parent = (ViewGroup) view;
TextView as = (TextView) parent.findViewById(R.id.title);
String qq = as.getText().toString();
Toast.makeText(MainActivity.this, qq, Toast.LENGTH_SHORT).show();
if (qq.equals("Save File")){
Intent i=new Intent(AlbumAdapter.this,Youtube_player.class);
startActivity(i);
}
return true;
//--------------------------------------------------------------------------------------------
default:
}
return false;
}
I need this code to be used in the class..
ViewGroup parent = (ViewGroup) view;
TextView as = (TextView) parent.findViewById(R.id.title);
String qq = as.getText().toString();
Toast.makeText(MainActivity.this, qq, Toast.LENGTH_SHORT).show();
if (qq.equals("Save File")){
Intent i=new Intent(AlbumAdapter.this,Youtube_player.class);
startActivity(i);
}
Upvotes: 0
Views: 2435
Reputation: 8149
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popupMenu = new PopupMenu(context, view);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.one:
Toast.makeText(getApplicationContext(), "1",
Toast.LENGTH_SHORT).show();
return true;
case R.id.two:
Toast.makeText(getApplicationContext(), "2",
Toast.LENGTH_SHORT).show();
return true;
case R.id.three:
Toast.makeText(getApplicationContext(), "3",
Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
popupMenu.inflate(R.menu.main);
popupMenu.show();
}
});
Upvotes: 1