Hi I am popping up dialog to take comments from user. And returning a value according to that. That "rcomment" is a global variable. And it returns null. This is not working. What am I doing wrong ?
public String getDoNotBoardDialog(final int groupposition)
{
final Dialog dia = new Dialog(this);
dia.requestWindowFeature(Window.FEATURE_NO_TITLE);
dia.setContentView(R.layout.donotboard);
final EditText donotedit = (EditText) dia.findViewById(R.id.donotboardcomment);
donotedit.setText("");
Button button1 = (Button) dia.findViewById(R.id.donotboardbutton);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
r = donotedit.getText().toString();
String boardingComment = getString(R.string.donotboard) + " " + r;
PostCommentForAC(groupposition, boardingComment);
Intent intent = new Intent(getBaseContext(), TestExList.class);
intent.putExtra("EmpID", empid);
startActivity(intent);
rcomment = "true";
dia.cancel();
}
});
Button button2 = (Button) dia.findViewById(R.id.boardbutton);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
rcomment = "false";
dia.cancel();
}
});
dia.show();
return rcomment;
}
The getDoNotBoardDialog will initial return rcomment as null. rcomment will only be changed to "true" or "false" when the onClickListeners are fired. They are fired not when getDoNotBoardDialog is run, but after that, whenever the onClickListeners are fired.
Whatever you want to happen when rcomment is changed to "true" or "false" should be placed in the onClick methods. So if you want to check what rcomment is after a user clicks, do it there.
Edit: dont use below code. It will kill your app (ANR).
You would have to wait before you can return something. A quick (but really dirty) solution would be to add some wait/notify mechanism like so: (written blind so might contain some errors).
public String getDoNotBoardDialog(final int groupposition) {
// some Object to wait on
final Object waitOnMe = new Object();
final Dialog dia = new Dialog(this);
dia.requestWindowFeature(Window.FEATURE_NO_TITLE);
dia.setContentView(R.layout.donotboard);
final EditText donotedit = (EditText) dia.findViewById(R.id.donotboardcomment);
donotedit.setText("");
Button button1 = (Button) dia.findViewById(R.id.donotboardbutton);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
r = donotedit.getText().toString();
String boardingComment = getString(R.string.donotboard) + " " + r;
PostCommentForAC(groupposition, boardingComment);
Intent intent = new Intent(getBaseContext(), TestExList.class);
intent.putExtra("EmpID", empid);
startActivity(intent);
rcomment = "true";
dia.cancel();
// stop waiting.
synchronized(waitOnMe) {
waitOnMe.notify();
}
}
});
Button button2 = (Button) dia.findViewById(R.id.boardbutton);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
rcomment = "false";
dia.cancel();
// stop waiting.
synchronized(waitOnMe) {
waitOnMe.notify();
}
}
});
dia.show();
// this wait's until someone calls notify
synchronized (waitOnMe) {
try {
waitOnMe.wait();
} catch (InterruptedException e) {}
}
return rcomment;
}
It's problematic though. You could miss the notify() and thus never stop waiting (e.g. when you close the dialog via the "back" button). A much cleaner and safer solution would be to use some Callback mechanism (you call some method in your program from each onClick) to get a value from the dialog.
In you application
the program flows to
return rcomment;
directly after going to
dia.show();
Remember it does not wait for the button to be clicked before it goes to the return statement!!! It goes to return statement directly after the dialog is shown (before the button is clicked)
Try using this string.equals(data) it should find out if the string is the same. Since rcomment is a string. Also like Soham said it will not update only until it is clicked.
I suggest that in the future you should change to Boolean rcomment. Since it only looks like you are doing either true or false status.
Related
This is the first time I'm ever dabbling in Android development so please bear with me.
My requirement is this:
I have two buttons on screen, A and B. If the user presses both buttons (order doesn't matter), I need another page to be displayed. Pressing either A or B should do nothing.
Is this possible? If so, how would I achieve this?
Thank you.
This is possible if you take a flag. (boolean)
You should set a flag in your button listeners.
public class Mtest extends Activity {
Button b1;
Button b2;
boolean flag_1 = false;
boolean flag_2 = false;
public void onCreate(Bundle savedInstanceState) {
...
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b1.setOnClickListener(myhandler1);
b2.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
flag_1 = true;
doSomething();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
// it was the 2nd button
flag_2 = true;
doSomething();
}
};
}
public void doSomething(){
if(flag_1 && flag_2)
{
//DO SOMETHING
}
}
}
Create two boolean's like button1isClickedand button2isClicked,then set an onClickListener for each Button. When the the Button is clicked set the value of these two boolean's to true, then simply create an if() statement that will chekc to see if both buttons have been clicked, like this:
if(button1isClicked == true && button2isClicked == true){
//display your new page
}
I'm looking around internet to have the possibility to press a button just two times. I'm implementing the code for a poker game, then the player should press the button one time to show the card and the second time to change it. How can I do it. The only thing I found was to click it one time (that I don't need because it has also to change a card, but just one time). That's what I have for the first button:
backgroundA.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
int randomInt1 = random1.nextInt(Deck.length());
int drawableIDA = Deck.getResourceId(randomInt1, -1);
backgroundA.setBackgroundResource(drawableIDA);
}
});
You need to have a flag that will check for click, if you click it once the flag will be true and if you click it again your statement inside your OnClickListener will get executed.
sample:
boolean flag = false;
boolean flag2 = false;
backgroundA.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
if(!flag){ flag = true }
else
{
if(flag2) { flag = false; }
else
{
int randomInt1 = random1.nextInt(Deck.length());
int drawableIDA = Deck.getResourceId(randomInt1, -1);
backgroundA.setBackgroundResource(drawableIDA);
flag2 = true;
}
}
}
});
I am developing a android App which is totally based on request and response from servlet.I have populate some data in customize Alert-dialog Where i using two thing one is cross button that will delete item from list in alert-dialog and update view of alert dialog , second thing is close button that will suppose to dismiss this alert-dialog. I am showing full coding of my alert-dialog. I calling alert dialog on button click by all these method.
intiliazeOrderListDialog();
showOrderListDialog();
My decleration is as follow
public AlertDialog detailsDialog, orderDialog;
AlertDialog.Builder builder;
Now i am going to post my intiliazeOrderListDialog() block.
public void intiliazeOrderListDialog() {
builder = new AlertDialog.Builder(MainScreen.this);
mContext = getApplicationContext();
inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
orderDialogLayout = inflater.inflate(R.layout.my_order_list,(ViewGroup)findViewById(R.id.order_list_root));
orderList = (ListView) orderDialogLayout.findViewById(R.id.order_list);
ibOrderDelete = (ImageButton)orderDialogLayout.findViewById(R.id.deleteOrder);
tvPrice = (TextView) orderDialogLayout.findViewById(R.id.order_list_total);
tvTaxes = (TextView) orderDialogLayout.findViewById(R.id.order_list_taxes);
tvTotal = (TextView) orderDialogLayout.findViewById(R.id.order_list_grand_total);
Button bclose = (Button) orderDialogLayout.findViewById(R.id.close);
Button bPlaceOrder = (Button) orderDialogLayout.findViewById(R.id.my_order_placeorder);
bclose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
orderDialog.dismiss();
System.out.println(" click on closowse");
}
});
bPlaceOrder.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Place order click");
palceMyOrdertoServer();
new SendOrderFromTable().execute();
System.out.println("place order to server is called");
String msg = "Your Order is Successfully placed to Kitcken";
Message msgObject = new Message();
msgObject.what = 1;
msgObject.obj = msg;
addMenuItemHandler.sendMessage(msgObject);
orderDialog.dismiss();
}
});
}
and at last i am going to post showOrderListDialog(); block
public void showOrderListDialog() {
builder.setView(orderDialogLayout);
orderDialog = builder.create();
orderDialog.show();
}
I know i have posted too many codes but its conveniences for those who want to help me . I have a very simple problem why my
orderDialog.dismiss();
is not working for me.? Thanks in advance to all .
Finally i solve my own issue . "Self help is best help ;;".
It was issue of calling method in setOnClickListener.
I just call it,
android:clickable="true"
android:onClick="clickHandler"
if (v.getId() == R.id.myOrder) {
System.out.println("Click my Order");
System.out.println("OrderListAdapter.totalCount ="
+ OrderListAdapter.totalCount);
// select COUNT(*) from CDs;
int jcount = 0;
jcount = countjournals();
System.out.println("jcount = " + jcount);
// Count implementation at my Order
if (jcount < 1) {
alertShow();
} else {
intiliazeOrderListDialog();
showOrderListDialog();
}
// startActivity(new Intent(RestaurantHome.this,
// MyOrderList.class));
}
In my android app, I have a Play button. but after I clicked the button, nothing happend. Seems when comparing the text on the button, they are not equal. if I use indexOf(PLAY), it works. Cannot figure out why it behaves like this. I defined my string values in res/values/strings.xml as
<string name="play">Play</string>
private final static String PLAY = "Play";
//some code in between
Button playButton = new Button(this);
playButton.setText(R.string.play);
playButton.setTextSize(BUTTON_FONT_SIZE);
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Button b = (Button) v;
if (b.getText().equals(PLAY)) { //stuck here.
startPlay();
} else {
stopPlay();
}
}
});
Thanks for the help.
You can use an intermediate variable, like this
String buttonText = b.getText().toString();
if (buttonText.equals(PLAY)) {
.....
Or just
if (b.getText().toString().equals(PLAY)) {
I have an activity class in my Android App, where after some data is handled, that data is sent to another method in another class which gives back an ArrayList. What I want to do is to iterate through that ArrayList and for every piece of text in it, I would like to update the TextView label in my interface. However this should be done whenever the user presses the 'Yes' button on screen. (i.e. whenever the user presses the 'Yes' button, the loop would progress to the next element of the ArrayList and its value updated in the TextView label). Here is what I have so far:
try {
sentenceGenerator = new SentenceGenerator();
ArrayList<ArrayList<String>> states = stateGenerator
.getPossibleStates(resultOutput, result, result);
for (ArrayList<String> state : states) {
viewPoint = state.get(0);
subject = state.get(1);
object = state.get(2);
subjectInfo = new ObjectReference();
if (resultOutput.equals("Sliema")) {
Sliema sliema = new Sliema();
subjectInfo = sliema.getInfo(viewPoint, object, subject);
}
try {
s = sentenceGenerator.generateSentence("start", "middle",
"end", resultOutput, viewPoint, object, subject,
subjectInfo.type, subjectInfo.name,
subjectInfo.properties, false);
instructionTextView = (TextView) findViewById(R.id.instructionTextView);
mHandler = new Handler();
instructionTextView
.setText("Press 'Yes' to start Orientation procedure");
Button confirmButton = (Button) findViewById(R.id.confirmButton);
Button declineButton = (Button) findViewById(R.id.declineButton);
confirmButton
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mHandler.post(mUpdate);
}
});
declineButton
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//
}
});
Log.d("INSTRUCTIONS", s);
} catch (Exception e) {
}
}
} catch (ClassNotFoundException e) {
}
and this is the update Handler:
private Runnable mUpdate = new Runnable() {
public void run() {
instructionTextView.setText(s);
// mHandler.postDelayed(this, 1000);
confirmPressed = true;
// i++;
}
};
Any Suggestions? Thanks in advance!
Try Adding a break;in the buttonclickListener where you want to terminate the loop.