-1

I'want to update my UI from another class PrayerTime.java.
First Method :
I used this and I got (cannot resolve methode findViewById(int) error
PrayerTime.java :

 protected void onPostExecute(String s) {
        TextView rs = (TextView) findViewById(R.id.result);
        rs.setText(s);

    }

Seconde Method :
In PrayerTime.java I made this :

public class PrayerTime extends AsyncTask<String, String, String>  {
public String fr = null;
..
protected void onPostExecute(String s) {
        this.fr = s;
    }

And in MainActivity.java :

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        PrayerTime pr = new PrayerTime();
        pr.execute();
        TextView rs = (TextView) findViewById(R.id.result);
        rs.setText(pr.fr);

    }

Here no error is shown but Also the Textview is not updated after The AsynTask is done.

I need you'r help to correct this or find another solution to update my UI

Chlebta
  • 3,090
  • 15
  • 50
  • 99

4 Answers4

1

See this answer: https://stackoverflow.com/a/12575319/2202894

You have to create an interface so PrayerTime can call a method of MainActivity to return the result.

Another more ad-hoc solution would be to pass a reference of MainActivity to PrayerTime and do this OnPostExecute

TextView rs = (TextView) mainActivity.findViewById(R.id.result);
rs.setText(s);
Community
  • 1
  • 1
Dimitris Fousteris
  • 1,091
  • 8
  • 12
  • Yes, findViewById() uses the current Context to look for the view. The other class that didn't create the layout for the activity doesn't know about the layout resources. You have to pass the MainActivity.this (the context) to the other class so it knows where to look for the "result" view resource. – AnxGotta Oct 20 '14 at 15:23
  • can you explain me More this part :` You have to pass the MainActivity.this (the context) to the other class so it knows where to look for the "result" view resource` – Chlebta Oct 20 '14 at 15:48
1

You cannot access the UI of an activity from another file, you need to create a callback to the activity with an Interface something like this

public class PrayerTime extends AsyncTask<String, String, String>  {

OnPrayerTimeTaskFinished listener;

    public interface OnPrayerTimeTaskFinished{
        public void onPlayerTimeTaskFinished(String myString);
    }

    public void setOnPrayerTimeTaskFinishedListener(OnPrayerTimeTaskFinished callback){
        listener = callback
    }

    protected void onPostExecute(String s){
        listener.onPlayerTimeTaskFinished(s);
    }
}

then in your activity

PrayerTime pt = new PrayerTime();
pt.setOnPrayerTimeTaskFinishedListener(new setOnPrayerTimeTaskFinishedListener(){
      @Override
        public void onPlayerTimeTaskFinished(String myString){
            //do stuff here with string
        }

});
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Please can you explain me this more ? I don't like to write code without understanding it thanks :) – Chlebta Oct 20 '14 at 15:51
  • what would you like to know or dont understand? – tyczj Oct 20 '14 at 15:53
  • basically this is how a button click works for example when you set `setOnClickListener` for a button you are setting the interface listener to call back there so think of your onPostExecute as the onClick event in a button. You finished doing whatever in the background of your asynctask and now you want to send it back to your activity to update the UI – tyczj Oct 20 '14 at 16:04
  • This part what I can't get clearlly `public void setOnPrayerTimeTaskFinishedListener` and then how it's called main activity : `pt.setOnPrayerTimeTaskFinishedListener(new setOnPrayerTimeTaskFinishedListener()` who it works and how function are called one by one . – Chlebta Oct 20 '14 at 16:19
  • 1
    you set the listener with `pt.setOnPrayerTimeTaskFinishedListener` so that your AsyncTask's onPostExecute can call the method `listener.onPlayerTimeTaskFinished(s);` so when your onPostExecute is called you call the method. the interface is a list of methods your listener can call. maybe this will help explain better http://docs.oracle.com/javase/tutorial/java/concepts/interface.html – tyczj Oct 20 '14 at 16:25
0

Seems like you are beginner in Android, first make your concept clear about AsyncTask & Handlers in Android. You need to understand difference between Main UI thread and normal Threads. & how you can communicate between them in Android.

Using AsyncTask & Handers you can achieve the desired functionality.

AsyncTask From android documentation

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

Handlers from android documentation

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.

You can Refer this tutorial also

Akhil
  • 6,667
  • 4
  • 31
  • 61
-1

You can create you textview global in you MainActivity Class and then you will be able to update the text. Do something like this.

     public class MainActivity extends Activity {

 TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
    tv = (TextView) findViewById(R.id.result);
     PrayerTime pr = new PrayerTime();
        pr.execute();
}

public class PrayerTime extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        //Do your work and return the result here
        return result; //result is your returned
    }


    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        tv.setText(result);

    }


  }
}
Raheel
  • 181
  • 12