Inside Onclicklistener I Cannot Access a Lot of Things - How to Approach

Inside OnClickListener I cannot access a lot of things - how to approach?

Replace this in your code with MyActivity.this where MyActivity is the class name of your Activity subclass.

Explanation: You are creating an anonymous inner class when you use this part of your code:
new OnClickListener() {
Anonymous inner classes have a reference to the instance of the class they are created in. It looks like you are creating it inside an Activity subclass because findViewById is an Activity method. Activity's are a Context, so all you need to do is use the reference to it that you have automatically.

Accessing program data from within onClick() inner class

You are not comparing your String correctly.

if(((EditText)findViewById(R.id.drv_in)).getText().toString() == "") {

is checking if the objects are equal, whereas you want to check if the values are equal. You should be using:

if(((EditText)findViewById(R.id.drv_in)).getText().toString().equals("")) {

Personally, I'd assign the value returned by getText to a String variable, rather than calling getText multiple times:

String myEditTextValue = (EditText)findViewById(R.id.drv_in)).getText().toString();
...
if ("".equals(myEditTextValue)) {

OnClickListener is not working inside my adapter class

adapterPosition in the ViewHolder's init block is returning -1. So we just need to call adapterPosition in the listener which will solve the issue, since it would retrieve the position when the view holder is created and attached to the recyclerview. Setting any kind of listeners in the init block is a good approach since the onCreateViewHolder called only once to create a view holder but the onBindViewHolder called multiple times for the same view holder, so setting listeners in the onBindViewHolder would be redundant.

   init {

thumbsUp.setOnClickListener {
val tweetId=snapshots.getSnapshot(adapterPosition).get("tweetId")
clickInterface.clickLike(tweetId.toString())
}

}

Cannot resolve method 'makeText(anonymous android.view.View.OnclickListener, java.lang.String, int)'

Just change

Toast.makeText(this, "text", Toast.LENGTH_SHORT).show();

To

Toast.makeText(ActivityName.this, "text", Toast.LENGTH_SHORT).show();

Where do I define my OnClickListener to close a custom dialog view in Android?

Your activity's content view (as set at the beginning of onCreate) is layout "main", and from that activity you're trying to access a view (dismiss_btn) in another layout entirely. Try moving this code:

  Button dismissMeBtn = (Button)findViewById(R.id.dismiss_btn); //This is the "Dismiss me" button defined on dialog layout
dismissMeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/* Changed to reflect access from listener within MyDialog */
MyDialog.this.dismiss();
}
});

Into your dialog - edit as needed.

OnClick not working in Fragment with radiobuttons

change

final Button radioButton = (Button) view.findViewById(R.id.radioButton);
final Button button = (Button) view.findViewById(R.id.button);

change

radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){

if (radioButton.isChecked()) {
button.setText("asd");
}
}

Move the above code to onCreateView() method

Best way to implement View.OnClickListener in Android

First, there is no best practice defined by Android regarding registering click listeners. It totally depends on your use case.

Implementing the View.OnClickListener interface to Activity is the way to go. As Android strongly recommends interface implementation over and over again whether it is an Activity or Fragment.

Now as you described :

public class ActivityMain extends Activity implements View.OnClickListener
{
private class ClickListener implements View.OnClickListener
{
@Override
public void onClick(View view)
{
switch (view.getId())
{
//handle multiple view click events
}
}
}
}

This is your approach. Now it is your way of implementation and there is nothing wrong with this if you are not concerned with memory overhead. But what's the benefit of creating the inner class and implementing the View.OnClickListener if you can simply implement that in the main class which can also lead to the code clarity and simplicity that you need.

So it just a discussion rather getting the best possible solution of implementing the View.OnClickListener because if you go with the practical point of everyone, you will go for a solution which is simple and memory efficient.

So I would prefer the conventional way. It keeps things simple and efficient. Check the code below:

@Override
public void onClick(View view)
{
switch (view.getId())
{
//handle multiple view click events
}
}

P.S : Your approach will definitely increase lines of code :P ;)



Related Topics



Leave a reply



Submit