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:
- protected override void OnCreate(Bundle bundle)
- {
- //...
- var lv = FindViewById
(Resource.Id.ListView); - lv.Adapter = new ListAdapter(this, Resource.Layout.List, list.CurrentList, Constants.ServerIP);
- }
- public ListAdapter(Context Context, int ListId, List
list, string serverip) : base(Context, ListId, Geraete) - {
- this.List = list;
- }
- public override View GetView(int position, View convertView, ViewGroup parent)
- {
- View v = convertView;
- if (v == null)
- {
- LayoutInflater inflater = (LayoutInflater) Context.GetSystemService(Context.LayoutInflaterService);
- v = inflater.Inflate(Resource.Layout.List, parent, false);
- }
- if (Get_Status(List[position]) == 1)
- {
- v.FindViewById
(Resource.Id.ImageView).SetImageResource(Resource.Drawable.green); - }
- if (Get_Status(List[position]) != 1)
- {
- v.FindViewById
(Resource.Id.ImageView).SetImageResource(Resource.Drawable.red); - }
- return v;
- }
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
- var thread = new Thread(() => MethodToRun(garaet));
- thread.Start();
- void MethodToRun(Geraet geraet)
- {
- while (true)
- {
- Thread.Sleep(2000);
- var a = new Activity();
- a.RunOnUiThread(() => {
- int status = Get_Status(geraet);
- if (status == 1)
- {
- v.FindViewById
(Resource.Id.ImageView).SetImageResource(Resource.Drawable.green); - }
- if (status != 1)
- {
- v.FindViewById
(Resource.Id.ImageView).SetImageResource(Resource.Drawable.red); - }
- });
- }
- }
2.) With a handler:
- handler = new Handler(Looper.MainLooper);
- actions = new Runnable(() =>
- {
- int status = Get_Geraetestatus(Geraetelist[position]);
- handler.Post(() =>
- {
- ImageView display = v.FindViewById
(Resource.Id.ImageView); - led.SetImageResource(status == 1 ? Resource.Drawable.green: Resource.Drawable.red);
- PostAction();
- });
- });
- private void PostAction()
- {
- handler.PostDelayed(() => {
- new Thread(actions).Start();
- }, 2000);
- }
3.) ThreadWithState
- ThreadWithState tws = new ThreadWithState(Serverip, Geraetelist[position]);
- Thread thread = new Thread(new ThreadStart(tws.ThreadProc));
- thread.Start();
-
public class ThreadWithState{
- public void ThreadProc()
- {
- while (true)
- {
- Thread.Sleep(1000);
- Activity a = new Activity();
- a.RunOnUiThread(() => { geraetestatus = Get_Geraetestatus(geraet); });
- }
- }
- }
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

Amit GuptaRank #190 · 12.8K reputationPosted Jan 14, 2019, 9:22 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 11, 2019, 3:02 AM EST
Sam Mettenmeyer117 reputationPosted Jan 11, 2019, 4:29 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 11, 2019, 4:52 AM EST
Sam Mettenmeyer117 reputationPosted Jan 13, 2019, 11:05 PM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 13, 2019, 11:38 PM EST
Sam Mettenmeyer117 reputationPosted Jan 14, 2019, 4:26 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 14, 2019, 4:55 AM EST
Sam Mettenmeyer117 reputationPosted Jan 14, 2019, 5:21 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 14, 2019, 5:32 AM EST
Sam Mettenmeyer117 reputationPosted Jan 14, 2019, 5:35 AM EST
Sam Mettenmeyer117 reputationPosted Jan 15, 2019, 12:08 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 15, 2019, 12:30 AM EST
Sam Mettenmeyer117 reputationPosted Jan 15, 2019, 5:53 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 10, 2019, 6:19 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 10, 2019, 6:22 AM EST
Sam Mettenmeyer117 reputationPosted Jan 10, 2019, 8:59 PM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 10, 2019, 10:26 PM EST
Sam Mettenmeyer117 reputationPosted Jan 11, 2019, 2:40 AM EST
Sam Mettenmeyer117 reputationPosted Jan 16, 2019, 5:49 AM EST
Amit GuptaRank #190 · 12.8K reputationPosted Jan 16, 2019, 7:46 AM EST