how to populate the alertdialog from the values queried from database?
-
29-01-2021 - |
Question
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();
}
Solution
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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow