Force user to select option in dialog box

When I first started the application, I wanted to suggest to the user (through the dialog box) to select his city, which would then save their preference and load the relevant data into the list. However, I noticed that the dialog box can be closed with the back button or by clicking anywhere outside the dialog box, which then calls up a null value for the database.

In any case, can I make the dialog remain open until the user selects an option?

I have already implemented .setCancelable(false)and this does not work. My code is below, DialogSetup is the inner class I'm working with.

Any help / ideas would be appreciated.

public class MainActivity extends ListActivity {
private Cursor lines;
private MetroSleepDb db;
private ListAdapter adapter;
public static final String PREFS_NAME = "METROSLEEP_PREFS";

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);



    chooseCityDialog();




}




public void setAdapters() {

    db = new MetroSleepDb(this);
    lines = db.getLines(); // you would not typically call this on the main thread
    //ListView listView = (ListView) findViewById(R.id.li);
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, 
            lines, 
            new String[] {"line_id"}, 
            new int[] {android.R.id.text1}, 0);


    getListView().setAdapter(adapter);
    getListView().setOnItemClickListener(onAnswerClicked);

}


public String getItem(int pos) {

    Cursor c = (Cursor) adapter.getItem(pos);
    String value = c.getString(c.getColumnIndex("line_id"));
    return value;
}

private OnItemClickListener onAnswerClicked = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        String line_value = getItem(position);
        Intent intent = new Intent(MainActivity.this, ChooseStations.class);
        intent.putExtra("line", line_value);
        startActivity(intent);
    }

};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem  item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;

    case R.id.menu_settings:
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void chooseCityDialog() {
    DialogFragment newFragment = new DialogSetup();
    newFragment.show(getFragmentManager(), "citypref");
}

public static final class DialogSetup extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.prompt_choose_city)
               .setCancelable(false) 
               .setItems(R.array.Cities, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                       //
               }
        });

        return builder.create();
    }



}

}

+5
3

! setCancelable(false) DialogFragment, AlertDialog.builder. . : setScanelable (false) AlertDialog.

DialogFragment.setCancelable(boolean cancelable) :

. Dialog.setCancelable(boolean), DialogFragment .

1 :

public void chooseCityDialog() {
    DialogFragment newFragment = new DialogSetup();
    newFragment.setCancelable(false);
    newFragment.show(getFragmentManager(), "citypref");
}

, , , - Dialog is null.

+6

AlertDialog DialogFragment .

    AlertDialog dlgBuilder = new AlertDialog.Builder(this);
    dlgBuilder.setMessage("Title");
    dlgBuilder.setCancelable(false)
            .setPositiveButton("OK", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    okCalledMethodInactivity();
                    dialogInterface.dismiss();
                }
            })
            .setNegativeButton("Exit app", new OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                }
            });
    AlertDialog dlg = dlgBuilder.create();
    dlg.show();
+2

""

@Override
public void onBackPressed() {

}
-2

All Articles