onClickListener in onBindViewHolder - android

Is it wrong to do that or will it work just fine? I'm trying to make every relative layout clickable and when you click on it it takes you to a new page where the mGameTitle and mReleaseDate are passed to this new activity.
I'm doubting if it will work 'cause I'm getting an error with View.OnClickListener; must either be declared abstract or implement abstract method 'onClick(View)' in 'OnClickListener
public void onBindViewHolder(final ViewHolderUpcoming viewHolderUpcoming, int i) {
Game currentGame = listOfGames.get(i);
viewHolderUpcoming.mReleaseDate.setText(currentGame.getReleaseDate());
viewHolderUpcoming.mGameTitle.setText(currentGame.getTitle());
viewHolderUpcoming.mLayout.setOnClickListener(new View.OnClickListener() {
//Intent intent = new Intent(this, GamePage.class);
});
}

As the error states, you need to implement onClick(View).
viewHolderUpcoming.mLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
//TODO: Implementation
}
});

Related

Using setOnClickListener on RecyclerView Fragment to move to another activity

I want to (1) pass the position variable and (2) open a new activity when an item is clicked.
I found that I need to create Activity variable in order to use Intent, and I also found that I can use putExtra to pass the position variable.
Although the below code works for regular_activity.java, it does not work for fragment.java. It completely shuts down the app when I open this fragment.
#Override
public void onClick(View view) {
Intent int_detail = new Intent(activity, bottom_nav_search_details.class);
int_detail.putExtra("position", position);
activity.startActivity(int_detail); }
Just in case anyone finds useful, here is longer lines of codes. When I remove the above part (Intent part), everything works--Log successfully shows different positions when I click different items.
private Activity activity;
#Override
public void onBindViewHolder(#NonNull ExampleViewHolder holder, final int position) {
final ExampleItem currentItem = exampleList.get(position);
holder.imageView.setImageResource(currentItem.getImageResource());
holder.textView1.setText(currentItem.getText1());
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i(TAG, "Clicked " + position);
Intent int_detail = new Intent(activity, bottom_nav_search_details.class);
int_detail.putExtra("position", position);
activity.startActivity(int_detail);
}
});
}

how to open another activity using holder in android studio

hello am trying to open or launch detail activity using holder button to view detail description. how to do it in android studio
here is my code so far:
holder.cmdStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent specPage= new Intent(this, SpecActivity.class);
startActiviti(specPage);
}
});
this code show red and can not function. how to code this right to open the activity in android studio
Like that:-
holder.cmdStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent specPage= new Intent(v.getContext(), SpecActivity.class);
v.getContext().startActivity(specPage);
}
});
packageContext required by the intent might not be right, in this case "this". Also, you should not open an activity directly from the holder, instead use an router-listener approach.
Use Context passed inside onCreateViewHolder(). You can't use "this" as a Context inside onBindViewHolder()
#NonNull
#Override
public RecyclerAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
mView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.recycler_view, viewGroup, false);
/* this one declared inside RecyclerAdapter*/
this.context = mView.getContext();
MyViewHolder vh = new MyViewHolder(mView);
return vh;
}
You can only use a context inside onBindViewHolder
holder.cmdStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent specPage= new Intent(context, SpecActivity.class);
context.startActiviti(specPage);
}
});
You are passing wrong object to Intent constructor, when you use 'this' inside onClickListener, you are passing onClickListener interface in place of context. You simply need to pass view context in place of 'this'.
Below code will work.
holder.cmdStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent specPage= new Intent(v.getContext(), SpecActivity.class);
v.getContext().startActivitiy(specPage);
}
});

OnClick Listener to apply to the whole card view instead of just the textView

I am trying to implement the onclick on the cardview in a recycler view. Currently I can only set the textview in a cardview to be clickable, like my code shows:
#Override
public void onBindViewHolder(final ShopViewHolder shopViewHolder, final int i) {
shopViewHolder.shopNameTV.setText(shops.get(i).name);
shopViewHolder.categoryTV.setText(shops.get(i).category);
shopViewHolder.phoneNumberTV.setText(shops.get(i).phoneNumber);
shopViewHolder.ratingsTV.setText(shops.get(i).ratings);
shopViewHolder.emailTV.setText(shops.get(i).email);
final Shop shop = shops.get(i);
shopViewHolder.shopNameTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("test", "onclickk here" + i);
// Intent intent = new Intent(RVAdapter.this,CustomerGetQueueActivity.class);
}
}) ;
}
Right now I can only set the shopNameTV to be clickable but I want the whole card to be clickable. When I tried to use shopViewHolder.setOnClickListener it shows me an error saying "cannot resolve method setOnClickListener(anonymous.android.view.View.OnClickListener).
How can I set the whole card to be clickable instead of just the textview inside the card ?
What you are doing is, applying the onClickListeners only to specific fields. That's why it is not applicable to whole view. You are almost there, all you need to do is.
shopViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("test", "onclickk here" + i);
//Intent intent = new Intent(RVAdapter.this,CustomerGetQueueActivity.class);
}
}) ;
public final View itemView
It is the parent view of your RecyclerView.ViewHolder.
EDIT
To resolve new problem, mentioned in comments:-
You will need to pass Application or component context as a first parameter to the Intent Constructor when you are creating an Intent for a specific component of your application.
Step 1 : Pass context to the constructor of Adapter
public class RVAdapter extend BaseAdapter{
Context mContext;
public RVAdapter(Context context, ... ) //Other parameters
{
mContext = context;
//Other code
}
Step 2 : Now use this context to start new Activity
Intent intent = new Intent(mContext, CustomerGetQueueActivity.class);
Save the view that is passed to your ShopViewHolder as a member variable name it for example cardView.
then use below code inside your onBindViewHolder:
shopViewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}) ;

How to get this from an anonymous method of abstract parent activity

I have an abstract Activity called Animal and two concrete descendant Activities Cat and Dog.
Cat & Dog are to present the same UI, consisting of a single button and so there is a single layout, activity_animal.xml that Animal sets as it's content view in OnCreate.
I want to set the button's OnClickListener in the abstract Animal class by means of an anonymous implementation of OnClickListener
private void setClickHandlers() {
((Button) findViewById(R.id.btn))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//how to get a reference to this?
}
});
}
and in onClick I want to make a new Intent. To make the Intent I need a reference to this.
Normally, in anonymous method code like this, I could use syntax such as
EnclosingClass.this
but here, I don't know what the enclosing class will be. At run time, it could be either a Cat or a Dog.
How to do this?
The only way I can think of is to provide an abstract getThis() in Animal which is overridden in each concrete descendant.
You should be able to say Animal.this as your context.
private void setClickHandlers() {
View view = findViewById(R.id.btn);
view.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Animal.this, MyDestinationActivity.class);
...
}
});
}
Animal.this will be either a Cat or Dog but since you only need Context the distinction is irrelevant.
public void onClick(View arg0) {
Activity host = (Activity) arg0.getContext();
}
I'd do it like:
private void setClickHandlers() {
final Animal thiz = this;
((Button) findViewById(R.id.btn))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent( thiz, AAAA.class );
}
});
}
You can determine the runtime type by using the instanceof operator.
if (Animal.this instanceof Dog) {
// dog related
}
else {
// cat related
}

Click listener method with passing argument in Android

I'm new to android and Java. I want to make an onClick method which carries an int argument, so this is my attempt:
public void randomClick(final int randomIndex)
{
private OnClickListener top_listener = new OnClickListener() {
public void onClick(View v) {
Intent top = new Intent(Main.this, ProjectDetail.class);
top.putExtra("spendino.de.ProjectDetail.position", randomIndex);
startActivity(top);
}
};
}
but it still contains error, can anybody fix that for me?
Later I want set the method to an ImageView, it will look more or less like this image1.randomClick(randomIndex1);.
Currently in your implementation the OnClickListener is not bounded to any view, so it won't get fired.
You should create your own (might be inner but not anonymous) class implementing the OnClickListener interface:
public class RandomClickListener implements View.OnClickListener
{
private final int randomIndex;
public RandomClickListener(final int randomIndex)
{
this.randomIndex = randomIndex;
}
#Override
public void onClick(View v)
{
Intent top = new Intent(Main.this, ProjectDetail.class);
top.putExtra("spendino.de.ProjectDetail.position", randomIndex);
startActivity(top);
}
}
[...]
image1.setOnClickListener(new RandomClickListener(randomIndex));
This way when you click on the image1 it will start the ProjectDetail activity with the randomIndex set above.
If you'd like to explicitly start the ProjectDetails activity (without any user interactions such as a click), you don't need an OnClickListener at all.

Categories

Resources