如何从数据库查询的值填充警报对话框?
现在,当我按下对话框的“确定”按钮时,我只能看到获取的值。但我想在警报对话框出现时看到它。我知道我已将显示代码放入 .setPositiveButton("OK",但即使我尝试将其放在前面,它也不起作用。我有些想法我需要使用这个 setview(addView) 但不这样做知道怎么做吗?
enter code here
private void populateFields() {
LayoutInflater inflater=LayoutInflater.from(this);
final View addView=inflater.inflate(R.layout.add_country, null);
new AlertDialog.Builder(this)
.setTitle("Edit country/year")
.setView(addView)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
Cursor c =
getContentResolver().query (CONTENT_URI.buildUpon ().appendPath(String.valueOf(mRowId)).build(), null, null, null, null);
/* Read alert input */
EditText editCountry =(EditText)addView.findViewById(R.id.editCountry);
EditText editYear =(EditText)addView.findViewById(R.id.editYear);
//System.out.println(c.getString(c.getColumnIndex("country")));
editCountry.setText(c.getString(c.getColumnIndex("country")));
editYear.setText(c.getString(c.getColumnIndex("year")));
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
// ignore, just dismiss
}
})
.show();
}
Right now I can only see the fetched values when I press OK button of the dialog. But I want to see it when the alertdialog appears. I know I have put the display code inside the .setPositiveButton("OK", But even when I tried putting it before that, it didnt work. I some how have idea that i need to make use of this setview(addView) but dont know how. Please help me.
enter code here
private void populateFields() {
LayoutInflater inflater=LayoutInflater.from(this);
final View addView=inflater.inflate(R.layout.add_country, null);
new AlertDialog.Builder(this)
.setTitle("Edit country/year")
.setView(addView)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
Cursor c =
getContentResolver().query (CONTENT_URI.buildUpon ().appendPath(String.valueOf(mRowId)).build(), null, null, null, null);
/* Read alert input */
EditText editCountry =(EditText)addView.findViewById(R.id.editCountry);
EditText editYear =(EditText)addView.findViewById(R.id.editYear);
//System.out.println(c.getString(c.getColumnIndex("country")));
editCountry.setText(c.getString(c.getColumnIndex("country")));
editYear.setText(c.getString(c.getColumnIndex("year")));
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
// ignore, just dismiss
}
})
.show();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在膨胀视图后立即放置代码。像这样的东西:
Try putting your code right after you have inflated the view. Something like this :