Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

android - how to pass string data from activity to fragment?

I want to pass string value from activity to fragment. For that I'm using bundle for transferring string value.

PUT STRING ACTIVITY Passing string value :-

           Bundle bundle = new Bundle();
            bundle.putString("Value", resultp.get(CurrentProjectActivity.VALUE));
            Log.d(TAG, "Value  ::: " + resultp.get(CurrentProjectActivity.VALUE));
            // set Fragmentclass Arguments
            AmenetiesFragment fragobj = new AmenetiesFragment();
            fragobj.setArguments(bundle);

In log I got "Value" value as well.

GET STRING VALUE IN FRAGMENT (IT IS NOT WORKING).

     @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_listview, container, false);
    Bundle bundle = this.getArguments();
    Log.d(TAG, "Value's value:)  ::: " + bundle);
    String strtext = bundle.getString("Value");
    return rootView;
}

In log I'm getting NULL value for BUNDLE. Please help me to resolve this Issue. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I advice you to follow this link1 and this . You should use interface. Check here

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

and in the Frament task

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("edttext");    
    return inflater.inflate(R.layout.fragment, container, false);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...