Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialsamuel zaffran
24,815 PointsWhy my Click is ignored ? Please Help !!
Hello, I don't understand why my click is ignored when i tapped on a item ! Please HELP ! Here my code :
public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuViewHolder> {
private Context mContext;
private MenuImages[] mMenuImages;
public MenuAdapter(Context context, MenuImages[] menuImages){
mContext = context;
mMenuImages = menuImages;
}
@Override
public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.menu_list_item, parent, false);
MenuViewHolder viewHolder = new MenuViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(MenuViewHolder holder, int position) {
holder.bindMenu(mMenuImages[position]);
}
@Override
public int getItemCount() {
return mMenuImages.length;
}
public class MenuViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
public ImageView mImageView;
public TextView mTextView;
public MenuViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.itemImageView);
mTextView = (TextView) itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(this);
}
public void bindMenu(MenuImages menuImage){
mImageView.setImageBitmap(menuImage.getImageMenu());
mTextView.setText(menuImage.getTitreImageMenu());
}
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext, Main2Activity.class);
v.getContext.startActivity(intent);
Toast.makeText(v.getContext(), getLayoutPosition(), Toast.LENGTH_LONG).show();
}
}
}
1 Answer
Seth Kroger
56,414 Points @Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext, Main2Activity.class);
v.getContext.startActivity(intent);
Toast.makeText(v.getContext(), getLayoutPosition(), Toast.LENGTH_LONG).show();
}
First you forgot the parentheses on getContext() twice, but that should have caused an error. Second is a bit more subtle, but it has to do with the fact that all resource ID's like R.id, R.string, etc. are ints. Because R.string.whatever is of type int methods in Android that take text almost always have two signatures, one that takes a String (or CharSequence) and one that takes in int in it's place. getLayoutPosition() returns an int, so the compiler assumes you want the resource ID version of makeText. If you want to make a Toast of the list position try converting the int to a string first:
Toast.makeText(v.getContext(), getLayoutPosition() + "", Toast.LENGTH_LONG).show();
samuel zaffran
24,815 Pointssamuel zaffran
24,815 PointsThank you for taking the time to answering me. About the getContext it's an error of copying pasting, and i found the error, it was about my xml file and also what you said about int and String. Thank's again though.