Sam Mettenmeyer

Sam Mettenmeyer

  • NA
  • 108
  • 11.2k

Update/refresh field within a certain time interval

Jan 10 2019 3:09 AM
Hello altogether,
 
In my Xamarin.Android-App, when opening a special page (activity), there is a table displayed on create. A ListAdapter defines the columns within that table. I have a function (Get_Status) which gets me an external int. The int is amongst other data displayed in each row in that table, but differs in it's value (depending on the received data). But this function is only called once per row (on create). How can I call this function multible time during runtime within a certain time interval? I want that if the received int changes, the display of the int in the table changes as well without calling the whole activity again, so just automatically update/refresh its display field every, lets say, 2 seconds.
 
Can anyone just give me a hint how to do this?
 
Please see my current code, maybe it's easier to help me.
 
Activity: 
  1. protected override void OnCreate(Bundle bundle)  
  2. {  
  3.     //...  
  4.     var lv = FindViewById (Resource.Id.ListView);  
  5.     lv.Adapter = new ListAdapter(this, Resource.Layout.List, list.CurrentList, Constants.ServerIP);  
  6. }  
 class ListAdapter : ArrayAdapter
  1. public ListAdapter(Context Context, int ListId, List list, string serverip) : base(Context, ListId, Geraete)  
  2. {   
  3.     this.List = list;   
  4. }  
  1. public override View GetView(int position, View convertView, ViewGroup parent)  
  2. {  
  3.     View v = convertView;  
  4.     if (v == null)  
  5.     {  
  6.         LayoutInflater inflater = (LayoutInflater) Context.GetSystemService(Context.LayoutInflaterService);  
  7.         v = inflater.Inflate(Resource.Layout.List, parent, false);  
  8.     }  
  9.     if (Get_Status(List[position]) == 1)  
  10.     {  
  11.         v.FindViewById(Resource.Id.ImageView).SetImageResource(Resource.Drawable.green);  
  12.     }  
  13.     if (Get_Status(List[position]) != 1)  
  14.     {  
  15.         v.FindViewById(Resource.Id.ImageView).SetImageResource(Resource.Drawable.red);  
  16.     }
  17.     return v;  
  18. }  
I tried several technics myself, unfortunately without success. I will share my efforts as well, maybe you see instantly where the failure was.

1.) using System.Threading
  1. var thread = new Thread(() => MethodToRun(garaet));  
  2. thread.Start();  
  1. void MethodToRun(Geraet geraet)  
  2. {  
  3.     while (true)  
  4.     {  
  5.         Thread.Sleep(2000);  
  6.         var a = new Activity();  
  7.         a.RunOnUiThread(() => {    
  8.             int status = Get_Status(geraet);  
  9.             if (status == 1)  
  10.             {  
  11.                 v.FindViewById(Resource.Id.ImageView).SetImageResource(Resource.Drawable.green);  
  12.             }  
  13.             if (status != 1)  
  14.             {  
  15.                 v.FindViewById(Resource.Id.ImageView).SetImageResource(Resource.Drawable.red);  
  16.             }  
  17.         });  
  18.     }  
  19. }  
 Received error message (MethodToRun Line 6): Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
 
 2.) With a handler: 
  1. handler = new Handler(Looper.MainLooper);  
  2.   
  3. actions = new Runnable(() =>  
  4. {  
  5.     int status = Get_Geraetestatus(Geraetelist[position]);  
  6.   
  7.     handler.Post(() =>  
  8.     {  
  9.         ImageView display = v.FindViewById(Resource.Id.ImageView);    
  10.         led.SetImageResource(status == 1 ? Resource.Drawable.green: Resource.Drawable.red);    
  11.         PostAction();  
  12.     });  
  13.   
  14. });  
  1. private void PostAction()  
  2. {  
  3.     handler.PostDelayed(() => {  
  4.         new Thread(actions).Start();  
  5.     }, 2000);  
  6. }  
Received error message: none (but it doesn't work either, the display is missing completely, debug mode showed that he skips completely everything from line 3 [actions = new Runnable] and jumps directly to return v;)
 
3.)  ThreadWithState
  1. ThreadWithState tws = new ThreadWithState(Serverip, Geraetelist[position]);  
  2. Thread thread = new Thread(new ThreadStart(tws.ThreadProc));
  3. thread.Start();  
  1. public class ThreadWithState
    {

  2.    public void ThreadProc()  
  3.    {  
  4.       while (true)  
  5.       {  
  6.         Thread.Sleep(1000);  
  7.         Activity a = new Activity();  
  8.         a.RunOnUiThread(() => { geraetestatus = Get_Geraetestatus(geraet); });  
  9.       }  
  10.    }
  11. }  
 Received error message (Thread Parameter Line 2): Could not convert "System.Threading.ThreadStart" to "Java.Lang.IRunnable" (Although in my instruction it's nearly the same code: https://docs.microsoft.com/de-de/dotnet/standard/threading/creating-threads-and-passing-data-at-start-time )
 
 Hoping that anyone can help me. Thanks in advance for answers and best regards

Answers (21)