Question

I have a main activity from which a fragment is called. From the fragment I need to show a dialog for user input. The alert dialog doesnt show , and there is no error. Passing the context with getActivity() from the fragment.

This is the code section in fragment that instantiates the helper class (QuantizationHelper) that contains the dialog creation

 Qradiobuttongroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
       QuantizationHelper quantizationHelper = new QuantizationHelper();     
         public void onCheckedChanged(RadioGroup group, int checkedId) {


        rssiQuantizedArray.clear();
        ProcessOutput.setText(" ");
                switch(checkedId){                  
                case R.id.radio_standardQ:
                    quantizationHelper.chooseParameters(getActivity(),checkedId,inputParams);
                    ProcessOutput.setText(quantizationHelper.StandardQuantization(RssiPlot,rssiArray,inputParams));
                    break;
                case R.id.radio_LevelCrossing:
                    quantizationHelper.chooseParameters(getActivity(),checkedId,inputParams);
                    ProcessOutput.setText(quantizationHelper.LevelCrossingQuantization(RssiPlot,rssiArray,inputParams));
                    break;
                case R.id.radio_Differential:   
                    ProcessOutput.setText(quantizationHelper.DifferentialQuantization(rssiArray));
                    break;

                }

            }

     });

This is the helper class with partial code

public final class QuantizationHelper {

 private ArrayList<Integer> rssiQuantizedArray = new ArrayList<Integer>() ;     
 AlertDialog paramDialog;

 void chooseParameters(final Context context,final int checkedId,final ArrayList inputParams){

     AlertDialog.Builder builder = new AlertDialog.Builder(context);         

     final CharSequence[] threshholdType = {" Mean "," Median"};

    final TextView param1 = new TextView(context);  
    final EditText input1 = new EditText(context);
    final TextView param2 = new TextView(context);
    final EditText input2 = new EditText(context);

     // Set up the buttons
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
         @Override
         public void onClick(DialogInterface dialog, int which) {
             switch(checkedId){ 
             case R.id.radio_LevelCrossing: 
                 inputParams.add(0,input1.getText().toString());     // Block length param 0             
                 inputParams.add(1,input2.getText().toString());  // alpha param 1

                 if (Double.valueOf((String) inputParams.get(1))>1.0 || Double.valueOf((String) inputParams.get(1))<0){
                     Toast.makeText(context, "Enter alpha in the range 0 to 1 ", Toast.LENGTH_SHORT).show();
                     return;
                 }
             }
             dialog.dismiss();
         }
     });

     switch(checkedId){                  
        case R.id.radio_standardQ:   
            builder.setTitle("Select Threshhold value");      
            builder.setSingleChoiceItems(threshholdType, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    inputParams.add(0,which);
                }

            });
            builder.setCancelable(false);
            builder.create();
            builder.show();

            break;
        case R.id.radio_LevelCrossing:   

            builder.setTitle("Choose parameters");           
            // Set up the parameter input layout
            LinearLayout dialogLayout = new LinearLayout(context);
            dialogLayout.setOrientation(1);          
            // Specify the type of input 
            input1.setInputType(InputType.TYPE_CLASS_NUMBER);
            input2.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
            param1.setText("Block Length");
            param2.setText("Alpha (0 to 1)");
            dialogLayout.addView(param1);
            dialogLayout.addView(input1);
            dialogLayout.addView(param2);
            dialogLayout.addView(input2);
            builder.setView(dialogLayout);
            builder.setCancelable(false);
            paramDialog = builder.create();
            paramDialog.show();             

            break;
        case R.id.radio_Differential:      

            break;

        }

 }

}

EDIT

I have tried this with a dialogfragment..but still no dialog appearing.

Qradiobuttongroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
QuantizationHelper quantizationHelper = new QuantizationHelper();    
         public void onCheckedChanged(RadioGroup group, int checkedId) {


                rssiQuantizedArray.clear();
                ProcessOutput.setText(" ");
                switch(checkedId){                  
                case R.id.radio_standardQ:                      
                    showDialog(checkedId);
                    ProcessOutput.setText(quantizationHelper.StandardQuantization(RssiPlot,rssiArray,inputParams));
                    break;
                case R.id.radio_LevelCrossing:
                    showDialog(checkedId);
                    ProcessOutput.setText(quantizationHelper.LevelCrossingQuantization(RssiPlot,rssiArray,inputParams));
                    break;
                case R.id.radio_Differential:   
                    ProcessOutput.setText(quantizationHelper.DifferentialQuantization(rssiArray));
                    break;

                }

            }

     });

void showDialog(int choice) {
     DialogFragment newFragment = chooseParametersDialog.newInstance(choice);
     newFragment.setTargetFragment(this, choice);
     newFragment.show(getFragmentManager(), "dialog");
 }

 @SuppressWarnings("unchecked")
@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
     switch(requestCode) {
     case R.id.radio_standardQ:
         inputParams.add(0, data.getIntExtra("which",0));
         break;

     case R.id.radio_LevelCrossing:
         inputParams.add(0, data.getIntExtra("blocklength",3));
         inputParams.add(0, data.getDoubleExtra("alpha", 0.2));
         break;

     }
 }

This is the dialogfragment.

public class chooseParametersDialog extends DialogFragment {

static chooseParametersDialog newInstance(int qMethod) {
    chooseParametersDialog f = new chooseParametersDialog();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("qMethod", qMethod);
    f.setArguments(args);

    return f;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int qMethod = getArguments().getInt("qMethod");
    AlertDialog.Builder builder;
    AlertDialog a = null;
    final Intent i = new Intent();

    switch(qMethod){
    case R.id.radio_standardQ:
        final CharSequence[] threshholdType = {" Mean "," Median"};  
        builder = new AlertDialog.Builder(getActivity());               
        builder.setTitle("Select Threshhold value");
        builder.setSingleChoiceItems(threshholdType, -1, new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {

                 i.putExtra("which",which); 
                 dialog.dismiss();
             }

         });
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                getTargetFragment().onActivityResult(getTargetRequestCode(),1, i);
            }
        });
        builder.setCancelable(false);
        builder.create();
        a = builder.create();
        return a;



    case R.id.radio_LevelCrossing:
        final TextView param1 = new TextView(getActivity());    
        final EditText input1 = new EditText(getActivity());
        final TextView param2 = new TextView(getActivity());
        final EditText input2 = new EditText(getActivity());

        LinearLayout dialogLayout = new LinearLayout(getActivity());
        dialogLayout.setOrientation(1);          
        // Specify the type of input 
        input1.setInputType(InputType.TYPE_CLASS_NUMBER);
        input2.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
        param1.setText("Block Length");
        param2.setText("Alpha (0 to 1)");
        dialogLayout.addView(param1);
        dialogLayout.addView(input1);
        dialogLayout.addView(param2);
        dialogLayout.addView(input2);

        builder = new AlertDialog.Builder(getActivity());         
        builder.setTitle("Choose parameters");
        builder.setView(dialogLayout);
        builder.setCancelable(false);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                String blocklength = input1.getText().toString();
                String alpha = input2.getText().toString();
                i.putExtra("blocklength",blocklength);
                i.putExtra("alpha",alpha);

                if (Double.valueOf((String)alpha)>1.0 || Double.valueOf((String)alpha)<0){
                    Toast.makeText(getActivity(), "Enter alpha in the range 0 to 1 ", Toast.LENGTH_SHORT).show();
                    return;

                }
                dialog.dismiss();
            }
        });
        builder.setCancelable(false);
        a = builder.create();
        return a;


    }
    return a;   
}
}

No exception/ error while debugging through the .show() code. Crashes because the inputparams arraylist which has to be fed in with the dialogbox are empty. Frustrating! Sorry for the long post

Was it helpful?

Solution

Use show(FragmentTransaction transaction, String tag) instead of show(FragmentManager manager, String tag)

FragmentTransaction ft = getFragmentManager().beginTransaction(); newFragment.show(ft, "dialog");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top