0

現在我只能按下對話框中的OK按鈕才能看到提取的值。但是我想在alertdialog出現時看到它。我知道我已經把顯示代碼放在.setPositiveButton中(「OK」,但是即使當我試圖在它之前放置它時,它也沒有工作。我有些想法,我需要使用這個setview(addView),但不要懂得請幫我如何從數據庫中查詢值填充alertdialog?

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(); 
} 

回答

3

嘗試把你的代碼已經膨脹的觀點之後事情是這樣的:。

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(); 
} 
+0

感謝它的工作就像一個魅力 – SASM

+0

很高興幫助:)! –