1

I'm quite new to Android Programming and I have a little Problem right now.

I'm building an App that should get Data from an DB in an Siemens PLC periodically. For different Data Types I have different Tabs in a Tab view. Now I try to update specific Data per Tab, only when the tab is active. For now I update them with the onTabChanged Method, once per Change. This works fine.

Now I tried several solutions for periodically updating ( like this or this ) but I couldn't get it work. The "best" Solution was with an whil(true) statement, which made the Data getting updated every 5 seconds, but it didnt update the UI and didn't leave the loop, except I forced the app to shut down.

As far as I got the answers right, my problem is, that I want to update the UI in my main thread. I've got a function that reads the Data from the PLC and one that writes the Data in the Items of the Tab. The read function needs data from a class that is handed over from a "loginactivity" as an Intent, so I can call it not until the intent was called, which is in the oncreate method of the activity that contains the tabview.

Is there any possibility to execute the lines of code, which work fine in the ontabchanged right now, ervery 5 seconds as long as the tab is active? As said before I have one function that reads which needs a class file handed over from the intent, and a function to write data. It looks like this atm:

Bundle data = getIntent().getExtras();
final Login_Data logindata = data.getParcelable("logindata");
tabHost.setOnTabChangedListener(new OnTabChangeListener() {

    @Override
    public void onTabChanged(String tabId) {

        if (tabId == "Anzeige1") {
            Anzeigedatenlesen(logindata);
            Anzeigeschreiben();                             
        } else if (tabId == "Anzeige2") {
            System.out.println("Anzeige2"); 
        } else if (tabId == "Anzeige3") {
            System.out.println("Anzeige2");
        }
    }
});
Community
  • 1
  • 1

1 Answers1

0

The code from the answers you link to should work for what you want, see the code below:

Bundle data = getIntent().getExtras();
final Login_Data logindata = data.getParcelable("logindata");
Handler mHandler = new Handler();
Runnable mRefresh = new Runnable() {

    @Override
    public void run() {
       // I guess this are the update methods?
       Anzeigedatenlesen(logindata);
       Anzeigeschreiben();
       mHandler.postDelayed(mRefresh, 5000);
    }   
};
tabHost.setOnTabChangedListener(new OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {

    if (tabId.equals("Anzeige1")) {
        // cancel any pending refresh that might be run in the near future 
        // as we got to our target tab and we need to refresh now!!!
        mHandler.removeCallbacks(mRefresh);
        // do a normal refresh
        Anzeigedatenlesen(logindata);
        Anzeigeschreiben();   
        // post a new refresh cycle as the user might stay on this tab                          
        mHandler.postDelayed(mRefresh, 5000);
    } else if (tabId.equals("Anzeige2")) {
        // for the other two tabs cancel the update if you get to them
        System.out.println("Anzeige2"); 
        mHandler.removeCallbacks(mRefresh);
    } else if (tabId.equals"Anzeige3")) {
        System.out.println("Anzeige2");
        mHandler.removeCallbacks(mRefresh);
    }
}
});

Also, you should use .equals() to test strings.

user
  • 86,916
  • 18
  • 197
  • 190
  • Thank you verry much Luksprog, it is almost how i expected. If i tab in, the data is getting refreshed, after the delayed time it is refreshed another time, but then nothing more is happening. How can i get it to update data until i switch tab to another one – TheOneAnswer Mar 20 '13 at 10:47
  • Since i cant edit my comment anylonger i post another one, should i make an infinite loop as said in the links i posted for continuous updates? – TheOneAnswer Mar 20 '13 at 10:55
  • @TheOneAnswer Yeap, sorry about that, the Runnable has to continue posting itself to keep the updating continuing. See my edited answer. – user Mar 20 '13 at 10:57
  • Thanks for the fast answer! I inserted the code u edited, but now i get an error message. underlined is the mRefresh in the mHandler.postDelayed(mRefresh, 5000) Statement in the public void run() and it says, that: "the local variable mRefresh may not have been initialized" also it brings another error, that wasnt there before inserting that line: in the onTabChanged part, the mHandler.removeCallbacks(mRefresh) brings the error, that mRefresh: "Missing code implementation in the compiler" – TheOneAnswer Mar 20 '13 at 11:07
  • @TheOneAnswer Did you make `mHandler` and `mRefresh` `final`? – user Mar 20 '13 at 11:21
  • @Luksprong Yes, they´re both declared final – TheOneAnswer Mar 20 '13 at 13:44
  • @TheOneAnswer My code should work(at least compile). The *Missing code implementation in the compiler* seems more related to the ide or some libraries you use or some other setup, but unfortunately I can't help you with that. – user Mar 20 '13 at 13:58
  • @Luksprong The "Missing code implementation in the compiler" error occures only when i put in the "mHandler.postDelayed(mRefresh, 5000);" Line in the run method, is it possible that i has something to do with my minimum requested api (i had this problem with another issue before)? it is set to 8 – TheOneAnswer Mar 20 '13 at 14:20
  • @TheOneAnswer No, that method is available from API level 1. You could try `mHandler.postDelayed(this, 5000);` but I still don't understand why you have that problem. – user Mar 20 '13 at 15:42
  • 1
    It all works well now with the "this" in the statement! Thank you very much Lukesprong! – TheOneAnswer Mar 21 '13 at 06:49
  • @TheOneAnswer I'm glad it worked out in the end. Strange, the first version should have worked as well. – user Mar 21 '13 at 06:51