第一次啓動應用程序時,我想提示用戶(通過對話框)選擇他們的城市,然後保存他們的偏好並將適當的數據加載到列表視圖中。但是,我注意到可以通過後退按鈕關閉對話框,或者向對話框外部的任何位置推入對話框,然後觸發數據庫的空值。強制用戶在對話框中選擇選項
是否有反正我可以強制對話框保持打開,直到用戶選擇了一個選項?我已經實施了.setCancelable(false)
,這似乎不起作用。我的代碼如下,DialogSetup是我正在使用的內部類。
任何幫助/想法,將不勝感激。
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();
}
}
}
您可以使用該空值觸發器作爲再次啓動對話框以獲取答案的方式嗎? – TronicZomB 2013-04-10 19:02:56
我可以檢查是否未選擇任何值並重新觸發對話框。這確實是一個解決方案,謝謝。 – markbratanov 2013-04-10 19:04:27
我相信你也可以使用'onDismissListener'來不排除沒有價值。 Althoug,我不知道爲什麼'setCancelable(false)'不起作用。我沒有使用'DialogFragment',但在其他'Dialogs'中正常工作 – codeMagic 2013-04-10 19:05:08