Reputation: 337
I am trying to do some data verification. In short, when an add button is pressed, if certain fields are not filled in then I want to display a message box and return from further processing.
This is the flow of my code without the messageBox code:
Button add = (Button) findViewById(R.id.addButton);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//local vars
String access;
String gender;
String avail;
String availCode;
// getting values from selected editItems
String name = textName.getText().toString();
String street = textStreet.getText().toString();
String city = textCity.getText().toString();
String state = textState.getText().toString();
String postal = textPostal.getText().toString();
String country = textCountry.getText().toString();
String directions = textDirections.getText().toString();
String comments = textComments.getText().toString();
//verify miniminal data
if((name.equals("")) || (street.equals(""))|| (city.equals("")) || (state.equals("")) || (postal.equals("")) || (country.equals("")))
{
}
I tried pasting in this code:
//verify miniminal data
if((name.equals("")) || (street.equals(""))|| (city.equals("")) || (state.equals("")) || (postal.equals("")) || (country.equals("")))
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setTitle("Title");
builder.setInverseBackgroundForced(true);
builder.setMessage("Must enter minimal data.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
return;
}
});
AlertDialog alert = builder.create();
alert.show();
}
But...I cannot get this line to build:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
Eclipse is saying context cannot be resolved to a variable.
I am confused as to what to do. Can someone help?
Upvotes: 0
Views: 258
Reputation: 36449
I don't see a context
variable anywhere in the code above, and AlertDialog.Builder
's constructor needs a Context
instance passed to it.
However, since you're doing this from an OnClickListener
's onClick()
, you can use View#getContext()
to get a Context
instance.
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
Upvotes: 1