2

In my android app, I need to generate one Alertdialog with a list of Company name. For example Company 1, Company 2, Company 3. Now if the user cllick company 1, he will get second alertdialog which will show some actions. Like Phone Call, Email, etc. Now I have implemented this two alertdialog in my code. But what I want to do, that for each company there should be different Phone number and email adress. So Far I have tried with same number with all the company. But in real in real if user click company 1, he will get the second alert list of action with phone call, email. if he clicks phone option he will see the phone number company 1, if he clicks company 2, he will get alertoption with phone number of company 2. But I am very new in developing area. I know there is something with Mapping topic, by which I can do it easily but I am not getting actually how to proceed with it. My code is like this

    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();

            }
        }
    });

}

My string arrays are

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

3 Answers3

0

you can pass the position for the item and test it at your function or you can pass it directly :

@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();

            }
        }
    });

}
Oussema Aroua
  • 5,225
  • 1
  • 24
  • 44
  • How can I set Title here builder.setTitle("+1234667"); according to the position of phoneNumber –  Jul 11 '17 at 14:35
0

To go deep in to logic behind that kind of scenario best way to use *HashMap*. according to your scenario. i change in your code check it out:

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();

            }
        }
    });

}
singh.indolia
  • 1,292
  • 13
  • 26
  • What is CompanyDeatil here. could you please explain. did you use any class here. –  Jul 11 '17 at 10:21
  • CompanyDetails are basically phone no, email id and others. – singh.indolia Jul 11 '17 at 10:22
  • like: c1 -> { phone no, emailId, etc.}, c2 -> { phone no, emailId, etc.} and so on – singh.indolia Jul 11 '17 at 10:23
  • could you plaese please give my one example of that. –  Jul 11 '17 at 10:24
  • Sure, i can give u an example of it just to modify your code – singh.indolia Jul 11 '17 at 10:26
  • first thing you tell me are you getting data from json file or not. – singh.indolia Jul 11 '17 at 10:28
  • initially I am trying to implement this code only with some sample data as a string. in my values-> string I have created an array with the company name like , then another array of contact way , then another array of phone number , another array of email. because it is too much to handling the json data for me –  Jul 11 '17 at 10:33
  • thank you very much for your answer. But if you dont mind could plaese elaborate your code a bit more. I really like the way you have done. But it would be really helpful fo me if you write some more in your code –  Jul 11 '17 at 10:35
  • sorry give me some time i m busy little bit but i will do it. – singh.indolia Jul 11 '17 at 10:40
  • Hii Ktina . I have edited my answer so please check it out full code. and use it accordingly. If in the case have you any problem ask me without hesitation. – singh.indolia Jul 11 '17 at 11:57
  • Can you please explain what have you done in this section public List setCompanydata(){. thi ssection is not properly organised I guess. Shall i implement is in company detail class –  Jul 11 '17 at 12:10
  • CompanyDetail class is the model class and it will be separate. just try it in this way first. – singh.indolia Jul 11 '17 at 12:16
  • to understand about model class and to set data in model class just read about it then you will get to know. – singh.indolia Jul 11 '17 at 12:17
  • I am cofused about public List setCompanydata(). i am not sure how to set data here comD.setcompanyPhoneNo(companyPhoneno); comD.setcompanyEmail(compnayEmailId); companyDetailList.add(comD); companyContactDetail.add(companyname, companyDetailList); –  Jul 11 '17 at 12:22
  • okay. except CompanyDetail class put your whole code in the same class. Do not be confuse. – singh.indolia Jul 11 '17 at 12:26
  • I have modified my question with code and comments. Could you plaese have alook –  Jul 11 '17 at 12:28
  • I have edit my code check my ans. and //cannot resolve problem companyPhon is company phone as you want to put. – singh.indolia Jul 11 '17 at 12:33
  • I gave my string arrays. I am trying, but do not know why it is showing that modifier private is not alloed here. also how can I get phone number from array to companyPhoneno here. –  Jul 11 '17 at 12:41
  • So just remove private from there and try. you will solve this. just try. – singh.indolia Jul 11 '17 at 12:43
  • right now I am going to home so leave your queries in comment i will reply tomorrow. – singh.indolia Jul 11 '17 at 12:44
  • ya I have done that. but for add it is showingcanoot resolve method add –  Jul 11 '17 at 12:45
  • so change add method accordingly like put. – singh.indolia Jul 11 '17 at 12:47
  • thnak you. One last question I have I have modified it in this way. shall it work comD.setcompanyPhoneNo(""); comD.setcompanyEmail(""); how can Iget data from array here –  Jul 11 '17 at 12:49
  • could you plaese tell me what is the problem here companyContactDetail = setCompanydata(); it is showing invalid method declaration –  Jul 11 '17 at 13:01
  • jsut change from : private HashMap> companyContactDetail = new HashMap>(); to private HashMap> companyContactDetail; – singh.indolia Jul 12 '17 at 05:16
0

It's not a good practice to display two dialogs one by one. Of course you can implement it, like described above, but it will be much better to display separated activity(fragment) with the list(ListView, RecyclerView) of companies and click on each item will show the dialog with needed params. It will look better from design side and users experience.

Dmitriy Mitiai
  • 1,112
  • 12
  • 15
  • Cpuld please explain a bit more how can I do that. I have tried previously with dialog fragments. But could not succeed to implement the dialog.It might me not related to the topic. but I have heared that generic class of dialog is one of the way to implement the Alerdiaolg. It would be nice if y ou explain it more –  Jul 12 '17 at 07:04
  • Simplest case is to use ListView(for start).Create a separated activity and in layout of the activity just put – Dmitriy Mitiai Jul 12 '17 at 07:39
  • That means I have to start a seperate activity for Dialog and then I will write firstAlerdialog and second alertDialog inside that –  Jul 12 '17 at 07:42
  • Could you please give me one example –  Jul 12 '17 at 07:43
  • No. You'll have only one dialog. Simplest case is to use ListView(for start). So, create a separated activity and in layout of the activity just put the ListView. You can see [this](https://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html) example or read [here](https://developer.android.com/reference/android/widget/ListView.html) from official doc. Then add to your ListView setOnItemClickListener(). And in onclick display you dialog. – Dmitriy Mitiai Jul 12 '17 at 07:49
  • [Here](https://stackoverflow.com/a/4752309/4730040) is good example, how to add OnItemClickListener() – Dmitriy Mitiai Jul 12 '17 at 07:53
  • then are sou suggest me to avoid alertdialog. because as far as I understand if I implement listview I will have skip Alertdialog. or may be I am wrong. Because What I need in my case that I need to generate One alert with List and then if I click one item I will redirect to second List –  Jul 12 '17 at 08:22
  • You will have only one dialog - with company parameters. The list of the companies will not be in dialog, but it will be in separated screen. the logic is next: for example, you have some button and when you'll click on it, you'll open new activity(not the dialog) with list of companies.And when you'll select any company from the list, you'll open the dialog with needed parameters. Clear? – Dmitriy Mitiai Jul 12 '17 at 10:31
  • Ok I am trying to do this now. If I have more question, I will ask you agin. and thank you for your direction :) –  Jul 12 '17 at 10:33
  • I have tried the way you are telling. but the problem is how can I pass all the listView data to this single Alertdialog. I have created another post https://stackoverflow.com/questions/45056335/how-can-i-link-a-listview-to-an-alertdialog/45056389?noredirect=1#comment77086389_45056389 –  Jul 12 '17 at 11:46