我有一个名为HeaderView的类,我在我的应用程序中使用它:
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);
}
}
在我的Activity中,这个HeaderView被调用onCreate导致它在XML中指定,并且在onResume()中也是如此:
@Override
public void onResume(){
super.onResume();
HeaderView.refreshView();
}
在onCreate()方法中,这可以正常工作,但onResume()我得到了正确数量的消息,我甚至记录了这一点,所以我确定打印了“没有新消息”,但是setVisibility()方法根本没有更新,相反,当我收到“新消息”时,我会看到之前的ImageView。
请帮助,我已经尝试使用一个线程处理程序来查看这是否是问题,但它仍然是这样。
答案 0 :(得分:0)
您应该实例化您的视图,并使refreshView
非静态。
像这样声明你的Activity
:
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();
}
}
并删除static
方法声明中的refreshView
关键字。