Search code examples
androidandroid-studiodialogandroid-alertdialogandroid-dialogfragment

How to get position of Item in Dialog fragment Android


I have two Alertdialog fragment in my app. If user click on the first alert dialog he will get the second alert dialog where he find some action list. Now the first alertdialog is working correctly so far. But in second alert dialog I want to set position by which User can get the correct information based on click. suppose if user click company 1, he will get phone and email of company 2, in the same way if he clicks on second company, dialog will provide the information of second company. But I am not getting the logic how can I implement it. Here is my code for first and second Alert Dialog. Also is it a good way to write phone and email. or is there any other way to implement it.

I have solved the problem with the help of answer code. here is my solved code for the question

Solved Code

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

Second Alert Dialog

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

Solution

  • You can use Bundle to send data between fragments. See link

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

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