为Alert Dialog Android

时间:2017-08-02 19:35:47

标签: java android listview model-view-controller android-dialogfragment

在我的Android App中,我创建了两个对话框片段类。第一个对话框片段提供comapany名称列表。该对话框将出现在主页面上的按钮上。此警报对话框有一个标题,listView和一个取消按钮,如下所示 -

 Company
Company 1
Company 2
Company 3
 Cancel

现在,只要用户点击第一个对话框的每个项目,第二个对话框就会出现。第二个diailog包含标题"联系方式",列表视图和取消按钮。

  Way of Contact
    Phone
    Email
    Copy

现在第二次点击与第一次点击之间存在关系。如果用户点击公司1,他将获得公司1的电话号码,如果他点击公司2,他将获得公司2的电话号码。类似的情况将发生在电子邮件ID上。

到目前为止,我的代码太硬编码了。无论如何我已经实现了这个代码。但我不满意解决这个问题的方法。现在我想以面向对象的方式继续前进。我已经读过Enum for company在这里可以保持comapny名称不变,然后用手机和电子邮件创建Mapping。但我在开发领域非常新。如果有人告诉我我该怎么办,那对我来说真的很有帮助。

这是我的FirstDialog类的代码

编辑第一个对话框片段

    public class FirstAlerDialogFragment extends DialogFragment {

    private ListView listView;
    private Button cancelButton;
    private TextView title;
    private ArrayAdapter<CompanyName> 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");

        //Create and set Adepter TO ListView1
        adapter=new ArrayAdapter<>(this.getActivity(),R.layout.first_alertlist_textstyle,R.id.Itemname,CompanyName.values());
        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;

                dismiss();
                FragmentManager fm = getFragmentManager();
                Bundle args = new Bundle();
                args.putInt("position", itemPosition);
                SecondAlertDialogFragment dialogFragment = new SecondAlertDialogFragment ();
                dialogFragment.show(fm, "dialogFragment");
                dialogFragment.setArguments(args);
                dialogFragment.show(getChildFragmentManager(), null);

            }
        });
        return rootView;
    }
}

编辑第二个对话

public class ContactWayFragment extends DialogFragment{

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

public ContactWayFragment() {
  //Empty Constructor
}

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

private Company[] companies = new Company[] {
        new Company("123", "mail1.com"),
        new Company("456", "mail2.com"),
        new Company("789", "mail3.com"),
        new Company("102", "mail4.com"),
        new Company("458", "mail5.com"),
        new Company("977", "mail6.com")
};
@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=rootView.findViewById(R.id.listView);
    cancelButton=rootView.findViewById(R.id.cancel_button);
    title=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<>(getActivity(), R.layout.first_alertlist_textstyle,android.R.id.text1,contactWay);
    listView.setAdapter(adapter);
    //cancel button to cancel this alert dialog
    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();
            int itemPosition = args.getInt("position");
            Company company = companies[itemPosition];

            if (position == 0) {
                //dismiss();
                //Phone Call Alert
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Calling to " + company.getPhoneNumber());
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    //make a phone call
                        dialog.dismiss();
                        Log.e("getActivity()-Call",""+getActivity());
                        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 (position == 1) {
                dismiss();

                //Email Alert Dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Email to " + company.getEmail());
                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 (position == 2) {
                dismiss();
                Toast.makeText(getActivity(), "click 2", Toast.LENGTH_SHORT).show();
            }
            if (position == 3) {
                dismiss();
                Toast.makeText(getActivity(), "click 3", Toast.LENGTH_SHORT).show();
            }
        }
    });
    return rootView;
}
public void call_action() {
...
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
    ....
      }
   }
}

已编辑的代码

在答案代码的帮助下,我更改了代码示例

 public enum CompanyName {
    COM1("FirstCompany"),
    COM2("SecondCompany"),
    COM3("ThirdCompany");

    private final String name;

    private CompanyName(String name){
        this.name=name;
    }
    public String getName(){
        return this.name;
    }

    @Override
    public String toString() {
        return name;
    }
}

公司模型类是

    public class Company {

    private CompanyName name;
    private String phone;
    private String email;

    public Company(String phone, String email) {

        this.phone = phone;
        this.email = email;

    }

    public CompanyName getName() {
        return name;
    }

    public void setName(CompanyName name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

1 个答案:

答案 0 :(得分:0)

确实,那个开关/案例看起来有点硬编码。您可以做的是使用一组对象,其中包含有关每家公司的信息。

这将是您的公司模型对象:

public class Company {
    String phoneNumber, email;

    public  Company(String phoneNumber, String email) {
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getEmail() {
        return email;
    }
}

这将是数组声明:

private Company[] companies = new Company[] {
    new Company("38888180", "email1.com"),
    new Company("411660", "email2.com"),
    new Company("895402260", "email3.com")
};

最后,要访问电子邮件或电话号码,您只需执行此操作:

    ...
    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");
            Company company = companies[itemPosition];

    ...
        builder.setTitle("Calling to " + company.getPhoneNumber());
    ...
        builder.setTitle("Calling to " + company.getEmail());