0

I am new to android. May I know how to make a listview (shows A,B,C) in the dialog and when people choose an item like item A (e.g. item A contains 1,2,3 objects), the dialog will show 1,2,3. Beside calling dialog 3times, is there any better way to do so?

FrankieWong
  • 53
  • 1
  • 7

2 Answers2

0

since you want to preserve state you should create a new dialog ontop of the A,B,C one that also has a list and similar UI. Ideally stay away from list based dialogs, but if you have to, two dialogs is best since you can hit back.

https://material.io/guidelines/components/dialogs.html#dialogs-simple-dialogs

L7ColWinters
  • 1,342
  • 1
  • 14
  • 31
0

Yes as L7ColWinters said, you can use two different dialogs to show the list. You can use single dialog to show as well but it will be little bit complex.

So using two dialogs you can do as follows.

First create your main list and sub lists,

List<String> list = new ArrayList<>();
List<String> a = new ArrayList<>();
List<String> b = new ArrayList<>();
List<List<String>> selectedList = new ArrayList<>();

You have to add all your lists except main list into selectedList.

selectedList.add(a);
selectedList.add(b);

Then use your button click to display dialog with listView as below

Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            final Dialog dialog = new Dialog(MainActivity.this);
            dialog.setContentView(R.layout.custom_dialog);

            ListView listView = dialog.findViewById(R.id.listView);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    List<String> newList = new ArrayList<>();
                    newList.addAll(selectedList.get(i));
                    Dialog dialog1 = new Dialog(MainActivity.this);
                    dialog1.setContentView(R.layout.custom_dialog);
                    ListView listView = dialog1.findViewById(R.id.listView);
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, newList);
                    listView.setAdapter(adapter);
                    dialog1.show();
                }
            });


            dialog.show();

        }
    });

so onClick of "A", you will get list of a and on "B", you will get list of b in second dialog.

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView"/>

</RelativeLayout>

you can use same layout for both adapters.

Sagar A
  • 89
  • 7
  • Thank you so much dude...I will try this. I don't know if this is the usual way that programmer will do. My case is that when the list item has child, people can keep clicking and go to the child list and so on . I think I am going to have a better design like a specific class to do this job. – FrankieWong Mar 14 '18 at 03:35