How to refer to a certain TextEdit in a specific Android Fragment? - java

I'm developing an Android app and for this I want to have a fragment with which the user can insert an amount. The fragment has a couple methods, like inserting the correct currency symbol on the basis of the country.
I now want the user to be able to insert several amounts on the same screen. As far as I understand I can reuse one fragment several times for this. Every time the fragment is used in the main xml it gets an id, and every fragment contains a couple EditTexts which each have an id as well.
I now wonder how I can get the value of a certain EditText within a certain fragment. So lets say I want to get the result of edit_text_2 from within fragment_3 (both are their respective id's). How would I do this?

You can get result from fragment via callback. Please check following documents:
http://developer.android.com/training/basics/fragments/communicating.html

Check these links out:
http://developer.android.com/training/basics/fragments/communicating.html
http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
Fragments are made to be self-contained, so that they don't know what is outside of them, and their containing activity doesn't necessarily know what is inside of them. You need to create an interface for the Activity to call to get the reference to your EditText.

Related

How can I turn 40 similar activities into one that holds different data?

I want to make a recipes app, the basic layout is the same for all the recipes the only thing that changes are the images, times and ingredients.
The problem is, I could make 40 activities, one for each recipe and performance wouldn't be a problem because the user is only interacting with one activity at the time. However, writing the same code and going on a copy paste spree feels wrong.
I would have to repeat the same code over 40 activities and it would work (I guess), but it would be much easier to create one activity with the functionalities I want like a timer and the layout and in some way make smaller files that insert the data for the selected recipe in that "pre-made template".
There's must be a way of doing it, although I'm not experienced enough
Here is an example layout
It is usually good practice to have a base activity that implements all the code common to several activities then those activities can simply inherit from the base activity like this public class ChildActivity extends BaseActivity.
This will allow you to call methods that are in the BaseActivity from any of the child activities. You can have a read up on Java Inheritance here and here is a blog post with some examples of using a base activity.
You can create only one activity that will receive the Receipt data as extra using Intent. The layout for this activity should contain an image view(or a recycler view to hold all your images), a recyclerview to show your steps/ingredients and a textview for the time.
Receiving these data from the activity(that one that the user selected which receipt he wants too check) that created this new activity, all you need to do is to setup your layout with this data.
Check this question to get how to pass data between activities
Click here to see how to create recycler views.

Update RecyclerView From Different Tab In Viewpager

I have a ViewPager with 2 tabs. Each tab just holds a Fragment with a RecyclerView. The RecyclerView in the first tab holds a list of all Articles while the RecyclerView in the second tab only holds the list of user's favorite Articles. Each Article in the first tab has a button in the RecyclerView row that allows the user to favorite it. I want new favorites to be displayed automatically (without forcing the user to manually press the refresh button) when the user navigates to the second tab. How can I go about doing this? Essentially I need to add / remove items from the RecyclerView which contains the favorites, but from the first fragment.
Things I have tried
Call adapter.notifyDataSetChanged() in the onRefresh() method of the favorites fragment. This works, but the performance hit is big because notifyDataSetChanged() is not a cheap operation. I would like to avoid this if possible.
Tried following this guide, and implemented some interfaces. The problem is, when I try to reference the adapter in my fragment from the containing activity, the adapter is always null. I can post code if needed to show what I was doing if this is supposed to be possible.
Now I am considering using SQLite DB to store changes (favorite removals/additions) in the onClickListeners, and then checking that table in the onResume() method and processing the changes individually, one by one with notifyItemChanged()
Is there a better way to do this?
For SQlite use CursorLoader to load data from SQlite. You can register to be notified when a data changed in a table. Then your loader will automatically load data again. Check this answer. Also check this Thread.
For getting data from internet you can use
Communicate with 2 fragments via parent Activity. Check this and this answer. This tutorial is good for better understanding.
LocalBroadcastManager to notify from one fragment to another fragment.

How can I make a single search for two different lists belonging to different activities in android studio?

I have two different activities :
one having department list and another activity having employee list.
I want to add search functionality to my app such that it can search data for all activities from a single activity.
Please suggest if it is possible and how ?
For code you can refer How to pass data collected in arraylist from json asset file in one activity having recyclerview to another activity again having recycler view?
That means you need to search data of all Activity in single Activity.
So you need to add include conman SerchView in xml file of all Activities. Also Implement BaseActivity that holds search Implementation for all of your Acclivities.

how to reuse single activity infinite times?

My app has an album style feature that users can pick/take photos from device and load it in every page in this album.
Now the number of this album pages is up to user and should be unlimited. On the Other hand the codes that is being used in activities as you could imagine are exactly same. I have wrote code once and I just want to reuse this activity for each page of album. how should I achieve this?
Since there are thousands of apps that are provide this kind of functions I know there is a good way but i'm unable to find that. Just a trick or a link to a tutorial or even a small explanation is good enough.
Thanks in Advance.
You can create a next() method, and change every layout view in the page with this function, so that you are in same activity but only views are being changed.
One solution is to add the data to be displayed to the "extras" of the Intent which you use to start the activity.
Alternatively, you might want to look at using a Fragment instead of an Activity. You might also want to look at using ViewPager to be able to swipe through the fragments which display the album data.

How to access objects in another source file

Sorry for my amatuer question, but I'm really new to Android, and even Java.
I have a listview layout in my appwidget. The listview items are handled in widgetservice.java, where i declared an array to store the listview items in.
There is a configuration button for every listview item. When you click on them a new activity starts, and i really need to acces the listview items in this activities (so actually an element of the before mentioned array).
I did some research how to do that, and I came accross with the Parcelalbe class, so that i can attach a Parcel to my Intents. That is great, however the parcels seem to be just mere copies, and i need my original array elements (so i can change them in the activities, etc).
I hope you could understand my problem, and would able to help me.
Cheers.
Parcelable is one way to go but unless you are just dealing with basic, single dimensional, String only Lists/Arrays than I wouldn't go down that route as it can get complicated quite quickly and is not hugely flexible.
The preferred way to go about this is using Fragments. Such as the ListFragment in this case.
Your ListFragment contains all the bits and pieces it needs to display a ListView. It should generate (or be able to access) it's own collection of items and know how to display and control them. The benefit of putting it all into a Fragment is that you can then easily reuse it in any Activity or layout.xml you like and make use of the same functionality again without having to duplicate all that code.
If you need your ListFragment to be dynamic and not just display the same list all the time you can pass it some parameters through a newinstance(...) method. That method will itself wrap all the info and arguments you want into a Bundle object that you will use to configure or populate your Fragment. Here's how to go about that.
What that ensures is that if your Fragment needs to be recreated (screen rotate, Activity change, Application focus etc etc) it will be able to recreate itself properly using the same parameters again without you having to intercede.
It may seem like more work now to try to get your head around the concept and use of Fragments but in the long run it will save you time, spare you headaches and let you write more maintainable and appealing code.

Categories

Resources