这是我编辑/更新的代码,我正在做一个自定义alertdialog,其中包含数据获取的数据库。现在,当提供的输入不正确时,我想在此基础上显示其他alertdialog,向用户提供一些消息。当用户关闭此消息alertdialog时,应该可以看到用于更新的前一个消息。我怎么能这样做?
public class CountryEdit extends ListActivity{
private Long mRowId;
private CountryDbAdapter mDbHelper;
public static final String PROVIDER_NAME = "assignment2.demos.MyCountriesCP";
public static final String uriString = "content://"+ PROVIDER_NAME +"/countries";
public static final Uri CONTENT_URI = Uri.parse(uriString);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(CONTENT_URI, null, null, null, getIntent().getExtras().getString("SORT_ORDER"));
String[] from = new String[] { "year", "country" };
int[] to = new int[] { R.id.year, R.id.country };
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.country_row,
c, from, to);
setListAdapter(sca);
mRowId = savedInstanceState != null ? savedInstanceState.getLong(assignment2.demos.MyCountriesActivity.KEY_ROWID)
: null;
if (mRowId == null) {
Bundle extras = getIntent().getExtras();
mRowId = extras != null ? extras.getLong(assignment2.demos.MyCountriesActivity.KEY_ROWID)
: null;
}
populateFields();
}
private void populateFields() {
LayoutInflater inflater=LayoutInflater.from(this);
final View addView=inflater.inflate(R.layout.add_country, null);
Cursor c = getContentResolver().query(CONTENT_URI.buildUpon().
appendPath(String.valueOf(mRowId)).build(), null, null, null, null);
/* Read alert input */
final EditText editCountry =(EditText)addView.findViewById(R.id.editCountry);
final EditText editYear =(EditText)addView.findViewById(R.id.editYear);
editCountry.setText(c.getString(c.getColumnIndex("country")));
editYear.setText(c.getString(c.getColumnIndex("year")));
new AlertDialog.Builder(this)
.setTitle("Edit country/year")
.setView(addView)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
String country = editCountry.getText().toString();
if(country.trim().length()>0 && editYear.getText().toString().trim().length()>0){
int year = Integer.parseInt(editYear.getText().toString());
ContentValues updateValues = new ContentValues();
updateValues.put(mDbHelper.COUNTRY, country);
updateValues.put(mDbHelper.YEAR, year);
getContentResolver().update(
CONTENT_URI.buildUpon().appendPath(String.valueOf(mRowId)).build(), updateValues, null, null);
finish();
}
else{
new AlertDialog.Builder(CountryEdit.this)
.setTitle("Invalid Inputs!")
.setMessage("You need to enter Country AND Year.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
finish();
}
}).show();
// Toast.makeText(CountryEdit.this,
// "You need to enter Country AND Year.", Toast.LENGTH_LONG).show();
//finish();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
// ignore, just dismiss
finish();
}
})
.show();
}
}
答案 0 :(得分:0)
第二个警告对话框在第二个即将显示时(当点击监听器完成时)自行解除。你无法避免这种情况。
有一个肮脏的黑客,您可以覆盖构建器创建的单击侦听器,如下所示:
create().getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( ... }
但如果您不知道自己在做什么(可能会破坏互联网;-),我不建议这样做。
你必须自己解雇对话。如果你不这样做它永远不会关闭,用户将永远看到它,并且必须杀死你的应用程序才能重新启动它。