Android的 - 不更新GUI onResume [英] Android - Not updating GUI onResume

查看:450
本文介绍了Android的 - 不更新GUI onResume的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为HeaderView我用遍了我的应用程序类:

I have a class called HeaderView which I use all over my application:

public HeaderView(Context context, AttributeSet attrs) {
    super(context, attrs);
    ctx = context;

    commonApi = AAALifestyleApplication.commonApi;
    user = commonApi.getCurrentUser();

    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.header_view, this, true);

    ((ImageView)this.findViewById(R.id.logo)).setOnClickListener(this);

    inboxButton = (ImageView) this.findViewById(R.id.inbox_image);
    inboxButton.setOnClickListener(this);
    inboxButton2 = (ImageView) this.findViewById(R.id.inbox_image2);
    inboxButton2.setOnClickListener(this);

    requestsButton = (ImageView) this.findViewById(R.id.requests_image);
    requestsButton.setOnClickListener(this);
    requestsButton2 = (ImageView) this.findViewById(R.id.requests_image2);
    requestsButton2.setOnClickListener(this);

    progressBar = (ProgressBar) this.findViewById(R.id.header_progress_bar);
    if (!showProgressBar) {
        progressBar.setVisibility(INVISIBLE);
    }
    AAAAsyncTask.setProgressBarListener(this);

    refreshView();
}

public static void refreshView(){
    SharedPreferences sp = ctx.getSharedPreferences("HeaderView", Context.MODE_PRIVATE);
    int newMessages = sp.getInt("newMessagesCount", 0);
    int newRequests = sp.getInt("newRequestsCount", 0);
    if(newMessages > 0){
        Log.d("daim", "new messages!");
        inboxButton.setVisibility(View.GONE);
        inboxButton2.setVisibility(View.VISIBLE);
    }
    else{
        Log.d("daim", "no new messages!");
        inboxButton.setVisibility(View.VISIBLE);
        inboxButton2.setVisibility(View.GONE);
    }
    if(newRequests > 0){
        requestsButton.setVisibility(View.GONE);
        requestsButton2.setVisibility(View.VISIBLE);
    }
    else{
        requestsButton.setVisibility(View.VISIBLE);
        requestsButton2.setVisibility(View.GONE);
    }
}

在我的活动,这HeaderView被调用的OnCreate导致其在XML中指定,以及在onResume()是这样的:

In my Activity, this HeaderView gets called onCreate cause its specified in the XML, and also in onResume() like this:

@Override
public void onResume(){
    super.onResume();
    HeaderView.refreshView();
}

在onCreate()方法这正常工作,但onResume()我得到的消息适量,我甚至记录这一点,所以我肯定知道,没有新邮件被打印出来,但setVisibility()方法不更新所有,而是我看到了previous ImageView的,当我有新邮件。

In the onCreate() method this works correctly, but onResume() I get the right amount of messages and I even logged this, so I know for sure that "no new messages" is printed, but the setVisibility() method doesn't update at all, and instead I see the previous ImageView when I had "new messages".

请帮帮忙,我已经使用处理器与一个线程来看看这是问题试过了,但它仍然是这样。

Please help, I've tried using handler with a thread to see if that was the problem, but it still remains this way.

推荐答案

您应该实例化视图,使 refreshView 非静态的。

You should instantiate your view, and make the refreshView non-static.

您声明活动是这样的:

public class YourActivity extends Activity {
    private HeaderView mHeaderView;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        mHeaderView = new HeaderView(this);
    }

    @Override
    public void onResume(){
        super.onResume();
        mHeaderView.refreshView();
    }
}

和删除在 refreshView 方法声明静态关键字。

And remove the static keyword in your refreshView method declaration.

这篇关于Android的 - 不更新GUI onResume的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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