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");
}
}
});