如何在Dialog片段Android中获取Item的位置

时间:2017-07-26 14:06:49

标签: android android-studio dialog android-alertdialog android-dialogfragment

我的应用中有两个Alertdialog片段。如果用户单击第一个警报对话框,他将获得第二个警报对话框,在那里他找到一些操作列表。现在第一个alertdialog到目前为止工作正常。但是在第二个警告对话框中,我想设置用户可以根据点击获取正确信息的位置。假如用户点击公司1,他将获得公司2的电话和电子邮件,如果他点击第二家公司,对话框将提供第二家公司的信息。但我没有得到逻辑我怎么能实现它。这是我的第一个和第二个警报对话框的代码。也是写电话和电子邮件的好方法。还是有其他方法来实现它。

我已经在答案代码的帮助下解决了这个问题。这是我解决的问题代码

已解决的代码

public class FirstAlerDialogFragment extends DialogFragment {

    private ListView listView;
    private Button cancelButton;
    private String[] companyName;
    private TextView title;
    private ArrayAdapter<String> adapter;

    public FirstAlerDialogFragment() {

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
    }

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

        View rootView = inflater.inflate(R.layout.first_alertlist_contact, container, false);

        //Button,ListView1 Initialization
        listView=(ListView) rootView.findViewById(listView1);
        cancelButton=(Button) rootView.findViewById(R.id.cancel_button);
        title=(TextView)rootView.findViewById(R.id.title);
        title.setText("Contact");
        // Defined Array values to show in ListView
        companyName = getResources().getStringArray(R.array.company_name);

        //Create and set Adepter TO ListView1
        adapter=new ArrayAdapter<String>(getActivity(), R.layout.first_alertlist_textstyle,android.R.id.text1,companyName);
        listView.setAdapter(adapter);

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // ListView Clicked item index
                int itemPosition = position;

                if (itemPosition == 0) {
                    dismiss();

            FragmentManager fm = getFragmentManager();
            Bundle args = new Bundle();
            args.putInt("position", itemPosition);
            ContactWayFragment dialogFragment = new ContactWayFragment ();
            dialogFragment.show(fm, "dialogFragment");
            dialogFragment.setArguments(args);
            dialogFragment.show(getChildFragmentManager(), null); }
            }
        });
        return rootView;
    }
}

第二次警报对话

public class SecondAlertDialogFragment extends DialogFragment {

    private ListView listView;
    private Button cancelButton;
    private String[] contactWay;
    private TextView title;
    private ArrayAdapter<String> adapter;
    private int position;
    private String phoneNumber, email;

    public SecondAlertDialogFragment() {

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

        View rootView = inflater.inflate(R.layout.first_alertlist_contact, container, false);

        //Button,ListView1 Initialization
        listView=(ListView) rootView.findViewById(listView1);
        cancelButton=(Button) rootView.findViewById(R.id.cancel_button);
        title=(TextView)rootView.findViewById(R.id.title);
        title.setText("What do you want to do");
        // Defined Array values to show in ListView
        contactWay = getResources().getStringArray(R.array.contact_way);

        //Create and set Adepter TO ListView1
        adapter=new ArrayAdapter<String>(getActivity(), R.layout.first_alertlist_textstyle,android.R.id.text1,contactWay);
        listView.setAdapter(adapter);

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // ListView Clicked item index

                Bundle args = getArguments();
                args = getArguments();
                int itemPosition = args.getInt("position");
                switch (itemPosition) {
                    case 0:
                        phoneNumber = "12345";
                        email="mail.com";
                        break;
                    case 1:
                        phoneNumber = "1246";
                        email="mail2.com";
                        break;
                    case 2:
                        phoneNumber = "8780";
                        email="mail3.com";
                        break;
                    case 3:
                        phoneNumber = "123456";
                        email="mail4.com";
                        break;
                }
                int item = position;

                if (item == 0) {
                    dismiss();
                    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                    builder.setTitle("Email to " + email);

                    builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing
                            dialog.dismiss();
                        }
                    });
                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing
                            dialog.dismiss();
                        }
                    });

                    AlertDialog alert = builder.create();
                    alert.show();
                }
                if (item == 1) {
                    dismiss();
                    Toast.makeText(getActivity(), "click 1", Toast.LENGTH_SHORT).show();
                }
                if (item == 2) {
                    dismiss();
                    Toast.makeText(getActivity(), "click 2", Toast.LENGTH_SHORT).show();
                }
                if (item == 3) {
                    dismiss();
                }

            }
        });
        return rootView;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用 Bundle 在片段之间发送数据。见link

FirstAlerDialogFragment

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int itemPosition = position;
        Bundle args = new Bundle();
        args.putInt("position", itemPosition);
        SecondAlertDialogFragment fragment = new SecondAlertDialogFragment;
        fragment.setArguments(args);
        fragment.show(getChildFragmentManager(), null);
     }

SecondAlertDialogFragment

Bundle args = getArguments();
int position = args.getInt("position");
switch (position) {
            case 0:
                phoneNumber = "12345";
                email="mail.com";
                break;
            case 1:
                phoneNumber = "1246";
                email="mail2.com";
                break;
            case 2:
                phoneNumber = "8780";
                email="mail3.com";
                break;
            case 3:
                phoneNumber = "123456";
                email="mail4.com";
                break;
            }