从DialogFragment获取数据到片段

时间:2016-01-25 19:37:35

标签: android android-fragments android-studio

我在Viewpager中有一个片段。 我正在打开一个DialogFragment,它从ParseQuery获取数据。到现在为止还挺好。烤制点击的项目也有效。 我试图在片段中为一个TextView设置文本,从而调用对话框没有成功...我尝试的任何东西都没有工作 - 意图,没有任何东西......已经疯狂了:) 如果需要,我不熟悉接口和回调。我不知道...... 非常感谢您提前回答! 想提一下,我不是一名高级程序员,所以任何详细的解释都会得到祝福:)

这是DialogFragment代码 - 我想从中获取并设置为TextView的是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();
            }

        }

    });

}

1 个答案:

答案 0 :(得分:1)

您可以使用setTargetFragment()方法。例如,在调用Dialog Fragment

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

并在您的onItemClick()内部DialogFragment执行此操作

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);
                    }
                });

您可以在调用/目标片段的onActivityResult()中获取该字符串值

@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;
        }
    }
}

希望这会有所帮助。