簡體   English   中英

使用asynctask更新listview

[英]Update listview using asynctask

嘗試從asynctask更新片段中的listview時出現此類錯誤:

The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.

我的AsyncTask類

protected Void doInBackground(Void... params) {
        int nextStep;
        BaseAnt tempAnt = null;
        ArrayList<BaseAnt> antList = new ArrayList<BaseAnt>();
        while (true) {
            Log.d("myLogs", "Start from APP");
            nextStep = RandomNumber.getRandomNumber(3);
            switch (nextStep) {
            case 0:
                tempAnt = new GuardAnt();
                AppStat.addToLog("Guard");
                break;
            case 1:
                tempAnt = new MotherAnt();
                AppStat.addToLog("Mother born");
                break;
            case 2:
                tempAnt = new WorkerAnt();
                AppStat.addToLog("Worker born");
                break;
            default:
                break;
            }
            antList.add(tempAnt);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            for (int i = 0; i < antList.size(); i++)
                antList.get(i).eat();
            publishProgress(String.valueOf(AppStat.WATER));
            if (AppStat.WATER < 0)
                break;
        }
        return null;

    }

    protected void onPostExecute(String result) {

    }

    @Override
    protected void onProgressUpdate(String... values) {
        Log.d("myLogs", "onProgressUpdate");
        LogFragment.mLogAdapter.notifyDataSetChanged();
        LogFragment.mLogAdapter.setSelectToLast();
        StatFragment.tvWater.setText(String.valueOf(AppStat.WATER));
        super.onProgressUpdate(values);
    }

AppStat是應用程序的類擴展,其中包含有關我需要的變量和方法的信息

    public static void addToLog(String str) {
    if (listLog.size() > 256)
        listLog.remove(0);
    listLog.add(getNowtime() + ": " + str);
}

在LogAdapter中擴展BaseAdapter:

    public void setSelectToLast() {
    Log.d("myLogs","UpdateToLast");
    notifyDataSetChanged();
    LogFragment.lvList.setSelection(getCount() + 1);
}

如何解決?

您不應在doInBackground()中調用addToLog()(或任何其他UI操作)。 您可以在doInBackground中收集所有內容,作為結果返回它,然后在onPostExecute中更新列表,或者使用publishProgress()並在onProgressUpdate()中更改列表內容...

onPostExecute和onProgressUpdate在UI線程中執行,doInBackground在另一個線程中執行。

UI更新是通過asynctask中的onpostexecute()方法完成的。 這樣做,並且在doinbackground()完成其工作之后onpostexecute在ui線程上運行時,它將正常工作。

您應該使用以下方法在主線程中運行更新:

MainActivity.this.runOnUiThread(new runnable(){
public void run(){
    Log.d("UI thread", "I am the UI thread");
}

});

您還可以查看這篇文章: Android基礎知識:在UI線程中運行代碼

嘗試使用

MainActivity.this.runOnUiThread(new runnable()
{
  public void run()
  {
    LogFragment.mLogAdapter.notifyDataSetChanged();
    LogFragment.mLogAdapter.setSelectToLast();
    StatFragment.tvWater.setText(String.valueOf(AppStat.WATER));
  }
}

在您的onProgressUpdate方法中

嘗試在onPostExecute中的適配器上調用notifyDataSetChanged()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM