如何在标记位置获取上下文?

时间:2014-01-06 07:10:22

标签: android

我正在尝试在我的Android应用程序中使用parse.com,如何在代码AlertDialog.Builder中的 * ** 处获取上下文(的 * ** * *** )?我尝试使用不起作用的getApplicationcontext。任何有android经验的人都会帮我这个吗?

mSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                mVibrator.vibrate(100);

                //Checking for internet connection...
                 ConnectivityManager cm =
                            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                        NetworkInfo netInfo = cm.getActiveNetworkInfo();
                        //internet connection inactive... show alert...
                        if (!(netInfo != null && netInfo.isConnectedOrConnecting())) {
                            AlertDialog.Builder adb = new AlertDialog.Builder(arg0.getContext());
                            adb.setTitle("ALERT");
                            adb.setMessage("Please turn on Internet.");
                            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int id)
                                {
                                // Action for 'Ok' Button
                                 }
                            });
                            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                            {
                                            public void onClick(DialogInterface dialog, int id)
                                                            {
                                                            // Action for 'Cancel' Button
                                                            dialog.cancel();
                                            }
                            });
                            adb.setIcon(R.drawable.ic_launcher);
                            adb.show();
                         }else{                          
                             ParseQuery<ParseObject> query = ParseQuery.getQuery("Admin");
                             query.whereEqualTo("password", mUserNameEditText.getText().toString());
                             query.whereEqualTo("userName", mPasswordEditText.getText().toString());

                             query.countInBackground(new CountCallback() {
                                  public void done(int count, ParseException e) {
                                    if (e == null) {
                                      // The count request succeeded. Log the count
                                      Log.d("test", "Sean has played " + count + " games");
                                      if(count>0){
                                            // save the login status... loggedIn/notloggedIn...
                                            SharedPreferences sharedPreferences =   PreferenceManager.getDefaultSharedPreferences(LoginScreenActivity.this);
                                            SharedPreferences.Editor editor     =   sharedPreferences.edit();
                                            editor.putBoolean("isLoggedIn", true);
                                            editor.commit();

                                            // if user name password is correct, navigate to next activity...
                                            Intent intent   =   new Intent(LoginScreenActivity.this,OptionScreenActivity.class);
                                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                            startActivityForResult(intent, 0);
                                            overridePendingTransition(R.layout.fade_in, R.layout.fade_out);
                                        }else{
                                            AlertDialog.Builder adb = new AlertDialog.Builder(*********************);
                                            adb.setTitle("ALERT");
                                            adb.setMessage("Wrong username or password.");
                                            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener()
                                            {
                                                public void onClick(DialogInterface dialog, int id)
                                                {
                                                // Action for 'Ok' Button
                                                 }
                                            });
                                            adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                                            {
                                                            public void onClick(DialogInterface dialog, int id)
                                                                            {
                                                                            // Action for 'Cancel' Button
                                                                            dialog.cancel();
                                                            }
                                            });
                                            adb.setIcon(R.drawable.ic_launcher);
                                            adb.show();
                                        }

                                    } else {
                                      // The request failed

                                    }
                                  }
                                });                                                  

1 个答案:

答案 0 :(得分:0)

试试如下:

    AlertDialog.Builder adb = new AlertDialog.Builder(getApplicationContext());

     AlertDialog.Builder adb = new AlertDialog.Builder(<YourActivity>.this);