【问题标题】:AsyncTask - onPostExecute UI confuseAsyncTask - onPostExecute UI 混淆
【发布时间】:2016-01-24 19:29:52
【问题描述】:

我试图通过搜索找出自己出了什么问题,但我想我错过了其他东西。

我有一个带有 doInBackground 的类,它为我检查一些东西,并将 TRUE 或 FALSE 返回到 onPostExecute。通过调试,我发现代码正在运行,参数通过函数发送并且逐行运行,但 UI 没有在它们必须更新的地方更新。

这是我的代码的一部分:

class check extends AsyncTask<Void,Void,Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {
        //some code here

        if(tmp.equals(SSID)){
            return true;
        } else {           
            //little bit of code
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean is) {
       super.onPostExecute(is);
        if(is){
            Button State = (Button) findViewById(R.id.State);
            State.setBackgroundResource(R.drawable.shape_ringcnt);
        } else {
            Button State = (Button) findViewById(R.id.State);
            State.setBackgroundResource(R.drawable.shape_ringnrdy);
        }
    }
}

我认为我隐藏的代码当然对 UI 没有任何作用,但如果其他一切正常,我可以显示隐藏的部分。

【问题讨论】:

  • 你确定它正在运行代码 onPostExecute
  • 如果是这样,我认为您需要刷新视图
  • 在setbackground之后写State.invalidate()
  • @has19 谢谢!我试过了,但 UI 仍然没有更新。我也检查了代码,我很确定代码在 OnPostExecute 中运行正确
  • 只需将新的 layoutparam 设置为按钮,因为它们在 State.setBackgroundResource 之后来自 xml,我认为它应该可以工作。这样视图应该被刷新

标签: java android asynchronous android-asynctask


【解决方案1】:

我刚刚用 ImageView 更改了 Button,就是这样!我不知道为什么它不适用于 Button,而是使用 ImageView 和更改资源的 URI

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多