Skip to content Skip to sidebar Skip to footer

How To Set Value For Textview Of Fragment Received From Another Fragment?

I don't know how to set in the textview of second fragment when received the value from another fragment. The data I received in the second fragment is the 'value' object of String

Solution 1:

You can send arraylist of any custom class data using Parcelable from one activity/fragment to next activity/fragment.

In previous fragment add data in bundle using putParcelableArrayList method. for e.g.

bundle.putParcelableArrayList("KEY_NAME",arrayName);

In next frgament get that data in onCreate using getParcelableArrayList method.

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        ArrayList<Item> itemArray = getArguments().getParcelableArrayList("KEY_NAME");
    }
}

And main thing is that change your StringList class to Parcelable. For Parcelable class example click below link..

Parcelable Class

Solution 2:

Please consider using newInstance pattern for passing values to fragment as calling receiveValue method after instantiating fragment is not a good practice. You should define your object as Parcelable and pass it to new fragment via newInstance method.

However if you received values via invocation of method receiveValue there is no need to use a Bundle. Bundle is used when you want to pass values from Fragment or Activity to another Fragment. For saving values you can define Global Variables like you did with TextViews. For Example:

Bundle bundle;
StringList mStringList;
private TextView headlineSecond;
private TextView authorSecond;
private TextView detailsSecond;
private List<StringList> s;

publicBusinessDetail() {
}

publicvoidreceiveValue(StringList value, int positionValue) {
    mStringList = value;
}

P.S: But for future reference, you can retrieve String value from Bundle by calling getString(String key). In your case it would be like:

Stringvalue= bundle.getString("news");
yourTextView.setText(value);

Also remove these lines, they don't do anything in your current fragment unless you trying to start a new Fragment:

BusinessDetaildetail=newBusinessDetail();
detail.setArguments(bundle);

For detailed explanation of fragment's communication with example and code snippet you can visit here: http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html

Post a Comment for "How To Set Value For Textview Of Fragment Received From Another Fragment?"