Xamarin Android-从另一个活动更新一个活动的UI控件 [英] Xamarin Android - Update UI control of one activity from another activity

查看:358
本文介绍了Xamarin Android-从另一个活动更新一个活动的UI控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有TextView("@ + id/lblAccountHead")的片段"AddNewIncomeFragment".在单击textview时,它会启动一个新活动"AccountHeadListActivit",其中显示了现有帐户头的列表.从"AccountHeadListActivity"中选择帐户头后,我想用所选帐户名更新第一个活动的"lblAccountHead",其他值必须保持不变.

I have one Fragment "AddNewIncomeFragment " with a TextView("@+id/lblAccountHead"). On click on textview it starts a new activity "AccountHeadListActivit" which shows a list of existing Account Head. On selection of account head from "AccountHeadListActivity" i want to update "lblAccountHead" of first activity with selected account name, other values need to be intact.

我以前使用xamarin形式的消息中心"来做到这一点.现在我试图在xamarin native(android)中做同样的事情.

Earlier i did it using "messaging center" for xamarin form. Now i trying to do the same in xamarin native(android).

*****应用第一个解决方案进行更新****

*****UPDATED WITH FIRST SOLUTION APPLIED****

AddNewIncomeFragment中的单击事件,该事件开始帐户头部活动:

  public void onAccounyHeadClick(object sender, EventArgs e)
        {
               var intend = new Intent(this.Activity, typeof(AccountHeadListActivity));
        //this.StartActivity(intend);
        this.StartActivityForResult(intend, 1000);
        }

帐户客户活动的ListView选择事件

void OnSelection (object sender, SelectedItemChangedEventArgs e)
        {

                     var result = new Intent();
        result.PutExtra("name", "Salary Account");
        result.PutExtra("id", 2);
        SetResult(Result.Ok, result);
       Finish();
        }

在关闭此活动时,我想使用选定的帐户标题名称/ID更新上一个活动的textview.请分享我们在xamarin andriod中必须执行的操作,应该使用StartActivityForResult,Local Notifications或任何最佳方法.

On closing this activity i want to update textview of previous activity with selected account head name/id. Please share what option we have to do this in xamarin andriod, should use StartActivityForResult,Local Notifications or any best approach.

我已经实现了上述解决方案,并且工作正常.但是问题是-第二个活动"AccountHeadList"包含一个添加新帐户的链接,该链接将启动一个新活动"AddNewAccount"-现在,如果用户创建了一个新帐户并保存,则该活动需要关闭,而第一个活动需要进行新的更新创建的帐户名.因此,基本上,"StartActivityForResult"在涉及三个活动并需要从第三个活动更新第一个活动时失败-请提出建议.

谢谢

@Paul

推荐答案

要接收结果,请致电 startActivityForResult()而不是startActivity()

To receive a result, call startActivityForResult() instead of startActivity()

然后在第二个活动中,在完成活动finish()之前,调用setResult(result)方法设置结果,然后在原始活动中可以覆盖该方法

then in your second activity, call the setResult(result) method to set the result before you finish the activity finish() after that in your original activity you can override the method

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent     data) {
      // set the text view with the data from the result
  }

这篇关于Xamarin Android-从另一个活动更新一个活动的UI控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆