How to popup like spinner without spinner in android?

I have a spinner widget in my activity that allows users to select a list name.

Usually the function of a counter is to switch between lists, but for several instances I replace the selection change listener in order to execute another function with the same list of parameters. After the choice is made, the old listener is restored and life continues.

This is a bad and bad location. Instead, I would like to have a function that simply accepts the selection listener and some other parameters and displays a list of pop-ups that is populated with the same cursor (or identical cursor) as the counter, without using the counter itself.

Can I do this?

+5
source share
5 answers

Use AlertDialog.Builderand put Adapterthrough setAdapter()which generates your lines.

In your case, I would not use the same one Cursor, because it Cursorhas an internal concept of the current line, and therefore messing with Cursorwhile it is used by yours SpinnerAdaptermay be a screw up Spinner. Go with identical Cursor.

+9
source

This is the best example for popups 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();
+10
source

API 11, listPopupWindow , .

+3
            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();
+1

You might want to use PopupMenu

see this example

+1
source

All Articles