Android - get values from dynamically added fragments - android

What I have created till now is as follows:
Please see this image
Purchase Screen
As shown in the image above, the screen (PurchaseActivity) initially has:
1. spinner for customer - line 1
2. fragement containing spinner for product, rate, and a delete fab - line 2
3. linear layout for displaying fragments same as above. They will be dynamically added.- line 3
4. fabAdder, adds fragment dynamically - line 4
5. save button for saving to persistent layer. line - 5
To add views dynamically, is this the right way. If so, how do I get the values out of the views inside fragment which are dynamically added in my activity.
Otherwise what is the right way of handling/providing the user with controls to add data dynamically.
Advantages of using this method:
The user will be able to add as much product as he wants, he may delete it conveniently and also editing is easy.
Please use the comments section to if you feel the question needs more content.
Answer or lead me in right path to fulfill this requirement.
-Newbie Androider

You can make outside class similar to POJO for handling values from fragment simultaneously. Something like this answer of mine for some other question: https://stackoverflow.com/a/44051019/5577679
Otherwise, why don't you make good old POJO for values?

Related

how to create multiple textviews inside a fragment?

I am trying to create a fragment and inside that i want to create multiple textviews whoose text is set from the data retreived from the database(mysql).
And also i want to have the feature "refresh" by which the textviews are updated with recent data from the database.Iam trying to do that but iam not getting enough idea how to do it as iam new to android programming.
Please help me with this. thank you in advance and any suggestions are acceptable.
i.e
I have database of a person and i want to get his name and address from that,then in my activity i want to create a fragment that displays a name and address in separate textboxes and if address or name exceeds some length then i can press "more" to see left information
Consider using a ListView to populate data from the database, and a SwiperRefreshLayout to refresh it. Check these links for more info :
SwipeRefreshLayout
ListViews
The answer depends on what your app will do.
If you have a fixed number of TextViews, and the number won't change in the future or depending on the answer to the database query, you could simply stick to normal TextViews; if the number changes, I suggest using ListViews or ExpandableListViews.
Regarding the layout, you can create a normal layout using AndroidStudio's layout designer, and then inflate it to your view.
For the refresh, you can add a button that calls the method in which you fill the textviews/listviews.
Actually your question is too broad: if you want a more specific answer, you should edit it asking what you really need.

What is the best way to change between different types in a android quiz?

For a school project a need to build a android quiz application with multiple question types. These question types are: Multiple Choice, Textbox, Radio button and a minigame in which you need to sort images in the right order in a draggable gridview.
I think to use multiple activities for this isn't needed and that i just can switch the overall layout between multiple xml layout files for each of these question types if i first check what the type of the question is. Most other posts on stackoverflow are only related to quizzes with one type of question, without minigames in between the questions so i wanted to know what the best method is to switch between these different question types and layouts. At least i know i need a switch/case to check the different question types (for my prototype located in a array) and switch to right type if you press the "Next question" Button that is located under the answers (under the gridview in case of the minigame).
I already searched on the internet for it and i found the following options but i don't know what's best in my case. I also wanted to make the app compatible with android version 2.3.3. Some examples could also help me very much!!
(Adapter)Viewflipper
An Adapter that changes a inner layout
setContentView() of the main activity
Including a layout in another layout and change it
Make some views/layouts visible and invisible.
Make a new activity for each question (if there is no other option)
You can do it with a ViewFlipper. In the ViewFlipper XML, just list each layout for each question type and just have the user scroll through the flipper to get to the type they want. You can probably do this by setting a OnTouchListener and calling showNext() as they scroll.
Setting the correct data for each view is solely up to you; you would just have to match the correct data based on what view they are in to populate it correctly. That's it :)
It depends on how you want to structure your quizes are the questions supposed to be mixed together? Are they all seperate quizes.
If they are separate the easiest approach would probably to implement a view pager. With the view pager it will create a new fragment for each page. From here you have a two options, you can either create a new fragment for each quiz type. Or you can create one quiz fragment and pass an argument to that fragment to select the correct layout. Either way you'll likely want a new layout for each quiz type. As for your minigame you can just include that as a page in your view pager in between two quiz views.
All of the above will work on 2.3 and above if you use the support library. I would recommend checking out some tutorials on viewPager they are fairly straightforward to use.

How to re-order the existing xml layout views programmatically

I already have one xml file called my_times.xml.
It contains views like, TextView, EditText, Spinner and ImageView.
Also I am using one web-service, which tells me the order of these views.
For Example, for one of my form field, say FirstName is appearing in the order 1, so this view will come in the first place of my form. If admin on the server changes this order to 2, then FirstName will come on the screen at second position and so on.
Is there any way to rearrange/reorder the existing xml layout, without disturbing the existing
xml layout.
My sample my_times.xml looks something like this:
<LinearLayout>
<TextView.../>
<EditText..../>
<TextView.../>
<Spinner.../>
</LinearLayout>
Please suggest me the ideal way to rearrange these existing views.
There are about 4 to 5 such screens which contains the form like fields that I want to rearrange/reorder dynamically based on the web=service response..
I didn't actually guess it but appears like a blink in my mind and tried the following solution and it worked...!
As I told earlier that my Web-Service was giving me the order of those form fields.
I made one method, which is returning me the order exactly like its on server.
Using this method I simply used the following two methods to re-order my views :
private void removeExistingViewsFromXML(){
lnrTimesContainer.removeView(tv1);
lnrTimesContainer.removeView(edt1);
lnrTimesContainer.removeView(tv2);
lnrTimesContainer.removeView(spn1);
}
And after getting the order and depending on the condition, I used the method :
lnrTimesContainer.addView(tv2);
lnrTimesContainer.addView(spn1);
Thats it...! :-)

List View Pagination

How do I do pagination using ListView. For an example, I have 12 records in my ListView and I want to show first 10 data in 1st page and the remain data in next page.
You can add a footer on your listview that will hold the buttons for next and back. In an stackoverflow question you have the complete solution for that.
Next step can be done in several ways. The simplest one, that I am seeing right now, is: Save the page you are at a variable. Then, each time you do next you add one to that variable. In your adapter, in the getView you multiply that variable by the position so you can get the correspondent position depending on the page the user is.
Better way to build view like pages, use Fragment,You can show desire content in pages as you mention in your question, for more details look out section 21 example,it cover fragment.

Android: Best way to fill Activity with new data

i got the following "problem".
I want to have an activity thats shows me the standings of some teams at a specific gameday.
therefor i would add a spinner and a TableLayout. At the first Start the activity should show the standings of the actual gameday but then you can choose any other gaymeday and the standing should get updated.
Whats the best way to create this activity?
assemble the whole TableLayout with all TableRows and TextViews, give them ids and update those views via id during runtime. Problem: huge unflexible hardcoded layout.xml
assemble the layout during runtime, add ids, update via ids
assemble the layout during runtime. on update remove old views and create new ones
assemble the layout during runtime. on update restart the activity
just whant to know which one is the best. or is there any other way to achieve that
thx Cheetah
If I were you, I'd actually use a GridView with an Adapter. This will abstract away all the handling of layout changes. You just have to worry about mapping your data to appropriate views. This example maps ImageViews to a GridView, but there's no reason you couldn't map to TextViews containing your data in a GridView. Also, because you're using an adapter, you can take advantage of all the Loader classes and they're asynchronous loading capabilities.
In addition, using the approach will allow you program to easily adapt as your dataset changes. You may want to add more data to the table in the future and this approach will allow you to easily do that without having to constantly change your xml layouts.
Does the number of views change? If no. Best way is to use the already existent views and update their values. Try to avoid recreating/reinflating views since that's an expensive task.

Categories

Resources