Sum values ​only if they exist - android

Hello everyone, I have a problem adding the values ​​of edittexts, I
created 20 fields but I want to add only the ones I enter the values,
example: field 1 = value 100 field 2 = value 50 field 3 = value 0
field 4 = value 0 result = 150 how can i perform this? I used
Double.parseDouble taking the value of edittext, tried to use the if
else method and was unsuccessful ... The goal of my app is to create
a table with items and values, and then turn it into PDF, thanks
everyone!
If I do not enter the value in any of the fields, a message appears
toast message saying String is empty.

I do not clearly get what scenario u have but it seems like you are getting input from some text fields and want to add those text fields values then u can try that
Optional<String> value= Optional.ofNullable(textFieldValue)
.filter(s -> !s.isEmpty()&& s.matches("-?\\d+(\\.\\d+)?");
if(value.isPresent()){
double d = Double.parseDouble(value.get());
//then do your addition stuff
}

Related

AutocompleteTextView text clear if entered value is not found from list

I have a dynamic arraylist that contains String values. I want to match one of its values with AutocompleteText whatever the user has typed/entered. In case the value is not matching with any of the Arraylist values I would like to clear Autocomplete Text.
Couple of Ways I have tried.
Match the typed or user selected value from autocomplete text with list directly without any loop using "contains" method. Didn't achieve the expected result.
Store the values in String[] array from list and loop through it and match it with user input/selection from Autocompletetext. Didn't achieve the result this way too.
Please provide any ideas on how to clear text from autocompletetextview if value is not found?
You did not specify any language that's why i will answer in kotlin
Override onKeyDown method and let's say you will check when enter pressed check if given keycode value equals to KeyEvent.KEYCODE_ENTER
In if block get your text and use an algorithm like below
val arr = intArrayOf(1,2,3) if (textFromTextView !in arr) it means your typed text not in your array and if this condition is true
use autoCompleteTextView.replaceText("") and i hope it will work for you.I never tried but i searched for you.

How to make each input in edit text a value in an array in android?

I am making a grading app, basically I have an edit field (number field) that you can enter your grades (in numbers) in, or multiple fields for each grade? But lets say I want to calculate the average grade , and if my edit fields are not in an array I would have to go through each value of the field by id and that is going to make the code too long. So I want an array that takes values from each edit field, so if I have 2 edit fields and I type 4 in the first and 6 in the second my array would be {4,6}. I am a beginner in android development but I have solid Java experience.
There's only so much you can do here since each EditText is its own object with its own value field. So to some extent, you're going to have to reference each EditText.
What you can do at least is track each EditText in a List as a member variable. that way you're only having to grab a reference to each edittext once in your activity. Then, when you need to reference the collection for averaging or whatever else, you can just iterate over the list calling .getText(). If you need to reference a specific edittext, either store a separate reference in another member variable, or look it up in the list by id or by a tag you set (see here for more info on tags).
Maybe init each editText in a HashMap with a list of numeric values and use the editText reference to add and get values to the list of numeric values like this:
final Map<EditText, List<Integer>> editTexts = HashMap<EditText, List<Integer>>();
If you init an EditText you could do something like this:
EditText editText = (EditText) findViewById(R.id.editText);
editTexts.put(editText , new ArrayList<>());
editText.setOnClickListener(new OnClickListener(){
public void onClick(View v){
editTexts.get((EditText) v).add(SOME_VALUE);
}
);
I havn't tested it but this could be a valid strategy.

remove/delete a value from a field with multiple values separated with semicolons

I am using the following command in Android to add a a value when needed in a a field that has multiple values separated with semicolons.
Table name is system and contain 3 fields: ID, Name, and Value.
The value field contains several values seprated with semicolons:
Wifi;Mobile Data;BlueTooth etc.
Now if I want to add a value in it the following command work perfectly, in this case I add Data; where the Name field is notification_panel_active_app_list:
UPDATE system
SET value = (SELECT value || 'Data;'
FROM system
WHERE name = 'notification_panel_active_app_list')
WHERE name = 'notification_panel_active_app_list';
Now I want to remove a value from this multiple-value field, how to do it? Let's say I want to remove 'Bluetooth;', how should I do it? I tried putting DELETE instead of SELECT but it doesn't do anything.
Your command to add a value can be simplified because the old value in the value column is available directly:
UPDATE system
SET value = value || 'Data;'
WHERE name = 'notification_panel_active_app_list';
To remove a value, you can use the replace function:
UPDATE system
SET value = replace(value, 'BlueTooth;', '')
WHERE name = 'notification_panel_active_app_list';
However, this works correctly only if this value is actually followed by a semicolon, and if there is no other entry that has this value as a suffix (such as RedAndBlueTooth;).

Android : If-statement to display "correct" when user inputs value

I want the user to input the value 4 and have a TextView called result display that it's correct
you are compare String and Editable
try "4".equals(ans.getText().toString())

Checking for input with space

I've made an offline currency convertor that gets the users input in the EditText section using a TextWatcher and returns the required ouptut from methods...and I"ve made it an a way that the user cannot insert a "null" value in the EditText section and then press the convert button by using euro.getText !==null for example.But I don't know how to proceed when the user leaves some space between the input,for instance 29 50.This will make my program to crash.My question what should I use to check for an input with space in order to avoid a program crash?Thank you.
Your program crashes with number format exception. You can do so:
try{
double value = Double.parseDouble(editText.getText().toString());
} catch(NumberFormatException ex){
Log.e(TAG, "improper number format");
//show some dialog saying what's the format that should be entered
}
You can also go with a regex:
String editTextValue = editText.getText().toString();
if(editTextValue.matches("\\d+\\.\\d+")){
double value = Double.parseDouble(editTextValue);
} else{
//show dialog saying what should be the format.
}
This is actually quite easy.
Does your app accept numbers with a comma or a dot? Either way, you can simply replace the String by the symbol of you choice by using the following:
String unspaced = edittext.getText().toString().replace(' ', '.'); // or , depending on what your app uses

Categories

Resources