开发者

about android project

I have elseif statement in which i declare flag is false and i declare

Resources r = getResources();
String refrigerant;
String[] refrigerant1 = r.getStringArray(R.array.refrigerant);

in my program want to set flag=true but my for loop is not executed and flag remains false pls give me solution.

is there any problem in array declaration?

else if (ref_flg == true && ptflg == false)
{
for (int i1 = 0; i1 < refrigerant1.length; i1++)
{
    if (refrigerant == "")
    {
        if (et1.getText().toString()== refrigerant1[i1])
        {
            flag = true;
            System.out.println("flag"+flag);

        }
    }
    else
    {
        if (refrigerant == refrigerant1[i1])
        {
            System.out.println("refrigerant"+refrigerant);
            flag = true;
            System.out.println("flag"+flag);
        }
    }
}
if (flag == true)
{
    if (refrigerant == "")
    {
        if (et1.getText().toString() != "")
        {
            refrigerant = et1.getText().toString();


        }
    }
    temp_flg = true;

    et1.setText("");
}
else
{
    System.out.println("flag"+flag);
    alertDialog.setTitle("Reset...");
    alertDialog.setMessage("Enter Valid REF");
    alertDialog.setB开发者_运维百科utton2("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // here you can add functions
            dialog.dismiss();
        }
    });
    alertDialog.setIcon(R.drawable.icon);
    alertDialog.show(); 
    ref_flg = false;
}
  }


You should never compare strings with "==" since they could be different objects. Use str.equals(otherStr) or str.equalsIgnoreCase(otherStr) instead. If you do not mind about leading and trailing spaces it is a good idea to call trim() before:

boolean equalStr(String str1, String str2)
{
   if((str1 != null && str2 == null) || (str1 == null && str2 != null))
       return false;

   if(str1 == null && str2 == null)
       return true;

   return str1.trim().equals(str2.trim());
}


Always use .equals(...) for testing equality. This applies to all objects (even strings).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜