Saturday, 15 January 2011

android - Function with return value uses AlertDialog -



android - Function with return value uses AlertDialog -

i'm overriding function interface (which can't modify), let's say

public abstract int getresult();

in function, i'd inquire user of result should be, using alertdialog. implementation should like:

public int getresult() { int result; dialoginterface.onclicklistener dialogclicklistener = new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { switch (which){ case dialoginterface.button_positive: result = 1; break; case dialoginterface.button_negative: result = 2; break; } } }; alertdialog.builder builder = new alertdialog.builder(this); builder.setmessage("what result?").setpositivebutton("1", dialogclicklistener) .setnegativebutton("2", dialogclicklistener).show(); homecoming result; }

obviously, won't work due asynchronous nature of alertdialog. proper way deal kind of situation?

you must run code on ui thread like:

runonuithread(new runnable() { public void run() { getresult(); } });

or

public int getresult(){ runonuithread(new runnable() { public void run() { // code } }); }

android asynchronous

No comments:

Post a Comment