フラグメントが呼び出されるメイン アクティビティがあります。フラグメントから、ユーザー入力用のダイアログを表示する必要があります。警告ダイアログは表示されず、エラーはありません。フラグメントから getActivity() でコンテキストを渡します。
これは、ダイアログの作成を含むヘルパー クラス (QuantizationHelper) をインスタンス化するフラグメントのコード セクションです。
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;
}
}
});
これは、部分的なコードを含むヘルパー クラスです。
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;
}
}
}
編集
ダイアログフラグメントでこれを試しましたが、まだダイアログが表示されません。
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;
}
}
これがダイアログフラグメントです。
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;
}
}
.show() コードによるデバッグ中に例外/エラーは発生しません。ダイアログ ボックスで入力する必要がある inputparams 配列リストが空であるため、クラッシュします。もどかしい!長文すみません