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

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.

Related

How to validate and set errors in recyclerview having multiple edittext?

I have a recycler view with multiple edittext and i need to validate each and every edittext before clicking add button and on tapping add button it again creates new edittexts but i need to validate previous edittexts before creating new ones and set errors how to implement this?
You need to add EditText values in array list and validate it when you click on Add Button like
val list = arraylistof<EditTextModel>()
add.setonclickListener{
val getItem = list[list.size.minus(1)]// get Last index of list
// because when you click add button always check last index because you already //check previous. when you add views on index 1 or index 2.... so on
if(getItem.editTextValue1.isEmpty() || getItem.editTextValue2.isEmpty()) {
Log.e("TAG","Edit 1 or Edit 2 may be empty. Please add values")
}
}

How to display database contents in a new activity at onclick of a button?

thing is, if i click a button GETDATA - then a new activity starts new.java and i want it to display the contents of my database.
in this new.java i have strings name and id. and i have lined database and this class, such that name and id have the corresponding details that i want to display.
now the ques is, how to display them ?
if i have a
EditText ta;
ta=findViewBy(R.id.edittext1);
ta.setText(name);
should this work ?
it isnt working. also, i dont want user to change the contents and want to make this filed un-editable.
should i use
TextView tf;
ta=findViewBy(R.id.textview1);
tf.setText(name);
?
even this isnt happening.
what is the procedure for this ?
it is done by using append.
TextView ta;
ta=(TextEdit)findViewById(Ri.id.textView1);
String a=getData(1); // goto database, id is 1
ta.append(a);

Getting Data from 2 edit fields in sublayout in Android

I want to get data from two edit fields in a sub layout . See the image below.
Image
I am writing code to dynamically add sub layouts with 2 edit fields in it. I can get the text from both edit fields manually with this code.
View childView = subLayout.getChildAt(0);//0 for 1st field, 1 for 2nd field.
String s = ((TextView)childView).getText().toString();
but how to get data from both using for loop.
Basicly, every child in your layout is subclass of the View class. Once you get a child, use instanceof to check if this child is actually a TextView
for(int i=0;i<subLayout.getChildCount();i++){
View v=subLayout.getChildAt(i);
if(v instanceof TextView){
Stirng s = ((TextView)v).getText().toString();
}
}
You must to create database somthih like thisenter link description here

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.

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