How to make each input in edit text a value in an array in android? - 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.

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.

Set Id to dynamically created edittext

I'm dynamically creating Edittext on a button click and saving this edittext to an ArrayList of type Edittext and then getting values of all edittext in an ArrayList of type String. While creating new Edittext Iam also creating a BUTTON with cross image which delete that edittext when user click on it. and then Iam passing these edittexts to Options of CheckBox question which is also Iam creating dynamically.
Issue is when I create multiple edittext and randomly delete someone and click ok arraylist remove the last index of it .Iam also setting ids to Edittext but couldnt able to understand how to get the Id of the same Edittext whose value I want to delete and then remove him from ArrayList too.
Here I'am creating the Edittext and adding them in "moreOptList" with there ids.
EditText checkBoxMoreEdt = new EditText(this);
checkBoxMoreEdt.setId(i);
moreOptList.add(i, checkBoxMoreEdt);
Here Iam deleting the view on Button clickListener,but instead of deleting the the exact value from arrayList also its only delete the view and the last index of array from the list.For example if I have enter 4 options
a
b
c
d
and then deleted the option 2 which is b and click ok,it will show me
a
b
c
what its doing is removing the last save value from arrayList now How should I get the Id of exact edittext I want to delete and delete it from arrayList also.
final Button button = new Button(this);
button.setId(b);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chkbParentLayout.removeView(ll);
moreOptList.remove(i);
}
});
Most probably the problem is in ArrayList. When you remove element by index all elements are shifted, hence indexes do not match your views:
list.add(0, 0) //[0]
list.add(1, 1) //[0, 1]
list.remove(0) //[1]
list.remove(1) //throws IndexOutOfBoundsException
One options is to replace ArrayList with HashMap:
map = HashMap<Integer, View>()
map.put(i, view)
...
group.remove(map.remove(i))
Another useful thing is to use setTag/getTag methods of View, you can store your indexes there, so you might not need to have external indexes storage:
view.setTag(index)
index = view.getTag()

Can i refresh (getText and setText) all Edit Text values in one go ?

I am using Edit Text watcher, to do some calculations.like below,
And every Edit Text group (A,B,C) is working fine independently and relatively. But relatively worked only if i come from top to bottom. I want to refresh (getText and setText) on every Edit Text onTextChange() method invoked. i can do this using custom method .
But can i do this in one go , like all edit text values of a particular XML get refresh, so each editText onTextChange will be called in TOP-DOWN approach, Because every descendent group is dependent in some context to previous group .
This is doable, you just must have an algorithm. But yes, it is possible, and not too challenging...
As a hint, EditText.onTextChanged() methods inside of the each listener will need access its other 5 EditText siblings/neighbors. (A-C rows and 1-2 columns, respectively).
All 6 onTextChangedListener implementations for the 6 Edit Texts may need to be unique in how they handle textChanged events. You will need to pass in the objects in which they are responsible for changing in their constructors.
ie
EditText et1 = (EditText)findViewById(R.id.editText1);
EditText et2 = (EditText)findViewById(R.id.editText2);
et1.setOnTextChangedListener(new TextChangedListener(et2){
//global var
EditText nextEditText;
public TextChangedListener(EditText editText){
super();
nextEditText = editText;
}
public void onTextChanged(){
this.alert();
}
public void alert(){
//modify data here and update self if necessary.
//then alert the next boxes on text changed method.
nextEditText = editText.getListener().alert();
}
});

Android - Get data from a dynamic EditTexts by ID

I have the code below in my adapter that creates dynamically EditTexts and set IDs using the method "et_settingValue.setId(setting.getId());".
An Editbox is created in every instace of the class Setting and it also contains a variable to store its id.
This part is already working properly, but now I need to access all those created EditTexts by ID and get its data. If possible, I would like to avoid creating another array to store the EditTexts because I already have their IDs.
Is there any way to do it using the dynamic IDs I already have?
EditText et_settingValue = (EditText) view.findViewById(R.id.et_settingValue);
et_settingValue.setText(setting.getValue().trim());
et_settingValue.setId(setting.getId());
Update 1
In my activity, I am trying to do this:
EditText et = new EditText(listView.getContext());
//loop to get each object child
settingConfig.getConfigName(); //ok
settingConfig.getConfigValue(); //ok
settingConfig.getConfigId(); //ok
et = (EditText) listView.findViewById(settingConfig.getConfigId()); //not working
et.getText(); // off course it will not working
Many thanks
ListView use a RecycleBin to reuse the view created from Adapter, so ListView will only contain a few child view, and you cannot find all EditText in the ListView.
To solve your problem, you should use a Map to record the value of all EditText. Add TextWatcher to each of them, and refresh the value in the Map on text changed.

Find out the new element in EditText View

I want to execute one method wheneer i enter number in EditText view.But i did not change the focus of EditText.I am stay in the same EditText when i enter a new number automatically i eant to execute one function.Now my question is how we find that event that means enter a new number in EditText
You need to associate a TextWatcher to your EditText, and then, check in onTextChanged() if the character added was a number. You could use a regular expression for that :
if(s.matches("[0-9]")){
doYourStuffMethod();
}
You can also use the Matcher class from Android SDK which is an helper for this kind of stuff.

Categories

Resources