Search code examples
javaandroidonclicklistenerimplements

setResult not working in a java class implementing onClickListener


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()?


Solution

  • 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();