Not getting any errors im just stumped on this friggin if statement. I want the if statement to basically say if a check box is not checked and 3 EditTexts are empty then print a toast. Otherwise dont print the toast and continue to the next activity. First I turn the value of the checkbox into either t/f for true/false then execute my if statements as the following.
CheckBox noPtD1 = (CheckBox) findViewById(R.id.noPTD1);
String noPt_D1 = "f";
if (noPtD1.isChecked()) {
noPt_D1 = "t";
}
if (noPt_D1.equals("f") && day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals(""))
{
Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS DAY
checkbox!", Toast.LENGTH_LONG).show();
}
if(noPt_D1.equals("t") || !day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals(""))
{
//PASS VARIABLES WITH INTENT
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);
//PASS VARIABLES FOR DAY 1
intent.putExtra("noPt_D1", noPt_D1);
intent.putExtra("day1_inst", day1_inst);
intent.putExtra("day1_uniform", day1_uniform);
intent.putExtra("day1_location", day1_location);
intent.putExtra("d1hours", d1hours);
intent.putExtra("d1min", d1min);
intent.putExtra("d1x1", d1x1);
intent.putExtra("d1x2", d1x2);
intent.putExtra("d1x3", d1x3);
intent.putExtra("d1x4", d1x4);
startActivity(intent);
}
This is working except for then I check the checkbox and and start the next activity with a button the next activity pops up like it is supposed to but the toast still appears. What did I mess up?
I got it I posted my answer below. But to rephrase exactly what my intent was:
If the user DOES NOT check the checkbox and leaves all 3 inputtexts blank show the toast and dont continue to next activity.
If the user DOES check the checkbox then DONT show the toast and continue to next activity.
Use brackets inside if statement
if (noPt_D1.equals("f") && (day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals("")))
{
}
if(noPt_D1.equals("t") || (!day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals("")))
{
}
You are making minor mistake in condition PLUS you need to use if - else block instead of 2 if blocks
//surround all && conditions inside separate braces
if (noPt_D1.equals("f") && (day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals("")))
{
//show toast
}
else if(noPt_D1.equals("t") || (!day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals("")))
{
//start activity
}
Hope this helps.
also try this. and its better to avoid including special characters for ur class name
if ("f".equals(noPt_D1) && "".equals(day1_inst) || "".equals(day1_uniform) ||
"".equals(day1_location))
{
Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS DAY
checkbox!", Toast.LENGTH_LONG).show();
}
if("t".equals(noPt_D1) || !"".equals(day1_inst) && !"".equals(day1_uniform) &&
!"".equals(day1_location))
{
//PASS VARIABLES WITH INTENT
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);
//PASS VARIABLES FOR DAY 1
intent.putExtra("noPt_D1", noPt_D1);
intent.putExtra("day1_inst", day1_inst);
intent.putExtra("day1_uniform", day1_uniform);
intent.putExtra("day1_location", day1_location);
intent.putExtra("d1hours", d1hours);
intent.putExtra("d1min", d1min);
intent.putExtra("d1x1", d1x1);
intent.putExtra("d1x2", d1x2);
intent.putExtra("d1x3", d1x3);
intent.putExtra("d1x4", d1x4);
startActivity(intent);
}
Try to check EditText values via length() of String value. Secondly put && operator in if statement because you said in your question: if a check box is not checked and 3 EditTexts are empty then print a toast. Otherwise dont print the toast and continue to the next activity.
if (noPt_D1.equals("f") && day1_inst.trim().length() == 0 && day1_uniform.trim().length() == 0 &&
day1_location.trim().length() == 0)
{
Toast.makeText(getApplicationContext(), "Please Enter All Data", Toast.LENGTH_LONG).show();
}
else
{
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);
intent.putExtra("noPt_D1", noPt_D1);
intent.putExtra("day1_inst", day1_inst);
intent.putExtra("day1_uniform", day1_uniform);
intent.putExtra("day1_location", day1_location);
intent.putExtra("d1hours", d1hours);
intent.putExtra("d1min", d1min);
intent.putExtra("d1x1", d1x1);
intent.putExtra("d1x2", d1x2);
intent.putExtra("d1x3", d1x3);
intent.putExtra("d1x4", d1x4);
startActivity(intent);
}
EDIT:
Make sure you should declare your Strings which I am assuming are:
String day1_inst = your_editText_inst.getText().toString();
String day1_uniform = your_editText_uniform.getText().toString();
String day1_location = your_editText_location.getText().toString();
String noPt_D1 = "f";
if(your_check_box.isChecked())
{
noPt_D1 = "t";
}
else
{
noPt_D1 = "f";
}
Try using an OnCheckedChangeListener Listener, like whats shown below :
CheckBox noPtD1 = (CheckBox) findViewById(R.id.noPTD1);
String noPt_D1 = "f";
noPtD1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
noPt_D1 = "t";
} else {
noPt_D1 = "f";
}
}
});
Replace getApplicationContext() with this if you are in activity. getActivity() if you're in fragment.
Just figured it out thanks to M S Gadag =)
Basically I took the if statements and put the startActivity if statement on top. Then I added a variable to show that that statement had already been executed. Then I used that variable to start another if statement to decide whether or not to show the toast.
int showToast = 0;
if("t".equals(noPt_D1) || !"".equals(day1_inst) && !"".equals(day1_uniform) &&
!"".equals(day1_location))
{
//PASS VARIABLES WITH INTENT
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);
//PASS VARIABLES FOR DAY 1
intent.putExtra("noPt_D1", noPt_D1);
intent.putExtra("day1_inst", day1_inst);
intent.putExtra("day1_uniform", day1_uniform);
intent.putExtra("day1_location", day1_location);
intent.putExtra("d1hours", d1hours);
intent.putExtra("d1min", d1min);
intent.putExtra("d1x1", d1x1);
intent.putExtra("d1x2", d1x2);
intent.putExtra("d1x3", d1x3);
intent.putExtra("d1x4", d1x4);
startActivity(intent);
showToast = 1;
}
if ("f".equals(noPt_D1) && "".equals(day1_inst) || "".equals(day1_uniform) ||
"".equals(day1_location))
{
if (showToast !=1)
{
Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS
DAY checkbox!", Toast.LENGTH_LONG).show();
}
}
Related
I'm using while loop and inside am using two if statements.
while(cr.moveToNext()){
if(cr.getCount() > 0){
if((cr.getString(5).equals(username)) && cr.getString(6).equals(password)
&& cr.getString(14).equals("success")) {
String un = cr.getString(2);
String uc = cr.getString(1);
String rc = cr.getString(4);
......................
Toast.makeText(LoginActivity.this, "Successfully Logged In", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), Dashboard.class);
startActivity(intent);
} else{
Toast.makeText(LoginActivity.this, "Invalid Login", Toast.LENGTH_SHORT).show();
}
}
}
In this scenario, if I passed wrong value "Invalid Login" is executing many times [as it is inside while loop] and If I pass correct values both successfully login and Invalid login message is showing. How to make if condition to be properly work in this type of scenario...
Assuming you want to check that everything is in order before starting the activities you can do the loop twice, like below.
I also used do while, so as not to skip the first result.
private void handleCursor(Cursor cr){
boolean everythingGood = true;
do{
if (cr.getCount() > 14) { // <- if you are using cr.getString(14)
//to get "Success" string, then you should
//check for that too
if (!checkSuccess(cr) )
everythingGood = false; }
}else {
//not enough columns received.. assumed login fail.
//do your failure workflow
everythingGood = false;
}
} while(cr.moveToNext() && everythingGood);
if(everythingGood){
//second loop (no need to check any more, just spawn the activities)
cr.moveToFirst();
do{
createActivities(cr);
}while(cr.moveToNext();
}else {
handleFailure();
}
}
I moved your check condition out to another function to make it easier to see the while loop
private boolean checkSuccess(Cursor cr){
return cr.getString(5).equals(username)) && cr.getString(6).equals(password)
&& cr.getString(14).equals("success");
}
you can use break; or you can use a boolean flag variable , at first keep it true, then enter the conditions if it is true and once entered make the flag variable false in side that condition.
Boolean isValid;
isValid = true;
while(cr.moveToNext())
{
if (cr.getCount() > 0)
{
if ((cr.getString(5).equals(username))
&& cr.getString(6).equals(password) && cr.getString(14).equals("success"))
{
if (isValid)
{
isValid=false;
String un = cr.getString(2);
}
}
else { .......... }
}
}
hell... I in my code prevent duplicate entries but when update any field in this row tell my this name exist Although I'm in same row
as example have column firstName value = Mohamed ,column lastName value = taha and column Age value 25 when update Age tell my this firstName exist.
How to Solution this problem ?
This is my method:
public void saveStore(){
String namePermission =ETNamePermission.getText().toString().trim();
String notes = ETNotesPermission.getText().toString().trim();
boolean isExist = dbHelper.isExistNamePErmission(namePermission);
if ( intent == null && TextUtils.isEmpty(namePermission)|| TextUtils.isEmpty(namePermission) ){
ETNamePermission.requestFocus();
ETNamePermission.setError(getString(R.string.error_empty_text));
return;
}
if (intent == null) {
if (isExist ==true){
ETNamePermission.requestFocus();
ETNamePermission.setError(getString(R.string.error_exist_permission));
return;
}
ItemsStore itemSavePErmission = new ItemsStore();
itemSavePErmission.setNamePermission(namePermission);
itemSavePErmission.setNotes(notes);
if (itemSavePErmission == null) {
Toast.makeText(getContext(), getString(R.string.error_save_permission), Toast.LENGTH_LONG).show();
}else {
dbHelper.addPermission(itemSavePErmission);
Toast.makeText(getContext(), getString(R.string.save_permission), Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}else {
ItemsStore itemUpdatePermision = new ItemsStore();
itemUpdatePermision.setId(intent.getInt(ID_PERMISSION));
itemUpdatePermision.setNamePermission(namePermission);
itemUpdatePermision.setNotes(notes);
boolean isExistForUpdated = dbHelper.isNamePermissioneUsedDailyMovements(intent.getInt(ID_PERMISSION));
if (isExistForUpdated == true){
Toast.makeText(getContext(), getString(R.string.this_permission_not_updated), Toast.LENGTH_SHORT).show();
return;
}
if (itemUpdatePermision != null){
dbHelper.updatePermission(itemUpdatePermision);
Toast.makeText(getContext(), getString(R.string.update_permission), Toast.LENGTH_LONG).show();
dialog.dismiss();
}else {
Toast.makeText(getContext(), getString(R.string.error_update_permission), Toast.LENGTH_LONG).show();
}
} }
Use Sqlite replace() method
db.replace(TABLE_NAME, null, contentValues);
But your table should have a unique or primary key. And replace method will check if a row exist with the given primary key. If exist the it will replace with updated value, if not then it will insert a new row with the given values.
Check this tutorial to learn about replace() method.
I'm a noob who's working on his first app. Imagine you're busy and not able to pick up some calls during the day. The app shows you a log of the calls you missed, and you can start calling them with a single button click. Not all at once - you start with the first missed number, automatically come back to the app when the call is finished, automatically dial the second number, and so on until the list is empty or you're done calling. This is my what my app looks like right now:
https://imgur.com/tke7SDx
I log missed calls and display them, and I have a "start calling" button that's supposed to start the loop. I'm not sure how to make it so that the onClick starts calling missed call no1, then missed call no2 etc and I haven't found much about it though my Google game isn't very strong yet. This is how I get the call details:
public String getCallDetails() {
StringBuffer sb = new StringBuffer();
// if
// (ActivityCompat.checkSelfPermission(getApplicationContext(),
// Manifest.permission.READ_CALL_LOG) !=
// PackageManager.PERMISSION_GRANTED) {
//
// return ;
// }
Cursor managedCursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("\n");
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
// Getting the current date and time using the date class
Date d = new Date();
if (dir == "MISSED") {
sb.append("\n Phone Number: " + phNumber + " \n Call Date: " + callDayTime + "\n");
sb.append(" ---------------------------------------------------\n \n");
}
}
managedCursor.close();
return sb.toString();
}
And this is my button onClick:
callBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
// String phone = ????
// Using the ACTION.CALL intent, you're going straight to the first
// call
// Intent callIntent = new
// Intent(Intent.ACTION_CALL, Uri.fromParts("tel",
// phone, null));
// Check for permission, write yes/no etc. here
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},
UNIQUE_REQUEST_CODE);
} else {
Toast.makeText(MainActivity.this, "Permission granted! Thank you!", Toast.LENGTH_SHORT).show();
}
startActivity(Intent.createChooser(callIntent, "callTitle"));
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Oh no, your call has failed!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
I'm also trying to filter it so that only missed calls from the past two days are showing, but that's something for later. Just wondering what's a good way to call-loop through the missed calls right now.
Any pointers are welcome!!
Thank you!
You can use Phone State Listener to listen call states. Whenever call states returns to STATE_IDLE you can go for next calls.
Dont forget to stop listining state when you are done.
i'm just a beginner in developing android apps.
i have this code in my .java file . All i wanted is that if either the EditText fields 1 or 2 is empty, then message pops using Toast, else the result.setText() will appear . However, when i'm trying to emulate this, and one or two of the EditText fields are empty the program crashes and stops. PLease help! Thank you !
EditText height = (EditText)findViewById(R.id.editText1);
EditText weight = (EditText)findViewById(R.id.editText2);
EditText result = (EditText)findViewById(R.id.editText3);
if(height.getText().toString()== "" || weight.getText().toString()==""){
Toast.makeText(this, "Error! Height or Weight is empty! ",Toast.LENGTH_SHORT).show();
}else{
result.setText("Accepted.");
}
EditText heightEditText = (EditText)findViewById(R.id.editText1);
height = heightEditText.getText().toString();
if (height.matches("") || TextUtils.isEmpty(height)) {
Toast.makeText(this, "Error! Height or Weight is empty!", Toast.LENGTH_SHORT).show();
return;
}
Try this.
if (height.getText().toString().trim().length() == 0)
{
Toast.makeText(MainActivity.this, "Please enter height", Toast.LENGTH_LONG).show();
}
else if (weight.getText().toString().trim().length() == 0) {
Toast.makeText(MainActivity.this, "Please enter height", Toast.LENGTH_LONG).show();
}
else {
result.setText("Accepted.");
}
I have three activities A,B and C
In my B activity I have one imageview, when I get a String from activity A to B, the imageview should be visible.
When I get a String from activity C to B then the imageview should not be visible.
//From activity A
Intent iin= getIntent();
Bundle b = iin.getExtras();
//From Activity C
Intent i2=getIntent();
Bundle abcd=i2.getExtras();
if(b!=null)
{
String j =(String) b.get("arrowvisi");
Toast.makeText(getApplicationContext(), j, Toast.LENGTH_LONG).show();
if(j==b.get("arrowvisi"))
{
img_back.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Operational arrow visible", Toast.LENGTH_LONG).show();
}
else
{
if(abcd!=null)
{
String jst =(String) abcd.get("arrow_val");
if(jst==abcd.get("arrow_val"))
{
img_back.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "scan dispatch visble", Toast.LENGTH_LONG).show();
}
else
{
//img_back.setVisibility(View.GONE);
System.out.println("from scan dispatch");
Toast.makeText(getApplicationContext(), "scan dispatch not visible", Toast.LENGTH_LONG).show();
}
}
Toast.makeText(getApplicationContext(), "Operational not visible", Toast.LENGTH_LONG).show();
}
}
Replace
String j = (String) b.get("arrowvisi");
with
String j = b.getString("arrowvisi");
and
String jst = (String) abcd.get("arrow_val");
with
String jst = abcd.getString("arrow_val");
also string comparison should be like
j.equals(b.getString("arrowvisi")) // change again
and
jst.equals(abcd.getString("arrow_val")) // change again
So, the final ans should be something like
//From activity A
Intent iin = getIntent();
Bundle b = iin.getExtras();
//From Activity C
Intent i2 = getIntent();
Bundle abcd = i2.getExtras();
if(b != null){
String j = "String to check"; // Replace content inside "" with your string
Toast.makeText(getApplicationContext(), j, Toast.LENGTH_LONG).show();
if(j.equals(b.getString("arrowvisi"))){
img_back.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Operational arrow visible", Toast.LENGTH_LONG).show();
}else{
if(abcd != null){
String jst = "String to check"; // Replace with string you want to check
if(jst.equals(abcd.getString("arrow_val"))){
img_back.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "scan dispatch visble", Toast.LENGTH_LONG).show();
}else{
//img_back.setVisibility(View.GONE);
System.out.println("from scan dispatch");
Toast.makeText(getApplicationContext(), "scan dispatch not visible", Toast.LENGTH_LONG).show();
}
}
Toast.makeText(getApplicationContext(), "Operational not visible", Toast.LENGTH_LONG).show();
}
}
Also note that, doing something like
String jst = abcd.getString("arrow_val"));
if(jst.equals(abcd.getString("arrow_val"))) // this will be always true
this, will result in if statement being always true.
2 options :
1) Remove if statement because always true.
2) change String jst = "Some string to compare" (change this) and now compare jst with getString("arrow_val") using jst.equals(abcd.getString("arrow_val")) in your if loop
change this
String j = (String) b.get("arrowvisi");
with
String j = b.getString("arrowvisi");
and
String jst = (String) abcd.get("arrow_val");
with
String jst = abcd.getString("arrow_val");
also put this in bracket()
b.get("arrowvisi")
abcd.get("arrow_val")
First of all,to compare strings in java,you need to use .equals
so change
if(j==b.get("arrowvisi"))
to
if(j.equals(b.getString("arrowvisi")))
and
if(jst==abcd.get("arrow_val"))
to
if(jst.equals(abcd.getString("arrow_val")))
And second,your condition always will be true,because you are comparing two equal values always,i.e. first getting value in jst and then comparing same value.
Edit : to resolve null pointer change
String j =(String) b.get("arrowvisi");
to
String j = b.getString("arrowvisi");
and
String jst =(String) abcd.get("arrow_val");
to
String jst =abcd.getString("arrow_val");
And post logcat exception.