-1

I would like to enable a few buttons from my main activity once the stuffs from doInBackground() is finished! Can someone please let me know how to do that?

I can't use findViewByID() for making he button visible from the AsyncTask class as it's not an activity class! :/

stealthjong
  • 10,858
  • 13
  • 45
  • 84
defiant
  • 3,161
  • 11
  • 41
  • 65

4 Answers4

5

Do Like this...

  • Define a method which enables the Buttons.
  • Then on PostExecute() on AsyncTask, call that method
Renjith
  • 5,783
  • 9
  • 31
  • 42
  • So I have to define the method in MainActivity.java and call it from AsyncTask PostEXecute()? – defiant Dec 28 '12 at 10:33
  • 4
    yes.. Make the buttons global so that you will get it anywhere in the activity. – Renjith Dec 28 '12 at 10:35
  • Its not working, I need to pass the view to the method which enables the buttons from the MainActivity right? But this method is being called from the AsyncTask class, so how do I do this? – defiant Dec 28 '12 at 11:44
  • you don't have to pass the view to `Asynctask` for just enabling it. Just call the method on `onPostExecute()`. in the method, enable the buttons. tats all... – Renjith Dec 28 '12 at 11:48
  • Check this: http://pastie.org/private/n9zyelt5zsumjl0et464q This gives me an error which makes the application to force close. Error Log: http://pastie.org/5588296 – defiant Dec 28 '12 at 12:09
  • Are you sure I don't have to pass view. I am using findViewId, so I think I am doing it the wrong way! :/ – defiant Dec 28 '12 at 12:33
  • try calling just the method without the `activity` tag i.e just `setButtonsVisible` – Renjith Dec 28 '12 at 15:46
0

there is one callback onPostExecution(...) { } of AsynTask class use this method to UI stuff,for enable,disable button just write this way in onPostExcustion(...)

runOnUiThread(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }
});

also make sure this method only available in activity class

thank you

Bhavdip Sagar
  • 1,951
  • 15
  • 27
0

your class which extends AsyncTask you can push your context into it, when calling the execute().

private class RegisterUser extends AsyncTask<String,String,String> {

    private ListActivity activity;
    public RegisterUser(ListActivity activity) {
        this.activity = activity;
    }

    protected void onPostExecute(JSONObject json) {
        activity.editText = (EditText)activity.findViewById(R.id.editText1);
        //or
        activity.enableButton();
    }
}

and call the execute from the Activity like this:

new RegisterUser(this).execute(new String[] {"param"});

or you can define the AsyncTask class inside your Activity class - where you can reach everything. more info Lars Vogel - Android Threads, Handlers and AsyncTask

laplasz
  • 3,359
  • 1
  • 29
  • 40
0

Follow this way:

[1] Create your AsyncTask :

public class performBackgroundTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog Dialog = new ProgressDialog(HotUsers.this);
    protected void onPreExecute() {
        Dialog.setMessage("Loading Hot Users..."); 
        Dialog.show();      
    }

    protected void onPostExecute(Void unused) {
        if(Dialog.isShowing()) 
            Dialog.dismiss();
        set_details_on_screen();

    }

    @Override
    protected Void doInBackground(Void... params) {         
        get_details_from_server(); // get data like userid,username,userdesc etc...
        return null;
    }
}

[2] That will call function to proceed for UI changes.

public void set_details_on_screen()
 {
    if(userid > 0 )
        handler_default.sendEmptyMessage(0);
    else
            handler_default.sendEmptyMessage(1);

 }

[3] At last your UI changes will be reflected on screen with this Handler.

private Handler handler_default = new Handler() {
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0: {
            textuserid = (TextView) findViewById(R.id.userid);
            textusername = (TextView) findViewById(R.id.username);
            textuserdesc = (TextView) findViewById(R.id.userdesc);

            textuserid.setText(userid);
            textusername.setText(username);
            textuserdesc.setText(userdesc);

            break;
        }
        case 1: {
            Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
            break;
        }

       }
    }
};

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37