how to create multiple textviews inside a fragment? - android

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.

Related

listfragment android layout

I'm trying to create a layout in my fragment that allows a user to enter their workout. The fragment has an ArrayList of sets, and each set consists of an exercise name, sets/reps, and rest time. I can only populate so many sets at the beginning, and want to add a button at the bottom 'add exercise' in case the user wants more exercises than what the loaded layout allows...example..
Ex #1
sets/reps
rest time
Ex#2
sets/reps
rest time
[add exercise]
I tried using a listfragment and an adapter, but im not sure how I can withdraw the data that is entered into the edit text fields and save them for later use. Any help is greatly appreciated, thanks in advance.
Using adapter is a good idea.
Here is one way to achieve your goal:
When person clicks on add exercise button, you should get the new exercise information from the user. You can use Dialog or another activity-screen for that.
Once you got the info(sets/reps and rest time). Go and update the ArrayList that populates the data in the adapter. And when you done call: Adapter.notifyDataSetChanged() this will make the adapter to invalidate the view based on the changes in the data(ArrayList in your case).

In Android, how can I make a form that changes depending on categories ? (the amount of inputs varies)

I'm doing a reminder application for Android that has an activity which is populated with inputs that depend on the category of the reminder. The amount of inputs (EditText) changes depending on the category, so im a little confused on how to tackle this case.
I propose 3 solutions here, but I'm not sure if there is a 4th solution or a better way to do it. I'm open to criticism. These are my choices:
I can make fragments for each category
Or I can make a common xml layout file that contains a number of inputs that reflect the category with most inputs, and find them programatically and assign them their properties
Or I can make the inputs programatically.
I'm trying to find the most elegant solution here. Thank you for your help.
What you can do is write all the inputs in one file,then set their visibility to hidden, and only programmatically change the visibility to show when you want it to. Tht way if you want to show three inputs, you can change the viibility of the three inputs and the rest of them would still be hidden, hence solving the issue.
Use some kind of adapter-based View. I recommend RecyclerView.
With it, you can covert your form objects into scalable View hierarchies in an elegant, efficient View, using an Adapter object.
You can use <include> to add your different screens and assign the visibility programatically

Listview filling from database

good evening , I need help about filling a list view from a MySQL Database,
i don't know how to do it becaus i'm beginner in android
i wanna icon and 3 text views the first is title bold and the second is the RIB and the last one is in the bottom and right of the list view
Your question is a bit too generic and not clearly defined. That's probably why it got down voted. I'd say your best bet is to start with the documentation. Start by reading about ListView and possibly Adapter and try it yourself. If you still have problems, ask a specific question about something you don't understand or can't get working. The best specific answer I can give is that you will need to define a custom adapter to create the item view you want. You will most likely want a layout xml for that item view.

Calling a custom dialog from a database?

Following on from this question:
ideal database field for calling data to
I have realised that I might be able to simplify my need for information to be brought to the screen by using a Dialog or a Custom Dialog, and by one of these methods I may call the whole finished page from a database instead of using static strings into the layout filling in specific fields with data like I am currently.
There could be a few hundred items or pieces of data to be called up in my App So am I correct with this assumption or do I need a specific other method to acheive this?
I am looking to call a custom dailog from a database so I can call a whole page of information complete but am unsure of limitations on this data wise or even if my sqlite database manager is even good enough for this task, each page will be called up "complete" where as before I was trying to have a template with the unchanging data (coke pepsi etc.)already included and appearing on every "result" page and then blank fields populated with data retrieved from a sqlite database (Litres size etc...) I am unsure which path to take : either make the page complete including all data like an image Or do I stick with my original plan to have a template screen with unchanging fields already in place and the changing information to be populated as needed from a Sqlite Database? hope this makes sense sorry if I was too vague with my question originally Im still learning.
Your question is vague but I'll risk an answer:
If you want to make a dialog like the image you posted on your previous question then this is the way to do it:
Create a custom dialog that will have as content a LinearLayout(with 3 TextViews representing the header labels in your image: COMPONENT, TYPE and AMOUNT, this will be somewhat identical to the list row layout below) + a ListView where the actual data will be shown.
Create a layout file for your ListView row to use(like the LinearLayout above will contain 3 TextViews for the drink name, type number and amount number).
When the dialog needs to be shown just get from the database all the drinks names, types and amounts and bind it to the ListView with one of the adapters based on a Cursor(like a SimpleCursorAdapter)
Getting the values from the database and binding them to many TextViews is not recommended(If you have many(I saw few hundred items or pieces of data)), also your dialog could be called a lot of times by the user in a short period of time and you don't want to do this binding each time for many views.
either make the page complete including all data like an image
Please don't do this!

How to handle ListView in this situation Android?

I ran into the situation that I need a way to edit the data of list-view item from another activity. I can handle the click event on each item, then edit it on the fly. However, I still prefer to handle all the editing in a separate activity. My listview item is customized from BaseAdapter.
Here is my main page,
Each item within the ListView, contains two other TextView. When the I hit the menu setting, it will go to another activity where I can edit and update the information:
I'm currently having two solutions in mind. Either retrieving data from the previous activity and update the whole ListView (which I think it's kinda expensive in the case user just edit one item). Or I can just get rid of the ListView and create a bunch TextView in the xml, by doing this I can just reference to each TextView by their id. So my question is, which way is preferred in this case? Any suggestion would be greatly appreciated.
Your ListView is displaying Email, Name, Headline, etc? That should be a fixed XML layout with TextView entries, I think. ListView is for variable numbers of elements. You CAN implement it with a ListView, but I wouldn't.
However, your concern about updating the whole list being overkill, I wouldn't worry about that either. You're talking about 7-10 fields. The amount of time Android needs to run through its lifecycle and display everything will dwarf you updating a few fields.
You can use SharedPreferences for this. You can create a wrapper class through which you can access the preferences.Thats the usual way to go about solving these kind of problems. You can check this for details.
You can have it as a variable in your application class, so that you can access that in a global context.
Use text views instead. List View code has been optimized for large amounts of data only and not recommended for small data.

Categories

Resources