Search code examples
javaandroidmethodsresolve

can't resolve finish methods --call from Android Studio methods


Android Studio 3.0.1

It works in QAnswerQuestion.java code

List<Integer> wrongList = UIResponse.checkAnswer(list);
if (wrongList.size() == 0)
{
  new AlertDialog.Builder(QAnswerQuestion.this).setTitle("Info")
       .setMessage("You are awesome and all answers are correct!")
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
           finish();
         }
         }).setNegativeButton("Cancel", null).show();
}

but when I try to put the above code in UIResponse.java

and call in QAnswerQuestion.java like this:

UIResponse.lastQuestionDialog(QAnswerQuestion.this,list);

and UIResponse.java code is

static void lastQuestionDialog(final Context context, List<Question> list)
{
  List<Integer> wrongList = UIResponse.checkAnswer(list);
  if (wrongList.size() == 0)
  {
    new AlertDialog.Builder(context).setTitle("Info")
          .setMessage("You are awesome and all answers are correct!")
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which)
               {
                 finish();
               }
              }).setNegativeButton("Cancel", null).show();
        }
}

It says "can't resolve finish methods "


Solution

  • The problem is you are showing dialog in other class which is UIResponse . And finish() is method of Activity.one simple solution can be .

    static void lastQuestionDialog(final Context context, List<Question> list)
     {
      List<Integer> wrongList = UIResponse.checkAnswer(list);
       if (wrongList.size() == 0)
       {
    new AlertDialog.Builder(context).setTitle("Info")
          .setMessage("You are awesome and all answers are correct!")
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which)
               {
                 ((Activity)context).finish();
               }
              }).setNegativeButton("Cancel", null).show();
        }
     }
    

    Other then this I suggest you to use an callback interface to notify Activity about the dialog actions so that you can manage them in your Activity . Read how-to-define-callbacks-in-android.