在我的Android应用程序中,我需要生成一个带有公司名称列表的Alertdialog。例如,公司1,公司2,公司3.现在,如果用户点击公司1,他将获得第二个alertdialog,它将显示一些操作。像电话,电子邮件等。现在我已经在我的代码中实现了这两个alertdialog。但我想做的是,每家公司应该有不同的电话号码和电子邮件地址。到目前为止,我已经尝试过与所有公司相同的号码。但实际上如果用户点击公司1,他将通过电话,电子邮件获得第二个行动警报列表。如果他点击电话选项他将看到电话号码公司1,如果他点击公司2,他将获得与公司2的电话号码的警报选项。但我在开发区域是非常新的。我知道有一些映射主题,我可以轻松地做到这一点,但我实际上并没有得到如何继续它。我的代码就像这样
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>
答案 0 :(得分:0)
您可以传递该项目的位置并在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();
}
}
});
}
答案 1 :(得分:0)
深入了解这种情景背后的逻辑使用*HashMap*
的最佳方式。
根据你的情况。我改变你的代码检查出来:
public class CompanyDetail{
String companyPhoneNo;
String companyEmail;
public void setcompanyPhoneNo(String phoneNo){
this.companyPhoneNo = phoneNo;
}
public void setcompanyEmail(String Email){
this.companyEmail = Email;
}
public String getcompanyPhoneNo(){
return companyPhoneNo;
}
public String getcompanyEmail(){
return companyEmail;
}
}
public HashMap<String, List<CompanyDetail>> setCompanydata(){
int n = 3;
private List<CompanyDetail> companyDetailList = new ArrayList<CompanyDetail>();
private HashMap<String, List<CompanyDetail>> companyContactDetail = new HashMap<String, List<CompanyDetail>>();
for(int i=0;i<n;i++){
CompanyDetail comD= new CompanyDetail();
comD.setcompanyPhoneNo(companyPhoneno);
comD.setcompanyEmail(compnayEmailId);
companyDetailList.add(comD);
companyContactDetail.add(companyname, companyDetailList);
}
return companyContactDetail;
}
private List<CompanyDetail> companyDetailList;
private HashMap<String, List<CompanyDetail>> companyContactDetail;
companyContactDetail = setCompanydata();
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();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
}
if (itemPosition == 1) {
alert.dismiss();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
showSecondDialogwithList();
}
if (itemPosition == 2) {
alert.dismiss();
companyDetailList1 = companyContactDetail.get(companyNameatPosition);
showSecondDialogwithList(Companyname,companyDetailList1 );
showSecondDialogwithList();
}
}
});
}
private void showSecondDialogwithList( String companyName, List<CompanyDetail> companyDetail) {
CompanyDetail obj = companyDetail.get(0);
private String companyPhone = obj.getcompanyPhoneNo();
private String companyEmail = obj.getcompanyEmail();
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(companyPhone);
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();
}
}
});
}
答案 2 :(得分:0)
逐个显示两个对话框并不是一个好习惯。当然你可以实现它,如上所述,但是用公司的列表(ListView,RecyclerView)显示分离的活动(片段)会更好,点击每个项目将显示带有所需参数的对话框。从设计方面和用户体验看它会更好。