TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sam Mettenmeyer
NA
108
11.3k
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:
protected
override
void
OnCreate(Bundle bundle)
{
//...
var lv = FindViewById
(Resource.Id.ListView);
lv.Adapter =
new
ListAdapter(
this
, Resource.Layout.List, list.CurrentList, Constants.ServerIP
);
}
class ListAdapter : ArrayAdapter
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);
}
});
}
}
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:
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);
}
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
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
Reply
Answers (
21
)
C# Windows Form - Serial Port problem / BackgroundWorker
how I stop all threads using manualResetEvent or any other?