0

I'm having a problem with this code

if ((equip != null) && equip.trim().equals("")){
    final Dialog dialog = new Dialog(getActivity());
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    dialog.setContentView(R.layout.dialog_layout_equip);

And my else is

final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
dialog.setContentView(R.layout.dialog_layout);

So if my string is null or empty i use this dialog dialog.setContentView(R.layout.dialog_layout); Otherwise i use this one dialog.setContentView(R.layout.dialog_layout_equip);

My logcat gives me this output from this code

String equip = reserveComp.getString(TAG_EQUIP);
                        Log.d("Equip", equip);

D/Equip﹕ Projetor, so it open the

dialog.setContentView(R.layout.dialog_layout);

but when i have this output D/Equip﹕ null it open the same

So whats wrong?

UPDATE

This is my full block code

JSONObject reserveComp = reqFeitasDetail.getJSONObject(0);
                        String equip = reserveComp.getString(TAG_EQUIP);
                        Log.d("Equip", equip);
                        if ((equip == null) || equip.trim().equals("")){
                            final Dialog dialog = new Dialog(getActivity());
                            dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
                            dialog.setContentView(R.layout.dialog_layout);
                            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                            dialog.show();
                            Button img = (Button) dialog.findViewById(R.id.close_button);
                            TextView data = (TextView) dialog.findViewById(R.id.data);
                            TextView sala = (TextView) dialog.findViewById(R.id.sala);
                            TextView hora = (TextView) dialog.findViewById(R.id.hora_inicial);
                            TextView fim = (TextView) dialog.findViewById(R.id.hora_final);
                            data.setText(reserveComp.getString(TAG_DATA));
                            sala.setText(reserveComp.getString(TAG_NOME));
                            hora.setText(reserveComp.getString(TAG_TEMPO));
                            fim.setText(reserveComp.getString(TAG_TEMPOFIM));
                            img.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                        } else {
                            final Dialog dialog = new Dialog(getActivity());
                            dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
                            dialog.setContentView(R.layout.dialog_layout_equip);
                            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                            dialog.show();
                            Button img = (Button) dialog.findViewById(R.id.close_button);
                            TextView data = (TextView) dialog.findViewById(R.id.data);
                            TextView sala = (TextView) dialog.findViewById(R.id.sala);
                            TextView equipamento = (TextView) dialog.findViewById(R.id.equip);
                            TextView hora = (TextView) dialog.findViewById(R.id.hora_inicial);
                            TextView fim = (TextView) dialog.findViewById(R.id.hora_final);
                            data.setText(reserveComp.getString(TAG_DATA));
                            sala.setText(reserveComp.getString(TAG_NOME));
                            equipamento.setText(reserveComp.getString(TAG_EQUIP));
                            hora.setText(reserveComp.getString(TAG_TEMPO));
                            fim.setText(reserveComp.getString(TAG_TEMPOFIM));
                            img.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                        }

And i had this logcat output

D/Reserva Sala completa﹕ {"tag":"reqEquipFull","success":1,"error":0,"reqEquipFull":[{"data":"2015-06-12","sala":"21","inicio":"14:05:00","fim":"15:45:00","nome":null}]}

D/Equip﹕ null

And i still have the same problem with the dialog, all (null and non null) open this dialog

dialog.setContentView(R.layout.dialog_layout_equip);
2
  • 4
    To test if equip is empty you can use : if(TextUtils.isEmpty(equip)) Commented Jun 8, 2015 at 15:47
  • not working...i have null Commented Jun 8, 2015 at 16:13

4 Answers 4

4

To check if your string is null or empty, the condition inside the if block should be

if ((equip == null) || equip.trim().equals("")){...}

You should also switch the code blocks inside your if-else statement if you want to do the following..

So if my string is null or empty i use this dialog dialog.setContentView(R.layout.dialog_layout); Otherwise i use this one dialog.setContentView(R.layout.dialog_layout_equip);

if ((equip == null) || equip.trim().equals("")){
    final Dialog dialog = new Dialog(getActivity());
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    dialog.setContentView(R.layout.dialog_layout);
}
else {
    final Dialog dialog = new Dialog(getActivity());
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    dialog.setContentView(R.layout.dialog_layout_equip);
}
Sign up to request clarification or add additional context in comments.

Comments

0

change the and condition to or condition in if statement

if ((equip != null) && equip.trim().equals(""))

to

if ((equip != null) || equip.trim().equals(""))

Comments

0

Your AND condition will never be true, you are checking NOT NULL in your if statement!!

if ((equip != null) && equip.trim().equals("")){

Should be an or and if == null

if (equip == null || equip.trim().equals("")){
    final Dialog dialog = new Dialog(getActivity());
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    dialog.setContentView(R.layout.dialog_layout_equip);
} else {
    final Dialog dialog = new Dialog(getActivity());
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    dialog.setContentView(R.layout.dialog_layout);
}

Comments

0
if (equip.equals("null") || equip.isEmpty())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.