guys I've been trying to solve this problem but I couldn't
I want when the user click on the btn_delete he will get a message to insure the delete (Yes or No), I've tried a lot of methods but I don't know exactly what's the problem, I'm new in Android programing so forgive me for my stupid questions, here is my Java code :
public void onDeleteClick(View v) {
int i = Integer.parseInt((String)v.getTag());
Address address = _list.get(_currentPage*PANELS_PER_PAGE + i);
_dbAdapter.deleteAddress(address.Id);
_GetAddresses();
}
Replace the onDeleteClick method with the following method:
public void onDeleteClick(View v) {
int i = Integer.parseInt((String)v.getTag());
AlertDialog.Builder alert = new AlertDialog.Builder(AddressListActivity.this);
alert.setTitle("Delete");
alert.setMessage("Are you sure you want to delete?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Address address = _list.get(_currentPage*PANELS_PER_PAGE + i);
_dbAdapter.deleteAddress(address.Id);
_GetAddresses();
dialog.dismiss();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
Related
Android Studio 3.0.1
It works in QAnswerQuestion.java code
List<Integer> wrongList = UIResponse.checkAnswer(list);
if (wrongList.size() == 0)
{
new AlertDialog.Builder(QAnswerQuestion.this).setTitle("Info")
.setMessage("You are awesome and all answers are correct!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}).setNegativeButton("Cancel", null).show();
}
but when I try to put the above code in UIResponse.java
and call in QAnswerQuestion.java like this:
UIResponse.lastQuestionDialog(QAnswerQuestion.this,list);
and UIResponse.java code is
static void lastQuestionDialog(final Context context, List<Question> list)
{
List<Integer> wrongList = UIResponse.checkAnswer(list);
if (wrongList.size() == 0)
{
new AlertDialog.Builder(context).setTitle("Info")
.setMessage("You are awesome and all answers are correct!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
finish();
}
}).setNegativeButton("Cancel", null).show();
}
}
It says "can't resolve finish methods "
The problem is you are showing dialog in other class which is UIResponse . And finish() is method of Activity.one simple solution can be .
static void lastQuestionDialog(final Context context, List<Question> list)
{
List<Integer> wrongList = UIResponse.checkAnswer(list);
if (wrongList.size() == 0)
{
new AlertDialog.Builder(context).setTitle("Info")
.setMessage("You are awesome and all answers are correct!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
((Activity)context).finish();
}
}).setNegativeButton("Cancel", null).show();
}
}
Other then this I suggest you to use an callback interface to notify Activity about the dialog actions so that you can manage them in your Activity . Read how-to-define-callbacks-in-android.
I am trying to get a number of cards to pop up using alertdialog.builder. Even though I did .create().show(); the dialog does not show on my screen. I'm not sure what is causing this problem.
I have marked the place where I am getting nothing in the comments.
Java Code:
ImageView image_questionmark= new ImageView(this);
final ImageView image_pass = new ImageView(this);
final ImageView image_youpay = new ImageView(this);
image_questionmark.setImageResource(R.drawable.card_questionmark);
image_pass.setImageResource(R.drawable.card_pass);
image_youpay.setImageResource(R.drawable.card_youpay);
AlertDialog.Builder dialog = new AlertDialog.Builder(PayActivity.this);
for(int i=0; i<people; i++) {
/*PASS*/
if(array[i] == 0) {
dialog.setView(image_questionmark);
dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(PayActivity.this)
.setView(image_pass)
.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); /*move on to next value in array*/
}
}).create().show(); /*HERE: Nothing showed on my screen when running debugger...*/
}
});
dialog.create().show();
/*If not the first card, show previous card*/
if(i!=0) {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
i--; /*return to previous value in array*/
} /*First card*/
else {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
}
});
}
}/*YOU PAY*/
else {
dialog.setView(image_questionmark);
dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(PayActivity.this)
.setView(image_youpay)
.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); /*move on to next value in array*/
}
}).create().show();
}
});
dialog.create().show();
/*If not the first card, show previous card*/
if(i!=0) {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
i--; /*return to previous value in array*/
} /*First card*/
else {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
}
});
}
}
}
Thank you for your time :)
I did not try your code.But you can try it like -
First initilise your image object then assign to another final variable.
I think it will work.
ImageView image_pas1 = new ImageView(this);
final ImageView image_youpay = new ImageView(this);
image_questionmark.setImageResource(R.drawable.card_questionmark);
image_pass1.setImageResource(R.drawable.card_pass);
image_youpay.setImageResource(R.drawable.card_youpay);
final ImageView image_pass=image_pass;
You need to set LayoutParams for your ImageView in order to know how to draw itself. Maybe something like this:
image_questionmark.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // width
LayoutParams.WRAP_CONTENT)); // height
And do this as well for your other 2 ImageViews
First of all, Dialogs need
buildervariablename.create();
only.
Second, every single Dialog has to be in a seperate class, like this:
public class TestDialog extends DialogFragment
{
// Put your dialog code in here
}
And then you just call your dialog there, where it is needed. Inside another method, or even another dialog, by using the following:
DialogFragment fragment = new TestDialog();
fragment.show(getFragmentManager(),"testdialog");
Hope this helped you, if you have further questions, just comment this answer.
I am a beginner, so if anyone would help me out. I created a list in the dialogue box , now how do i use those options? Like click one and it does something , click another and it does something else.
CharSequence features[] = new CharSequence[] {"Save", "Send", "Something", "Something"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Options");
alertDialog.setItems(features, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Eta chu ma aile",
Toast.LENGTH_LONG).show();
}
});
alertDialog.show();
return true;
}
If you know exact position of every item, just compare it with which param.
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// handle "Save" option
} else if (which == 1) {
// handle "Send" option
} ...
}
You can use following code:
Somewhere in another function:
String title = "My Alert Box";
String msg = "Choose Option";
alertfunc(title,msg);
The main alert function:
private void alertfunc(String title, String msg) {
if (title.equals(TASK_VIEW_PROFILE)) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
//Do something
}
})
.setNegativeButton("Send",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
.setNegativeButton("Something",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
//...and so on
}
}
I have an AlertDialog which displays an array into a single selected choice:
protected boolean blFrom, blTo;
protected void showSelectToDialog() {
boolean[] checkedDate = new boolean[toDate.length];
int count = toDate.length;
DialogInterface.OnClickListener setD2 = new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//TODO Auto-generated method stub
onChangeSelectedTo(which);
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select To Year");
builder.setSingleChoiceItems(toDate, count, setD2);
builder.setCancelable(true);
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog2 = builder.create();
dialog2.show();
}
protected void onChangeSelectedTo(int j) {
bTo.setText(toDate[j]);
sTo = ((AlertDialog)dialog2).getListView().getCheckedItemPosition();
blTo = true;
displayToast(String.valueOf(sTo));
to = j;
dialog.dismiss();
}
What I want to do is the first time it loads it should display the default. Once I select a choice and the dialog closes and I bring up the same dialog again it should show the previously selected choice I made and scroll to it.
How do I accomplish it?
As of right now, I can get the selected position but what next?
You can store the chosen value in a variable of your Activity or using SharedPreferences
Alright, this might be kinda simple, but I cannot figure out how to do this. How can I change this function to return the String class_name? I know that I need to change the function from void to String, but what else do I need to do?
Much appreciated!
public void addClass() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Add Class");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String class_name = input.getText().toString();
}
});
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
Returning the class_name value doesn't help. You have to implement a callback method that takes the value and performs the required action:
public void addClass() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// ...
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setClassName(input.getText().toString());
}
});
// ...
}
protected void setClassName(String class_name) {
// do what ever has to be done with class_name
}
The behavior (a synchronous dialog) you are trying to get deliberately does not exist in android. Whatever you want to happen when the dialog button is clicked has to be placed in the OnClickListener.OnClick method.
Assuming alert.show does not return until input.getText() contains the entered value, you could try:
public void addClass() {
final Vector<String> retval = new Vector<String>();
retval.add("");
...
...
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String class_name = input.getText().toString();
retval.set(0,class_name);
}
});
...
...
return retval.get(0);
}
AlertDialog is asynchronous and therefore what you want to achieve is not possible through that code.
you could set String class_name as global ad then initialize it to
class_name = input.getText().toString();
inside the onClick method of alert Button.