【问题标题】:Xamarin.Android: Automatically refresh the display of an ImageView without calling the whole activity againXamarin.Android:自动刷新 ImageView 的显示,而无需再次调用整个活动
【发布时间】:2019-06-04 00:09:47
【问题描述】:

在我的 Xamarin.Android-App 中,当打开一个特殊页面(活动)时,创建时会显示一个表格。 ListAdapter 定义该表中的某些字段。 我有一个函数可以让我获得一个外部 int。 int 是该表中显示的其他数据之一。 但是这个函数只被调用一次(在创建时)。 如何在特定时间间隔内的运行时多次调用此函数?我希望如果收到的 int 发生变化,表格中 int 的显示也会发生变化,而无需再次调用整个活动。

谁能帮助我并给我一个提示如何做到这一点?那真是太好了!

请让我分享一些代码,以便您更容易理解我:

Resource.Layout.Activity:

<LinearLayout>
    [...]
    <ListView
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ListView" />
</LinearLayout> 

活动:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Activity);
    [...]    
    var lv = FindViewById<ListView>(Resource.Id.ListView);
    lv.Adapter = new ListAdapter(this, Resource.Layout.List, list.CurrentList, Intent.GetStringExtra("ServerIP"));
}

Resource.Layout.List:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"     
android:orientation="horizontal"     
android:layout_width="fill_parent"     
android:layout_height="fill_parent">

[...]

<ImageView         
    android:id="@+id/ImageView" />

</RelativeLayout>

类 ListAdapter : ArrayAdapter

public ListAdapter(Context Context, int ListId, List<Geraet> 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<ImageView>(Resource.Id.ImageView).SetImageResource(Resource.Drawable.green);
    }
    if (Get_Status(List[position]) != 1)
    {
        v.FindViewById<ImageView>(Resource.Id.ImageView).SetImageResource(Resource.Drawable.red);
    }
}

[...]

private int Get_Status(Geraet geraet)
{
    var request = HttpWebRequest.Create(string.Format(Constants.StatusPath, Serverip, Constants.WebservicePort, geraet.Ip));
    request.ContentType = "application/json";
    request.Method = "GET";

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            var content = reader.ReadToEnd();

            var result = System.Text.RegularExpressions.Regex.Replace(content, @"[^0-9]", "");

            if (result == string.Empty)
            {
                return -2;
            }
            else
            {
                return Int32.Parse(result);
            }
        }
    }
}

提前感谢您的回答和最好的问候

【问题讨论】:

    标签: c# android xamarin.android refresh


    【解决方案1】:

    您可以通过System.Threading 完成此操作:

    var thread = new Thread(() => MethodToRun(garaet));
    thread.Start();
    

    OnCreate() 中运行上述代码。在MethodToRun() 你可以做任何你想做的事。例如:

    void MethodToRun(Garaet garaet) {
        while(true) {
            Thread.Sleep(1000);
            Activity.RunOnUiThread((garaet) => { [...] });
        }
    }
    

    在这种情况下,[...] 将在 UI 线程上每隔一秒运行一次。将其替换为处理图像更新的代码。

    【讨论】:

    • 对不起,我又给你写信了。在尝试插入处理图像更新的代码时,他强调 Activity.RunOnUiThread 说“需要对象引用”。请参阅: private void MethodToRun(){ while (true){ Thread.Sleep(1000); Activity.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);}});}
    • @Sam 试试这个:Thread thread = new Thread(new ParameterizedThreadStart(garaet)) 并将 MethodToRun() 更改为 MethodToRun(Garaet garaet)
    • 嗯,现在他在 ParameterizedThreadStart(geraet) 中强调 geraet 说“预期的方法名称”,当我将 geraet 更改为 MethodToRun(geraet) 时它没有效果(同样的错误):(
    • 真的很感谢你的帮助,很抱歉我不得不给你写这么多。将其更改为您更新的代码时,现在他强调 new ParameterizedThreadStart(MethodToRun) 说“没有覆盖 (/of?^^) “MethodToRun”匹配 (/correspond to?^^) 代表“ParameterizedThreadStart” i>”再次为我的新手问题和我糟糕的英语感到抱歉。
    • 现在他再次强调 Activity.RunOnUiThread 说“需要一个对象引用”。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多