运行OnUiThread(运行) 刷新 ListView 无效

ListView refreshing by runOnUiThread(run) not working

所以我对 Android Studio 和一般的应用程序开发还很陌生,我遇到这个问题有一段时间了,但没能解决。我认为这里有人可能会提供一些解决此问题的想法.. 问题:UI 线程上的 ListView 刷新(如建议 here)对我不起作用。这是 'Runnable run' 的声明(在 MainActivity.java 中):

        public Runnable run;
        // ...
        run = new Runnable() {
        public void run() {
            ArrayList<String> temp1 = (ArrayList<String>) arrayList_1.clone();

            View v = getLayoutInflater().inflate(R.layout.fragment_main, null);

            lv_1 = (ListView) v.findViewById(R.id.listViewMe);

            lv_1.setAdapter(adapter_1);

            arrayList_1.clear();

            arrayList_1.addAll(temp1);

            adapter_1.notifyDataSetChanged();

            lv_1.invalidateViews();
            lv_1.refreshDrawableState();

            Log.e("gig", "DONE REFRESHING");
        }
    };

我这里调用方法:

@Override
public boolean onNavigationItemSelected(MenuItem item)
{
    int id = item.getItemId();

    Fragment fragment = null;

    if (id == R.id.nav_main) {
        fragment = new FragmentMain();
    } if (id == R.id.nav_history) {
        fragment = new FragmentHistory();
    }

    if (fragment != null)
    {
        FragmentTransaction localFragmentTransaction = getSupportFragmentManager().beginTransaction();
        localFragmentTransaction.replace(R.id.screen_area, fragment);
        localFragmentTransaction.commit();

        runOnUiThread(run); // here
    }

    ((DrawerLayout) findViewById(R.id.drawer_layout)).closeDrawer(GravityCompat.START);
    return true;
}

现在让我感到困惑的部分是:当我通过此方法向 ListView 添加新项目时它起作用了:

public void addDebtToOther(String name, String money)
{
    String full = name + ", " + money + " EUR";

    lv_1 = (ListView) findViewById(R.id.listViewMe);

    if (lv_1!=null) {
        lv_1.setAdapter(adapter_1);

        arrayList_1.add(full);
        adapter_1.notifyDataSetChanged();
    } else {
        Log.e("gig", "ListView error, lv is NULL");
    }
}

仅此而已,任何帮助都将非常有用!

run object 中,一个新的 View 被夸大了,但据我所知,它没有被添加到 ActivitycontentView 或它的任何一个 children,所以它只是内存中某个地方的 View,没有被渲染到屏幕上。

你是不是想做这样的事情?:

        public Runnable run;
        // ...
        run = new Runnable() {
        public void run() {
            ArrayList<String> temp1 = (ArrayList<String>) arrayList_1.clone();

            // View v = getLayoutInflater().inflate(R.layout.fragment_main, null);
            // lv_1 = (ListView) v.findViewById(R.id.listViewMe);
            lv_1 = (ListView) findViewById(R.id.listViewMe);

            lv_1.setAdapter(adapter_1);

            arrayList_1.clear();

            arrayList_1.addAll(temp1);

            adapter_1.notifyDataSetChanged();

            lv_1.invalidateViews();
            lv_1.refreshDrawableState();

            Log.e("gig", "DONE REFRESHING");
        }
    };

好的,在 head-banging 大约一周后,在@Eric 的帮助下,我已经弄明白了。结果我需要在片段的 .onStart() 中调用 run() 方法,如下所示:

    @Override
    public void onStart() {
    super.onStart();

    ((MainActivity)getContext()).run.run();
    }

再次感谢埃里克!