Reputation: 161
Say I have 5 buttons. For each button I want to be able to fire a listener. Once the listener is fired I want fire an async task in my sdk, and then have the sdk return the status of the async task.
1) Do I write a separate listener/ button? I read somewhere I can have only one registered listener in android, if thats true how do I handle many listeners?
2) How do I return postExecute call result from SDK to the api level?
Upvotes: 0
Views: 264
Reputation: 44571
You can have multiple listeners and depending on what you want to do with them, you can have one for each button. As far as the postExecute()
, it runs on the UI thread so you can show your result from there or do whatever you want with it. For more details, you will need to provide some of the code you have tried and explain exactly where you are having trouble. If you haven't already, go through the
Android Docs about getting started
Upvotes: 0
Reputation: 86948
1) Do I write a separate listener/ button? I read somewhere I can have only one registered listener in android, if thats true how do I handle many listeners?
A View can only have one listener of each type, ie a Button cannot have two OnClickListeners
. Don't confuse this with the fact that one listener can be attached to multiple Views, ie ButtonA and ButtonB can have the same OnClickListener
2) How do I return postExecute call result from SDK to the api level?
Your terminology isn't right, but you'll figure it out as you go. Typically onPostExecute()
will call another method or work directly with a View:
@Override
protected void onPostExecute(String result) {
doSomething(result);
textView.setText(result);
}
Upvotes: 2
Reputation: 1881
Easiest is if you setup one listener in your activity, and then handle multiple buttons. You can do this with the OnKeyListener class. You then do a switch on which key was hit, setup cases for the buttons you wish to act on, and start your AsyncTask
.
I'm not quite sure what you mean by "return postExecute". But if you look at AsyncTask you can see how to use the proper parameters to return a result into onPostExecute. When you instantiate your subclass of AsyncTask
you can easily pass in the activity or context you wish to perform a call back on.
Upvotes: 0