-2

i have code that fetches a string on php page using a class that extends AsyncTask . Now I need to change button color on UI according to the string value. But I discovered that v cannot change it in AsyncTask class so v need to pass again the resultString to main thread. How should i do that? Here is my code:

MainActivityClass
{
    //button color changes acc to php page string
}

AsyncTaskClass
{
    String result=fetch string data from php using doInBackground method;
    //cant change button color here need to pass result to main activity
}
jrbedard
  • 3,662
  • 5
  • 30
  • 34

2 Answers2

0

Async task has a override method, onPostexecute(), call your mainActivity function in this function and pass string as parameter.

Use following for reference:

https://developer.android.com/reference/android/os/AsyncTask.html

https://stackoverflow.com/a/9671602/2487029

Community
  • 1
  • 1
niksya
  • 291
  • 2
  • 11
0
Class A{
    private MyListener ml;
    doInBackground(){
        //string your_string = GetFromWeb();
        //passString(your_string);

    }
    public void setMyCustomListener(MyListener l){
        ml = l;
    }
    public interface MyListener{
        public void passString(String s);
    }
}

Class B implements MyListener {

    @Override
    public void passString(String s){
        //Do your thing here
    }
}
  • create a global variable :/ really ? v have here 2 diffrent class how can i send value of global variable from 1 class to another?????? :? – PRERAK CHOKSI Sep 28 '16 at 15:50
  • What I am suggesting here to you is that you can use a custom interface which the class which needs to update the UI of activity or your Activity should implement – Ibrahim Ali Khan Sep 28 '16 at 17:09