从另一项活动更新tabhost的ImageView [英] Update tabhost imageView from another activity

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

问题描述

我现在有5个选项卡的tabhost。在其中一个选项卡我有创建的标签时,它通过POST提取数据来显示一个数字的ImageView的。我想知道我是如何从标签的活动之一(说Rate.java)可以调用该方法来更新ImageView的就是在其中一个选项卡。

I currently have a tabhost with 5 tabs. Over one of the tabs I have an ImageView that when the tabs are created it pulls data via POST to display a number. I am wondering how from one of the tab activities (say Rate.java) I could call that method to update that ImageView that is over one of the tabs.

我知道这是不是很具体,但我认为我写的,所以你知道我在说什么。

I know it's not very specific but I think I wrote it so you know what I am talking about.

让我知道,如果你需要的信息了。

Let me know if you require anymore info.

talitore

推荐答案

根据所提供的资料,即马上想到的两个选项是:

Based on the information given, two options that immediately come to mind are:


  • 发送来自标签的活动(例如Rate.java)广播,并有活动举办的ImageView听吧。

  • 创建某种BaseActivity的(延长活动),它需要一个定制的监听器接口与更新方法。让你的标签活动扩展了BaseActivity并与您的ImageView活动实现它。然后,您可以拨打监听update方法从卡活动(实例它们作为BaseActivity沿着听众通过),使活动与在它的ImageView的行为。

  • Send a broadcast from the tab activity (e.g. Rate.java) and have the activity hosting the ImageView listen for it.
  • Create some sort of BaseActivity (extending Activity) that takes a custom Listener interface with an update method. Have your tab activities extend that BaseActivity and the activity with your ImageView implement it. You can then call the update method on the listener from your tab activities (instantiate them as a BaseActivity and pass along the listener) and make the activity with the ImageView act upon it.

//每次请求编辑:

有关广播和接收信息的一个很好的出发点是为 <文档code>广播接收器 。在你的情况下,它可能比较容易在code只是创建它们。

A good starting point for information about broadcasts and receivers is the documentation for the BroadcastReceiver. In your case it's probably easiest to just create them in code.

一个小例子,将包含类似如下:

A minimal example will contain something like the following:

BroadcastSendingActivity:

public class BroadcastSendingActivity extends Activity {

    public static final String UPDATE_IMAGEVIEW = "UPDATE_IMAGEVIEW";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sender);

        Intent i = new Intent();
        i.setAction(UPDATE_IMAGEVIEW);
        sendBroadcast(i);
    }

}

BroadcastReceivingActivity:

public class BroadcastReceivingActivity extends Activity {

    private BroadcastReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.receiver);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver();
    }

    private void registerReceiver() {
        if (mReceiver == null) {
            mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals(BroadcastSendingActivity.UPDATE_IMAGEVIEW)) {
                        // code to update imageview...
                    }
                }
            };
        }
        getApplicationContext().registerReceiver(mReceiver, new IntentFilter(BroadcastSendingActivity.UPDATE_IMAGEVIEW));
    }

    private void unregisterReceiver() {
        if (mReceiver != null) {
            getApplicationContext().unregisterReceiver(mReceiver);
        }
    }

}

请注意,我没有测试code,但我敢肯定,你就可以找出我可能已经犯任何错误。 :)

Note that I did not test the code, but I'm sure you'll be able to figure out any mistakes I might've made. :)

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

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