ok, i get a little bit confused on the if-else statement
. I have 4 row in my table layout
and each row will be checked to see whether it has value or not.
Case 1
public void onClick(DialogInterface dialog, int ii)
{
if ((i != null && i.trim().length() > 0))
{
WD.insertWorkDetails(a1, W1, P1, b, c); //row 1
WD.insertWorkDetails(a2, W2, P2, d, e1); //row 2
WD.insertWorkDetails(a3, W3, P3, f, g); //row 3
WD.insertWorkDetails(a4, W4, P4, h, i); //row 4
ts.insertTimeSheet(name, weather, date, status, b, i);
WF.insertWorkForce(subContractors, noPeople, noHours);
Toast.makeText(getApplicationContext(), "4 Row has value", Toast.LENGTH_LONG).show();
}
else if (TextUtils.isEmpty(i) && (g != null && g.trim().length() > 0))
{
if ((TextUtils.isEmpty(W4)) && (TextUtils.isEmpty(P4)) && (TextUtils.isEmpty(h)) && (TextUtils.isEmpty(i)))
{
WD.insertWorkDetails(a1, W1, P1, b, c);
WD.insertWorkDetails(a2, W2, P2, d, e1);
WD.insertWorkDetails(a3, W3, P3, f, g);
ts.insertTimeSheet(name, weather, date, status, b, g);
WF.insertWorkForce(subContractors, noPeople, noHours);
Toast.makeText(getApplicationContext(), "Row 4 no value", Toast.LENGTH_LONG).show();
}
else
{
database = dbHelper.getWritableDatabase();
WD.insertWorkDetails(a1, W1, P1, b, c);
WD.insertWorkDetails(a2, W2, P2, d, e1);
WD.insertWorkDetails(a3, W3, P3, f, g);
WD.insertWorkDetails(a4, W4, P4, h, i);
ts.insertTimeSheet(name, weather, date, status, b, g);
WF.insertWorkForce(subContractors, noPeople, noHours);
Toast.makeText(getApplicationContext(), " All Row has value ",
Toast.LENGTH_LONG).show();
}
}
In case 1, g!=null
and w4
holds a value, but I get Row 4 no value
which suppose to return All Row has value
. Why?
Case 2
else if (TextUtils.isEmpty(i) && (TextUtils.isEmpty(g) && (TextUtils.isEmpty(e1)) && (c != null && c.trim().length() > 0)))
{
if ((TextUtils.isEmpty(W4)) && (TextUtils.isEmpty(P4))
&& (TextUtils.isEmpty(h)) && (TextUtils.isEmpty(i))
&& ((TextUtils.isEmpty(W3)) && (TextUtils.isEmpty(P3))
&& (TextUtils.isEmpty(f)) && (TextUtils.isEmpty(g)))
&& ((TextUtils.isEmpty(W2)) && (TextUtils.isEmpty(P2))
&& (TextUtils.isEmpty(d)) && (TextUtils.isEmpty(e1))))
{
ts.insertTimeSheet(name, weather, date, status, b, c);
WF.insertWorkForce(subContractors, noPeople, noHours);
WD.insertWorkDetails(a1, W1, P1, b, c);
Toast.makeText(getApplicationContext(), "Row 1 has value only", Toast.LENGTH_LONG).show();
}
else if ((W4 != null && W4.trim().length() > 0))
{
WD.insertWorkDetails(a1, W1, P1, b, c);
WD.insertWorkDetails(a2, W2, P2, d, e1);
WD.insertWorkDetails(a3, W3, P3, f, g);
WD.insertWorkDetails(a4, W4, P4, h, i);
ts.insertTimeSheet(name, weather, date, status, c, b);
WF.insertWorkForce(subContractors, noPeople, noHours);
Toast.makeText(getApplicationContext(), "All row have value", Toast.LENGTH_LONG).show();
}
}
In case 2, c and W4!=null
, but I get row 1 has value only
.
At least there must be to closing braces at the end of your code.
Anyway, I see this structure of if-s:
if(...condition-1...) {
...code-1...
} esle if(...condition-2...) {
if(...condition-3...){
...code-3...
} else {
...code-4...
}
}
Maybe You want "else" with code-4 to refer to "if" with condition-2, not to "if" with condition-3? Then You would change the structure in this way (additional } before else):
if(...condition-1...) {
...code-1...
} esle if(...condition-2...) {
if(...condition-3...){
...code-3...
}
} else {
...code-4...
}
I think, There is no issue with if-else statement. May be, you are getting empty string with length > 0. TextUtils.isEmpty() : Returns true if the string is null or 0-length. http://developer.android.com/reference/android/text/TextUtils.html#isEmpty(java.lang.CharSequence)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.