You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用runOnUiThread刷新ListView无效,求技术解决方案

Fixing ListView Refresh Issues with runOnUiThread()

Hey there! Let's break down why your ListView isn't updating even when using runOnUiThread()—I remember hitting similar roadblocks when I first started Android development, so let's work through this step by step.

Common Causes & Fixes

  • Your Adapter isn't linked to the updated dataset
    You're cloning arrayList_1 into temp1, but if your ListView's Adapter is still referencing the original arrayList_1, changes to temp1 won't show up. You need to either update the Adapter's existing data source or replace it and notify the Adapter of changes.
    Example fix:

    run = new Runnable() { 
        public void run() { 
            ArrayList<String> temp1 = (ArrayList<String>) arrayList_1.clone();
            // Make your changes to temp1 here
            
            // Update the adapter with the new data
            if (yourAdapter != null) {
                yourAdapter.clear();
                yourAdapter.addAll(temp1);
                yourAdapter.notifyDataSetChanged(); // Critical to trigger refresh
            }
        }
    };
    
  • The Runnable might not be executing at all
    It's easy to accidentally skip the code that calls runOnUiThread(run)—maybe a conditional check fails, or a background task doesn't complete. Add a log statement to confirm the Runnable is running:

    run = new Runnable() { 
        public void run() { 
            Log.d("ListViewRefresh", "Runnable is executing!"); // Check logcat for this line
            ArrayList<String> temp1 = (ArrayList<String>) arrayList_1.clone();
            // Rest of your code
        }
    };
    
  • Double-check ListView & Adapter initialization
    Make sure you've properly set up your ListView and Adapter in onCreate() (or wherever you initialize them). If you forget to call listView.setAdapter(yourAdapter), or if yourAdapter is null when you try to update it, nothing will happen.
    Example initialization:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView listView = findViewById(R.id.your_listview_id);
        arrayList_1 = new ArrayList<>();
        yourAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList_1);
        listView.setAdapter(yourAdapter); // Don't skip this line!
    }
    
  • Stop cloning if you don't need to
    Cloning the list is unnecessary if you can update the original dataset directly (as long as you do it on the UI thread). A simpler approach is to modify arrayList_1 inside the Runnable and then notify the Adapter:

    run = new Runnable() { 
        public void run() { 
            arrayList_1.clear();
            arrayList_1.addAll(yourNewData); // Replace with your actual data
            yourAdapter.notifyDataSetChanged();
        }
    };
    
  • Don't re-create the Adapter every time
    If you're creating a new Adapter instance inside the Runnable instead of reusing the one you set on the ListView, the old Adapter will still be attached—so no updates will show. Always reuse the same Adapter object.


内容的提问来源于stack exchange,提问作者nisser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:46:53