如何从数据库查询的值填充警报对话框?

发布于 2024-12-06 03:29:11 字数 1445 浏览 1 评论 0原文

现在,当我按下对话框的“确定”按钮时,我只能看到获取的值。但我想在警报对话框出现时看到它。我知道我已将显示代码放入 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-12-13 03:29:11

尝试在膨胀视图后立即放置代码。像这样的东西:

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 */
EditText editCountry =(EditText)addView.findViewById(R.id.editCountry);
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",null)
.setNegativeButton("Cancel",null)
.show();
}

Try putting your code right after you have inflated the view. Something like this :

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 */
EditText editCountry =(EditText)addView.findViewById(R.id.editCountry);
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",null)
.setNegativeButton("Cancel",null)
.show();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文