在我的Android应用中,我需要生成一个Alertdialog,其中包含公司名称列表。例如,公司1,公司2,公司3。现在,如果用户单击公司1,他将获得第二个Alertdialog,该对话框将显示一些操作。像电话,电子邮件等。现在,我在代码中实现了这两个Alertdialog。但是我想做的是,对于每个公司,应该有不同的电话号码和电子邮件地址。到目前为止,我已经与所有公司尝试使用相同的号码。但是实际上,如果用户单击公司1,他将通过电话,电子邮件获得第二个警报列表。如果他单击电话选项,他将看到电话号码公司1,如果他单击公司2,他将得到带有公司2电话号码的alertoption。但是我在开发领域非常陌生。我知道“映射”主题中有一些内容,通过它可以轻松实现,但实际上并没有获得如何继续进行的知识。我的代码是这样的

    public List<CompanyDetail> setCompanydata(){
    int n = 3;
    private List<CompanyDetail> companyDetailList = new ArrayList<CompanyDetail>(); //modifier private is not allowed here
    private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>(); //modifier private is not allowed here
    for(int i=0;i<n;i++){
        private CompanyDetail  comD= new CompanyDetail(); //modifier private is not allowed here
        comD.setcompanyPhoneNo(companyPhoneno); //cannot resolve problem companyPhone
        comD.setcompanyEmail(compnayEmailId);
        companyDetailList.add(comD);
        companyContactDetail.add(companyname, companyDetailList);//cannot resolve method 'add(?,java util list..
    }

    return companyContactDetail; //incompatible type

}
private List<CompanyDetail> companyDetailList;
private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>();
companyContactDetail =  setCompanydata(); //unknown class company contact deatil

private void showFirstDialogwithList() {

    //Create a new builder and get the layout.
    final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
    LayoutInflater inflater = this.getActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
    builder.setView(dialogView);
    builder.setCancelable(true);
    //Show the dislog
    final AlertDialog alert = builder.show();
    //Get the TextView, ListView, Button from the layout.
    TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
    Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
    ListView alertListView = (ListView) dialogView.findViewById(listView1);

    alertTitle.setText("Contact");
    // Defined Array values to show in ListView
    String[] values = getResources().getStringArray(R.array.company_name);


    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this.getActivity(), R.layout.first_alertlist_textstyle, android.R.id.text1, values);
    alertListView.setAdapter(arrayAdapter);

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

    alertListView.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) {
                alert.dismiss();
                showSecondDialogwithList();
            }

            if (itemPosition == 1) {
                alert.dismiss();
                showSecondDialogwithList();
            }
            if (itemPosition == 2) {
                alert.dismiss();
                showSecondDialogwithList();
            }

        }

    });
}

private void showSecondDialogwithList() {

    final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
    LayoutInflater inflater = this.getActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
    builder.setView(dialogView);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);

    //Show the dislog
    final AlertDialog alert = builder.show();

    //Get the TextView, ListView, Button from the layout.
    TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
    Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
    final ListView alertListView = (ListView) dialogView.findViewById(listView1);

    alertTitle.setText("What do you want to do");
    // Defined Array values to show in ListView
    String[] values = getResources().getStringArray(R.array.contact_way);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
            R.layout.first_alertlist_textstyle, android.R.id.text1, values);
    alertListView.setAdapter(adapter);

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

    alertListView.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) {

                alert.dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                builder.setTitle("+1234667");
                builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // close the dialog, go to login page
                        if(isPermissionGranted()){
                            call_action();
                        }
                    }
                });
                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 (itemPosition == 1) {
                alert.dismiss();
                ......;

            }
            if (itemPosition == 2) {
                dismiss();
                showEmail();

            }
            if (itemPosition == 3) {
                dismiss();

            }
        }
    });

}


我的字符串数组是

    <string-array name="company_name">
    <item>company 1</item>
    <item>Company 2</item>
    <item>Company 3</item>
</string-array>

<!-- AlertDialog way of Contact array -->
<string-array name="contact_way">
    <item>Phone Call</item>
    <item>Email</item>
</string-array>

<String-array name="phone">
    <item>123456</item>
    <item>125658</item>
    <item>123451</item>
</String-array>

<String-array name="email">
    <item>email1</item>
    <item>email2</item>
    <item>email2</item>
</String-array>

最佳答案

您可以传递商品的位置并在function上对其进行测试,也可以直接传递:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        alert.dismiss();
        showSecondDialogwithList(position);
    }

private void showSecondDialogwithList(int position) {
    String phoneNumber;
    switch (position) {
        case 1:
            phoneNumber = "123";
            break;
        case 2:
            phoneNumber = "456";
            break;
        case 3:
            phoneNumber = "789"
    }
    final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
    LayoutInflater inflater = this.getActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.first_alertlist_contact, null);
    builder.setView(dialogView);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
    //Show the dislog
    final AlertDialog alert = builder.show();
    //Get the TextView, ListView, Button from the layout.
    TextView alertTitle = (TextView) dialogView.findViewById(R.id.title);
    Button alertButton = (Button) dialogView.findViewById(R.id.cancel_button);
    final ListView alertListView = (ListView) dialogView.findViewById(listView1);

    alertTitle.setText("What do you want to do");
    // Defined Array values to show in ListView
    String[] values = getResources().getStringArray(R.array.contact_way);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
            R.layout.first_alertlist_textstyle, android.R.id.text1, values);
    alertListView.setAdapter(adapter);

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

    alertListView.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) {
                alert.dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("+1234667");
                builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // close the dialog, go to login page
                        if (isPermissionGranted()) {
                            call_action();
                        }
                    }
                });
                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 (itemPosition == 1) {
                alert.dismiss();
            ......;

            }
            if (itemPosition == 2) {
                dismiss();
                showEmail();

            }
            if (itemPosition == 3) {
                dismiss();

            }
        }
    });

}

07-26 02:14