How to popup list like a spinner without spinner in android?

ε祈祈猫儿з 提交于 2019-12-20 14:23:32

问题


I have a spinner widget in my activity which lets users pick a list name.

Normally, the function of the spinner is to switch between lists but for a couple of instances, I swap out the selection change listener to perform a different function with the same list of options. Once the selection has been made, the old listener is restored and life goes on.

This is a bad and buggy arrangement. Instead, I would like to have a function that just takes a selection listener and some other parameters and shows a popup list that's populated by the same cursor (or and identical cursor) as the spinner, without using the spinner itself.

Is there any way I can do this?


回答1:


Use AlertDialog.Builder and supply an Adapter via setAdapter() that generates your rows.

In your case, I would not use the same Cursor, as a Cursor has an intrinsic notion of the current row, and so messing with the Cursor while it is used by your SpinnerAdapter could screw up the Spinner. Go with an identical Cursor.




回答2:


This is best example for popup details like spinner using AlertDialog and AlertDialog.Builder

        AlertDialog dialog;

         final CharSequence[] items = { "Item1", "Item2" };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int pos) {
            switch (pos) {
                case 0:
                              {
        Toast.makeText(this,"Clicked on:"+items[pos],Toast.LENGTH_SHORT).show();

                      }break;
            case 1:
                              {
        Toast.makeText(this,"Clicked on:"+items[pos],Toast.LENGTH_SHORT).show();

                      }break;
        }
    }});
dialog=builder.create();
dialog.show();



回答3:


If you are not limited by API level 11 then listPopupWindow is close to what you want.




回答4:


            CharSequence[] items = {"Mangoes", "Bananas", "Grapes"};

            new AlertDialog.Builder(getActivity())
            .setTitle("Action")
            .setItems(items, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {

                    if(item==0){
                      // Mangoes selected
                    }
                    else if(item==1){
                      // Bananas selected
                    }
                    else if(item==2){
                      // Grapes selected
                    }   
                }

            })
            .show();



回答5:


You might want to use PopupMenu

see this example



来源:https://stackoverflow.com/questions/2129624/how-to-popup-list-like-a-spinner-without-spinner-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!