Android - Get data from a dynamic EditTexts by ID - android

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.

Related

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()

How to get user inputs from multiple fragments added to single activity

As shown in image, I have added the same fragment 5 times in activity by performing on click operation on ADD FRAGMENT button:
Now I want to get all user entered data from those 5 edittext on click of GET DATA button. Is this possible?
(Both buttons is in Main Activity)
First we need to traverse to the dynamic edit text linear layout and then only we can count how many edit texts are avaliable then read the values of corresponding edit text :)
//step:1
LinearLayout layout = (LinearLayout)findViewById(R.id.LinearDynamicEditTextID);// change as your //dynamic EditTexts' parent Linear Layout id.
//step:2
for(int i=0;i<layout.getChildCount();i++)// Count how many children avaliable
{
View v = (View)layout.getChildAt(i);// get the child view
if (v instanceof EditText)// verifying whether the child is EditText for secure.
{
EditText editText = (EditText)layout.getChildAt(i);//thats it.
Log.w("Edit Text "+i+" Value" , editText.getText());
}
}
Now I want to get all user entered data from those 5 edittext on click
of GET DATA button. Is this possible?
Answer: Yes, this is possible.
How?
Solution: You can make public variables in the activity and you can access those variables from the fragment which you are adding. On click of GET DATA button, you can read those variables set by the different fragment and show them in the same Activity or in different Fragment.

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.

Select Checkbox when its Value Equals the String from the EditText field

Problem: In my app, there exists two different ways for a user to add an item to a list: via a TextField and via checking an element in an ExpandableListView. Every time the user enters an item into the TextField, I would like to check if that value appears within any group of the ExpandableListView. If it does, then I would like to check that item.
Implementation of Solution: I have a HashMap that stores as keys all the values within the ExpandableListView, and its values are the corresponding group HashMap in which they are in. If the item is found then I plan to derive its location by using a second HashMap that stores its position within the group.
Question: Using this information, how can I check/uncheck that particular box? Specifically how can I identify a particular child of the ExpandableListView, and then identify a child of that?
Thank you for your time.
My approach is slightly different but it does the work.
Each element of yours must have a HashMap. Considering adding another element to it.
A string element call it ChkVal default value in it is false.
Now when you match the value from textbox to the element in that group. Using the hashmap.
Change its checkbox value.
Boolean b = false;
String chkCondition = hmap.get(chk).toString().toLowerCase();
if (chkCondition.equals("true"))
b = true;
chkBox.setChecked(b);

How can Dynamic Add and Remove View In android ? And this is also Store even if I closed the Application

In my Application I want to Add and Remove View (like Button, or Textview, Checkbox ) by Coding (Programming ).
In Details:
I have One EditText and One Add Button. User Enter Anything in EditText and Press the Add Button then this one is added in bellow LinearLayout, and whether User click on his/her added Button it will going to next LinearLayout.
I get sucess upto this.
but when user click the button in second LinearLayout then it will come back on first Linearlayout. I am getting error Here, i don't know where I made a Mistake.
And I also facing Problem about how can I Store this all. Like User Add 5 Button and closed the application and whenever he/she came back to application I need whatever he/she previously added.
Here is what i done.
http://dynamicandroidview.blogspot.com/2011/12/how-to-add-view-in-android-by-coding.html
Try to create a database table with minimum 2 columns in your case it will be id and buttonText.
Now when user clicks on the add button it will save text to the database and will create the button dynamically below any buttons which are already created before or as a new button.
Now in your onCreate method get the count of text thats stored in database.Some thing like the following code:
DB getData = DB.getInstance();
getData.open(this);
ArrayList<TextHolder> getList = new ArrayList<TextHolder>();
getList = getData.getAllTextFromGeT();
getData.close();
x = genList.size();
Here x will be the number/count of elements that are already stored in the database.Now you can another int say i and using this i and x in the for loop you can create buttons dynamically.
Inside the loop you can do something like the following to get text for all the buttons that are being created:
TextHolder firstOne = getList.get(i);
String text = firstOne.getText();
You will also need class with getters and setters method in order to convert DB elements into objects.Like in the above code getText() is our getter method which is getting elements from database and returning it here.
here text will be the text of the button.
So every-time users starts the application he will see all the buttons that he created when he ran the application before and newly added button will appear on the spot and also will be stored in the database for future retrieval.
Remember we are just storing text of the button and assigning it unique id which helps us to create the buttons.Hope this helps

Categories

Resources