简体   繁体   中英

get data from DialogFragment to the Fragment

I have a Fragment in a Viewpager. I am opening a DialogFragment from it which is taking the data from a ParseQuery. So far so good. Toast the clicked item also works. I am trying to setText for a TextView in the Fragment that the dialog is called from without success... Anything i try just doesn't work - Intent, Bundle nothing... Going crazy with it already :) I am not familiar with Interface and Callbacks, if that is needed. I don't know... Thanks a lot in advance for any answer!! Want to mention that i am not an advanced programmer, so any detailed explanation will be blessed :)

Here is the DialogFragment code - what i am trying to take from it and set to the TextView is pType.get(position).toString()

 @Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    ParseQuery<PropertyTypes> query = ParseQuery.getQuery(PropertyTypes.class);
    query.whereExists("propertyType");
    query.findInBackground(new FindCallback<PropertyTypes>() {

        @Override
        public void done(final List<PropertyTypes> pObjects, ParseException e) {

            if (e == null) {

                final ArrayList<String> pType = new ArrayList<String>();

                for (PropertyTypes j : pObjects) {

                    pType.add(j.getTypeName());

                }

                ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, pType);
                mylist.setAdapter(arrayAdapter);
                mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        getDialog().dismiss();

                        Toast.makeText(getActivity(), pType.get(position).toString() + " was clicked", Toast.LENGTH_LONG).show();

                        // Intent...?
                        // Bundle...?
                    }
                });

            } else {
                e.printStackTrace();
            }

        }

    });

}

You can make use of setTargetFragment() method for this. For example, use setTargetFragment() while calling your Dialog Fragment

DialogFragment dialogFragment = new DialogFragment();
dialogFragment.setTargetFragment(this, REQUEST_CODE);
dialogFragment.show();

and on your onItemClick() inside DialogFragment do this

mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        dismiss();
                        Toast.makeText(getActivity(), pType.get(position).toString() + " was clicked", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent();
                        intent.putStringExtra("yourKey", yourStringValue);
                        getTargetFragment().onActivityResult(
                        getTargetRequestCode(), Activity.RESULT_OK, intent);
                    }
                });

and you can get that string value in your onActivityResult() of your calling/target Fragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case REQUEST_CODE:
                if (resultCode == Activity.RESULT_OK) {
                   if(data!=null){
                        // set value to your TextView
                        textView.setText(data.getStringExtra("yourKey"));
                    }
                } 
                break;
        }
    }
}

Hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM