In my current Android project, I am adding a layout file for an activity composed by a list of EditText fields and a Button in the end.
I am looking for a way to, when the button is clicked, the method associated to the click retrieve all ths fields and store them in a array of Objects (something like Object object[]).
Anyone knows if this is possible and how to do that?
You have to get a reference to each of the EditTexts and then get their current value, one by one.
Related
I want to create an app for personal use and i'm lost.
It needs to have 9 text inputs and 1 save button.
Saving data to second activity with 9 rows.
1 row for each input text.
Please help.
If understand correctly you want to get user input in one activity and display this input in another activity. Text input is commonly done using an EditText while TextViews are used for showing text. You can put these in your layout XML file and then find them by their resource id. Then you will have to set up a callback for your save Button where you get the input from all of your EditTexts. I assume that you want to go your other activity after that so you can collect the CharSequences into an ArrayList. Then you create an Intent for you other activity and put the ArrayList into your Intent.
You can get the ArrayList again in your other activity via getIntent().getExtras().getCharSequenceArrayListExtra(yourKey) and fill your TextViews with the text from your ArrayList.
I have a database object which includes a lot of String fields. Right now, these string fields are managed by the user through an activity with a lot of edit texts. However, I'm having trouble saving all of the information from my EditTexts to a database. I've tried doing this when the activity calls onPause but it is not working how I would like (I'm using a ListView with the EditTexts so it's hard to say if the views will be there). I've been looking at the text watcher but it seems really tedious to add one for every EditText, some of which are created dynamically.
I've considered extending EditText and implementing something to use the TextWatcher but I'm again not sure about the best way to go about this.
Anybody have any suggestions on how I can accomplish this? Thanks for the help.
I would say you should hold a reference to each of these EditText objects in an ArrayList and then use an array list adapter to provide the data to your listview.
when any of the EditText's are changed you can call notifyDatasetChanged on the adapter.
in the onPause method you can loop thru the ArrayList and save each to your database.
Couple of ideas spring to mind:
1.
Implement a save button at the top/bottom of the list view or in the action bar.
The save button onclicklistener would grab each text in the list view and update the relevant fields in the database.
Place the saveButton work in the UI thread so it will block until completed and the activity won't be destroyed. you could also create a progress dialog just to let users know it's being updated.
2.
Implement a custom adapter for your listview and in the getView() method of the adapter (you have to override this anyway) add a text watcher dynamically... that way it's not all that arduous for you to add them individually
i am implemeting sample application in android for creating Dynamic Views.
Please observe below image............
By seeing the above picture i want to create a dynamic view it consists of close imageview, button, spinner and edittext by clicking Plus button. After entering all fields then i click on plus button create new view with above widgets similarly.
I want to delete particular row by clicking delete imageview. and again add by clicking plus, repeat this process untill how many rows we want. Then we click on submit button get all rows of views values and show those details on next screen....
And also tell me other way that is, Is there any possibility of create seperate xml file for that view and use it on our code...
Like see below picture...
Please help me....
Thanks in advance.....
If you Store the Views in a iterable data structure like List, you could iterate over them when a "delete"-Button is klicked and search for the ones to delete by checking the coordinates by calling getX() and getY().
However, like lfor said before, the better way would be to create an own data structure for each row. I would recommend to create a "Row"-class containing the views you want to add per row including the "delete"-Button and passing the Activity you want to add the rows to as a reference. By doing so you can add the components for each row by calling the activity's method addContentView() inside the "Row"-class. Also you know which Views (all of the "Row"-Object you're in) you have to remove, when the delete button is pressed.
I know I can pass a single variable with android:tag="x" and retrieve it with view.getTag().
How can I pass and retrieve more than one?
EDIT: What I was trying to do was use the tag attribute in a button to pass a value to the method invoked when it is clicked. I was wondering if it is possible to use two or more tags in a Button element. I have since discovered that I am going about it the wrong way (I'm an android noob). I was trying to use the Button in the xml to store button specific data when I should have given it an id, and then used the id in the code to differentiate the buttons.
You can declare global variable and set values of that variables and can use in button onClick(...) method
I'm trying to come up with a scalable way to link two edit text boxes together for unit conversion. The user would enter a value in either of the boxes and the converted value would show up in the other.
I would normally just make it so when one EditText was edited, it would get the input, pass it through a method to convert the value and set the text of the other field.
The problem is I have a lot of these pairs and each pair is used to convert a different kind unit. If I used a TextWatcher for each box, you can see where that would start to get out of hand.
I thought about extending the TextWatcher so I could pass it the EditText View and its partner's view, but I'm not sure how to pass it what kind of conversion method needs to be used. I could assign an int to each kind of conversion and use a switch, but that doesn't seem like a good solution to me.
Is there a better way?
This is how I would do it. Create a class, say PartnerEditTextInfo ,which contains a reference number and a EditText obj. Attach this as a tag to every EditText in your app. Set the editText in the PartnerEditTextInfo to the partner editText and have a unique reference number. Thus every EditText can get a hold of its partner.
Extend the EditText class and over ride onTextChanged() method to call a common conversion method(This can be a static class static method) . You can get the partner editText by getting the tag object of the editText whose text has changed. Then ,based on which pairs of editText(based on reference number) is changed apply required conversion formula and do the setText on both the editTexts in the pair.
Caution- You need have a way to make sure you wont get into a infinite loop, have some sort of flag to differentiate changes to editText's text made by user and made by the conversion method.