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 GuptaPosted Jan 14, 2019, 2:22 PM
Amit GuptaPosted Jan 16, 2019, 12:46 PM
Sam MettenmeyerPosted Jan 16, 2019, 10:49 AM
Sam MettenmeyerPosted Jan 15, 2019, 10:53 AM
Amit GuptaPosted Jan 15, 2019, 5:30 AM
Sam MettenmeyerPosted Jan 15, 2019, 5:08 AM
Sam MettenmeyerPosted Jan 14, 2019, 10:35 AM
Amit GuptaPosted Jan 14, 2019, 10:32 AM
Sam MettenmeyerPosted Jan 14, 2019, 10:21 AM
Amit GuptaPosted Jan 14, 2019, 9:55 AM
Sam MettenmeyerPosted Jan 14, 2019, 9:26 AM
Amit GuptaPosted Jan 14, 2019, 4:38 AM
Sam MettenmeyerPosted Jan 14, 2019, 4:05 AM
Amit GuptaPosted Jan 11, 2019, 9:52 AM
Sam MettenmeyerPosted Jan 11, 2019, 9:29 AM
Amit GuptaPosted Jan 11, 2019, 8:02 AM
Sam MettenmeyerPosted Jan 11, 2019, 7:40 AM
Amit GuptaPosted Jan 11, 2019, 3:26 AM
Sam MettenmeyerPosted Jan 11, 2019, 1:59 AM
Amit GuptaPosted Jan 10, 2019, 11:22 AM
Amit GuptaPosted Jan 10, 2019, 11:19 AM