Android runOnUiThread not updating UI elements

我是研究僧i 提交于 2019-12-11 12:49:51

问题


Hi I am developing android application in which I am using grid view. I want to updated grid item dynamically.My grid item contains one title text. I tried to do it like this but it is not working for me.

 ((Activity)context).runOnUiThread(new Runnable()
    {
        public void run()
        {
            Debug.print("this is update progress inside thread  ...");

            owner.setText("Uploading...");

            invalidate();
            requestLayout();
        }
    });

So in above code its printing debug statement. But not updating my owner text which is inside grid item.

I tried this also...

 Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            owner.setText("Uploading...");
        }
    };
    handler.sendEmptyMessage(0);

回答1:


Try to refresh your adapter which you used to load the grid item. Either you could use invalidateViews() method or adapter.notifyDataSetChanged()

This will help you to resolve your issue.

Happy coding..




回答2:


So in above code its printing debug statement. But not updating my owner text which is inside grid item.

I think it's textview does not reference to your item in grid adapter. You can check it by set tag when init and getTag() inside runonUIThread method.

Let check this textview is local or global variable. If global, it's only reference to the last item - then check the last item change? OK if it's not. Let post full ur code.




回答3:


runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Debug.print("Updating UI ...");
        owner.setText("Uploading ...");
        invalidate();
        requestLayout();
    }
});


来源:https://stackoverflow.com/questions/29466552/android-runonuithread-not-updating-ui-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!