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).
Related
I'm having a big form inside a recycleview, with multiple repeating items. Think of it like multiple forms: data of car 1, data of car 2 etc. under each other.
Now I'd like to validate all data before saving. Basically the problem is, that the views might not be visible when the user presses the save button.
What's a working way to do this?
My idea would be to scroll manually to each form viewholder THEN validate those, and just stop at the first invalid cell.
But is there a better way? Because this is a bit hacky to me. Is there a way to force a recycleview to actually create a view without it being visible on screen?
Thanks
You should do your validation things on your dataList, for example you have a big array in your activity and you passed it to your adapter.after user clicks on save button you should start validating process on your list(not your adapter).
for example in your Activity you have an ArrayList of car object (carList) and you passed it to your adapter.after clicking on save button you should do something like this in your activity class:
for(int i=0;i<carList.size();i++)
{
if(!isDataValid(carList.get(i)))
//do your stuff here
}
//notify your adapter here
I've been searching for an answer for this but still didn't found what I need.
I'm developing an android app that works with 2 fragments and each one have custom list view.
In the first fragment, I want to add a new item in list view and have the options to delete, edit or share.
On sharing item will be to shared to the listview of the 2nd fragment.
The 2nd fragment is where all users can see what other users have shared and also can add it to there list in the first fragment.
There will be a offline/local database(first fragment) and online database( 2nd fragment )
My question is what kind of storage I can use for that?
Also, how it will deal with the add, edit, share and delete?
I wish i can have some help, thank you
ps: I designed everything i need.. but I just stopped in the storage part.
You have two options to store data. First you can use the SQL lite database to store values or you can use NoSQL database like JSON where each and every value will be stored in the form of key value pair and light weighted.
When you want to remove some value form the list then firstly remove the value from the actual database and then from current ArrayAdapter of the list and at last attach the adapter to the view.
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.
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.
I'm writing my first Android app and want to pick up good coding practices. I have an Activity which contains a 2-column grid of all the data items available in the app (listActivity). There's an Activity to create a new data item (createActivity), which is triggered from the listActivity. Now, when the createActivity finishes, what is the best way to handle this situation with respect to the following:
Should the createActivity store the new data item in the permanent storage and return only the ID of the newly created item to the listActivity OR should it return all the data fields of the item as putExtras() of the returnIntent?
Should the listActivity 'repaint' the entire data view or should it simply append the newly created data item dynamically? Will the answer to this question change if the listActivity also has to handle delete & edit events? What if the list view is NOT a 2-column grid but a single column list?
Should the createActivity store the new data item in the permanent storage and return only the ID of the newly created item to the listActivity OR should it return all the data fields of the item as putExtras() of the returnIntent?
That depends on what your listActivity needs to know about the new item. Generally saving the data in storage and only sending back the ID should be enough - based on my answer to some of your following questions. You might also need some indicator of the action that has been performed (Create, edit or delete).
Should the listActivity 'repaint' the entire data view or should it simply append the newly created data item dynamically?
Yes, if something in the underlying Adapters data set has changed a repaint is most probably needed (or at least calling notifyDataSetChanged() on the Adapter). If the order of the items in the list does not matter, you could just append the item to the list - but I still recommend that you save it in storage and then retrieve it when you want to add it to the Adapter. This is also why you would only need to pass the ID around. Also in order to append you would need some way of knowing if the last listitem has one or two columns filled.
Will the answer to this question change if the listActivity also has to handle delete & edit events?
The edit functionality is basicly the same as created (except the fields have values when you open the edit view). If you delete an item from the list a full repaint of the list is probably the best thing to do, as to not confuse the order of the items.
What if the list view is NOT a 2-column grid but a single column list?
Appending would be easier - but there is not much difference. In a single column list you might be able to avoid some full repaints that would be needed in a 2-column list - though I don't think it will make much difference.