0

In my project I have some buttons, with their own View.OnClickListener. What I want is to "externalize" all this onClickListener into a java calss that implements the interface, and handle all the click events, here.

But I found a problem, that I don't know how to fix it.

One of the buttons launch a setResult(RESULT_OK, startChatIntent) and finish(), but thismethods inside the class, are marked as: Cannot be resolved as method.

This is the class : (I take out the other buttons functionalities, that works OK, to make it more clear)

public class startCircuitListener implements View.OnClickListener {

    private int mChatType;
    private String mGroupName;
    private String mNickName;
    private String mMessage;
    private ArrayList<String> mGroupEmails;
    private Context context;

    public startCircuitListener(int mChatType, String mGroupName, String mNickName, String mMessage, ArrayList<String> mGroupEmails, Context context) {
        this.mChatType = mChatType;
        this.mGroupName = mGroupName;
        this.mNickName = mNickName;
        this.mMessage = mMessage;
        this.mGroupEmails = mGroupEmails;
        this.context = context;
    }

    @Override
    public void onClick(View v) {


        Intent startChatIntent = new Intent();
        startChatIntent.putExtra("chatRoom", mGroupName);
        startChatIntent.putExtra("chatServer", HelperVariables.CONFERENCE_SERVER_NAME);
        startChatIntent.putExtra("nickname", mNickName);
        startChatIntent.putExtra("Individual_Group", 0);
        startChatIntent.putExtra("MessageToSend", mMessage);
        startChatIntent.putExtra("Invitations", mGroupEmails);
        setResult(RESULT_OK, startChatIntent);
        finish();

    }

}

How can I make work setResult and finish()?

Shudy
  • 7,806
  • 19
  • 63
  • 98
  • [`setResult()`](http://developer.android.com/reference/android/app/Activity.html#setResult%28int%29) and `finish()` are declared in `Activity`. So you need to call those methods on an instance of `Activity`. If you'd show more of your code, we could give more precise advice. - Is your listener a normal, "outer" class, or is it a non-static inner class of another class, maybe even an `Activity`? – JimmyB Mar 02 '16 at 11:54
  • Yes, this listener is a external java class (basically a new File), that I want to call, in the buttons as this : `bt_startCircuit.setOnClickListener(new startCircuitListener(param1,param2, ..paramN)); – Shudy Mar 02 '16 at 12:03

1 Answers1

1

You have to create your listener instance with the current Activity instance, and store it as a member of startCircuitListener.

Then call this.myActivity.setResult(RESULT_OK, startChatIntent);. Same thing for finish();

Xvolks
  • 2,065
  • 1
  • 21
  • 32