I have a fragment which summons a custom CursorAdaper and display it on a ListView
. The thing is I want to change the cursor by changeCursor() from another activity when I add new data, How can I get access to the CursorAdapter displayed on the fragment?
Essentially you have to pass data from one Activity to another and let the fragment of you choice receive the data (each fragment of a given Activity may get the Intent that started/restarted/resumed the Activity).
Consider this code
-- pass data:
String[] myListEntries = getNewListContents();
Intent updateList = new Intent(this, ActivityThatListFragmentBelongsTo.class);
updateList.putExtra("updated_list", myListEntries);
startActivity(updateList);
-- receive data (in fragment):
#Override
public void onResume() {
Intent wasStartedWithData = getActivity().getIntent();
String[] updatedList = wasStartedWithData.getStringArrayExtra("updated_list");
// pass updatedList to adapter
}
Now you might actually have more complicated data than an Array of Strings. In this case
you could create a class that implements 'Parcelable' (http://developer.android.com/reference/android/os/Parcelable.html) and call
putExtra(Parcelable parcel) / getParcelableExtra(String name) on the Intent.
Related
I have one list view and there are different values within it such as name, qty,unit,disc,price. I want to pass these values to next activity. I set 5 text boxes in new activity. this new activity is for update the values. how to pass values and display the same in new activity.???
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), Update.class);
myIntent.putExtra("ItemName", "value");
startActivity(myIntent);
}
});
You can use following ways for sharing data in android:
Intent and put extra data in the Bundle
SharePreferences in the Application class
SQLite database
Static variable
You can pass the data via Intent Extras eg :-
Intent intent = new Intent(this,calledActivity.class);
intent.putExtra("Caller",THIS_ACTIVITY);
startActivity(intent);
In this example Caller is the Key and THIS_ACTIVITY contains the value to be passed. This is then retrieved in the called activity like :-
String caller = getIntent().getStringExtra("Caller");
You can pass an entire object through Intents, as long as they implement the Parcelable / Serializable class.
public class Myclass implements Parcelable {}
Implementing these classes will ensure that your Object can be serialized.
Then you will be able to attach it to your Intent, and later retrieve it by calling getParcelableExtra or other methods.
I am trying to send this object information to another activity with intent. All information is in this picture.
First Activity
Intent intent = new Intent(getApplicationContext(),AnotherActivity.class);
intent.putSerializable("VALUE", listItem);
Second Activity
Intent intent=this.getIntent();
Object list = intent.getSerializable("VALUE");
Please change Object to GeoLabStudent
you should use adapter's reference to get item
GeoLabStudent object = (GeoLabStudent)geoLabAdapter.getItem(position);
Make your model GeoLabStudent to serializable or parcelable for sending your object to other activity
Example
I'm trying to transfer ArrayList between activities, but nothing I've tried works as well.
This was my best shot, but this didn't work either.
I'm calling the external action here:
getComics getComicInfo = new getComics(charName, pageNum);
getComicInfo.execute();
getIntentData()
and here i'm trying to put data, but the problem is due the fact that this is an external action, so I can't shift through activits.
if(counter == comicInfoObject.length()){
Log.v("check arr length =>" , Integer.toString(comicList.size()));
Intent i = new Intent();
i.putExtra("comicArrayList", (ArrayList<Comics>)comicList);
}
and here i'm tring to retrive the data, but it doesn't get inside the "if"
public void getIntentData(){
Intent i = getIntent();
if(i != null && i.hasExtra("comicArrayList")){
comicList2 = i.getParcelableExtra("comicArrayList");
int size = comicList2.size();
}
}
first code is where I call an external class that using api and in the bottom line creates the arrayList
second code is inside the external class, where I'm trying to pass the arrayList with putExtra
third code is where i'm tring to retrive the data after getIntentData().
Pack at the sender Activity
intent.putParcelableArrayListExtra(<KEY>, ArrayList<Comics extends Parcelable> list);
startActivity(intent);
Extract at the receiver Activity
getIntent().getParcelableArrayListExtra(<KEY>);
You need make a Comics class that implements Parcelable, then use put ParcelableArrayListExtrato pass arraylist.
Here is a sample link for Pass data between activities implements Parcelable
However be careful if your array list too big, you could get intent exception, for this case you could think about a static reference to store your array list.
I want to pass data between two fragments without using activity and fragment activity.
I don't want to pass data between fragments using activity like this : Communicating with Other Fragments
Below is my scenario :
I have one Parent fragment and inside that there are two child fragments.Now my need is to pass data between these two fragments.How to achieve this?
I looked into this : Event Bus but not getting working example for fragments.
Is there any other alternative to pass data between fragments?
Any help will be appreciated.
Edited as per InnocentKiller's answer :
In FragmentOne , I have implemented :
FragmentTwo = new FragmentTwo();
Bundle bundle = new Bundle();
bundle.putString("Hello", "My name is Siddharth");
fragment.setArguments(bundle);
In FragmentTwo, I have implemented :
Bundle bundle = this.getArguments();
String myInt = bundle.getString("Hello","Test");
mStartTripButton.setText(myInt);
Best Way to exchange data between activity/fragments, fragment/fragment/, activity/activity, class/ class, make a common singleton class like:
public class DataHolderClass {
private static DataHolderClass dataObject = null;
private DataHolderClass() {
// left blank intentionally
}
public static DataHolderClass getInstance() {
if (dataObject == null)
dataObject = new DataHolderClass();
return dataObject;
}
private String distributor_id;;
public String getDistributor_id() {
return distributor_id;
}
public void setDistributor_id(String distributor_id) {
this.distributor_id = distributor_id;
}
}
now set from anywhere(Fragment, activity, class) at any event before you move to new screen
DataHolderClass.getInstance().setDistributor_id("your data");
now get anywhere(Fragment, activity, class)
String _data = DataHolderClass.getInstance().getDistributor_id();
You can use EventBus https://github.com/greenrobot/EventBus to pass any complex object around in Android.
Example passing an object from one Activity to another:
Add your object on the EventBus:
EventBus.getDefault().postSticky(anyObject);
startActivity(new Intent(getActivity(), SomeActivity.class));
Anywhere in SomeActivity:
AnyObject anyObject = EventBus.getDefault().getStickyEvent(AnyObject.class);
This examle is just to show that it works across Activities, you can call getStickyEvent to get anyObject anywhere in your code.
So, you can post an object in Fragment A, and use getStickyEvent in fragment B to retrieve it.
This is especially convenient if you have a lot of arguments and/or complex objects you wish to move from one place to the other. Simply create a single 'holder' object containing getter/setters for the arguments and then pass it along. Much simpler than having to pass them separately and somehow serialize the complex objects.
I think you are trying to pass data from one fragment to another fragment, So try using below code.
Use a Bundle, So write below code in first fragment from where you want to send data.
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("message", "From Activity");
fragment.setArguments(bundle);
and to retrieve that data, use the following code in your other fragment
String strtext=getArguments().getString("message");
I'm trying to change Roman's Nurik WizardPager so that in one of the steps display some data from my database.
I'm taking a Model and an UI from the library and I modify them so I can have a
DisplayOrderPage with a parameter ArrayList for my data
public DisplayOrderPage(ModelCallbacks callbacks, ArrayList<Salads> ord , String title) {
super(callbacks, ord, title);
}
and a DisplayOrderFragment which is going to display the data.
I can get an ArrayList with my data from my database in the MainActivity but I don't know how to pass that data to the SandwichWizardModel since it's not an Activity.
you can do one thing.. also pass the context of the activity as parameter. And declare a method in the activity which change the gui within a handler .. like this
in Activity
changeGUI(){
new Handler().post(new Runnable(){
// change gui
}
}
and call like
methodInCLasse(this, arrayList);