How to parsing in a fragment? - android

I made an application which has an Activity that parses a XML file (XMLPullParser) and shows data in a Listview. It works.
In the same application I have another Activity in which I put different fragments in dependece of the selected item of the Navigation Drawer.
Now, my question is: how can I use the data, obtained in the Parsing activity, and manage them inside a fragment?

You don't have to send the whole parsed data to the fragment.
You can write an own Handler or Manager class to parse the file for you and keep it in the memory (It can be some Singleton stuff). Once you parsed it you just have to pass the ID of the item and the Fragment can get it out from your Handler.
You can pass whatever you like to the Fragment like this:
public static YourFragment newInstance(int index) {
YourFragment f = new YourFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
//To get out
//...
Bundle args = getArguments();
int index = args.getInt("index", 0);
//...

Related

Value in the Fragment cannot be shown

I have a typo regarding getting value from bundle and show it in the Fragment.
My song class has two values title and its detail.
private String songTitle;
private String details;
After sending value from bundle in Adapter part, I get it from Detail Part by doing vice versa process. I show it via getSupportFragmentManager feature.
Although the detail page open without any problem, there is no value in Detail part.
Here is my Detail part and Fragment part.
How can I fix it?
Detail Part
if (savedInstanceState == null) {
// Get the selected song position from the intent extra.
int selectedSong =
getIntent().getIntExtra(SongUtil.SONG_ID_KEY, 0);
Log.i(LOG,"selectedSong : " + selectedSong);
// Create instance of the detail fragment and add it to the activity
// using a fragment transaction.
SongDetailFragment fragment =
SongDetailFragment.newInstance(selectedSong);
getSupportFragmentManager().beginTransaction()
.add(R.id.song_detail_container, fragment)
.commit();
}
Detail Fragment
public static SongDetailFragment newInstance (int selectedSong) {
SongDetailFragment fragment = new SongDetailFragment();
// Set the bundle arguments for the fragment.
Bundle arguments = new Bundle();
arguments.putInt(SongUtil.SONG_ID_KEY, selectedSong);
fragment.setArguments(arguments);
return fragment;
}
Link : Project Link
I solve it by defined static methods for list process in which items are inserted

How to pass data from one activity to other activity fragment in android? [duplicate]

This question already has answers here:
Send data from activity to fragment in Android
(22 answers)
Closed 5 years ago.
I have passed some key and value data from one activity to another activity fragment so I have not get key and value to the last point in the fragment.
I have passing data using bundle.
In your activity create bundle to set to the fragment
Yourfragment fragment = new Yourfragment();
Bundle args = new Bundle();
args.putString(ARG_DATA, data);
fragment.setArguments(args);
Then load the fragment getSupportFragmentManager().beginTransaction().replace(R.id.your_container,fragment).commit();
Then in your fragment oncreate get the data like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mData = getArguments().getString(ARG_DATA);
}
}
From activity to activity you can pass data by using Intent and when you get data in second activity in which you are creating fragments on creating fragment pass that data to fragment by making constructor in fragment or by using bundle. For further assistance and if you dont know how to do this do let me know.
Pass data from activity to activity:
Intent intent = new Intent(this, Second.class);
intent.putExtra("data", sessionId);
startActivity(intent);
get data in Second activity:
String s = getIntent().getStringExtra("data");
Pass data from activity to fragment:
Bundle bundle = new Bundle();
bundle.putString("data", "From Activity");
// set Fragmentclass Arguments
FragmentOne fragment = new FragmentOne ();
fragment.setArguments(bundle);
get data from activity to fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("data");
return inflater.inflate(R.layout.fragment, container, false);
}
Happy coding!!
You can pass data between Activity and Fragments and Fragment to fragment using below:
https://developer.android.com/training/basics/fragments/communicating.html
But to pass it between activity, you may use bundle data, (very few variables) or use Application Class to store it in memory, make sure you do not bloat up your memory.
New Android architecture components also do provide good options, it all depends upon your use:
https://developer.android.com/topic/libraries/architecture/index.html

Pass parameter from activity to fragment

I have an activity that calls to a web service and I want to pass these result to a fragment. Obviously the web service is invoked by an AsyncTask, so the fragment is loaded before getting result.
How can I pass this paramteter from activity's AsyncTask to fragment when is received?
You can implement a method inside your Fragment and call it when needed. For bidirectional communication between an Activity and a Fragment see http://developer.android.com/training/basics/fragments/communicating.html
Set bundle in your fragment.
Bundle args = new Bundle();
args.putInt("id",value);
Fragment newFragment = new Fragment();
newFragment.setArguments(args);
In your fragment get the bundle as
Bundle b = getArguments();
String s = b.getInt("id");
You could move the AsyncTask into the fragment.
But if you wish to keep your current set up, you should save the Fragment reference when you initialize it, and create a public function in the fragment that takes the new data as a parameter.
So the activity code could look like this:
MyFragment fragment = new MyFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
and then you could call:
fragment.updateData(myNewData);
Just make sure to do the appropriate null-checks, just to be safe.

Android: Passing a non-primitive ArrayList type from one fragment to another in a bundle

I currently have this Custom ArrayList:
ArrayList<PlaceDetails> place_list = new ArrayList<PlaceDetails>();
which will be populated during the onCreateView() portion.
I am unsure as of how do I pass this ArrayList in a bundle from this fragment class to another fragment class. Below is the snippet of my codes:
public void Map(View view){
if(hasConnection() == true){
Bundle b = new Bundle();
// how should I be passing the ArrayList in this bundle?
FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
TOnlineMapViewFragment mapfrag = TOnlineMapViewFragment.newInstance(b);
ft.replace(R.id.container, mapfrag).addToBackStack(null).commit();
}
}
So I've created the bundle and I wanted to pass it to the next fragment with the newInstance() method. How should I do this?
Consider implementing Parcelable interface in your classes. Then you would be able to store PlaceDetails in Bundle and pass it to setArguments() method.
I've found a tutorial: http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
I haven't testes it but it looks good.
Are you really sure a bundle is really needed?
TOnlineMapViewFragment mapfrag = TOnlineMapViewFragment.newInstance(b);
mapfrag.setPlaceList(place_list)
Should be trivial to create setPlaceList(ArrayList<PlaceDetails> place_list) ...

Send data from activity to fragment

How can we send data from actvity to fragment? The Fragments are configured to actvity by using FragmentPagerAdapter.
Regards
mini.
You can pass a bundle to the fragment on creation with setArguments.
You can create methods to set the data on the Fragment class.
You can perform this by using Bundle
Send data from the activity (or fragment) :
int a = 5;
Bundle args = new Bundle();
args.putInt("INT_DATA_TAG", a);
Fragment fragment = Fragment.newInstance(args);
//Making fragment transaction
Retrieve data in the fragment
int a;
public static Fragment newInstance(Bundle args) {
a = args.getInt("INT_DATA_TAG"); //use a constant for the tag
return new Fragment();
}

Categories

Resources