通过runOnUiThread(运行)刷新ListView不起作用

时间:2018-03-21 17:45:09

标签: android listview android-studio android-runonuithread

所以我对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");
    }
}

关于它,任何帮助都会非常有用!

2 个答案:

答案 0 :(得分:0)

run对象中,新的View被夸大了,但就我所知,它没有被添加到Activity&#39; contentView或其中任何一个孩子,因此内存中的某个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");
        }
    };

答案 1 :(得分:0)

好的,经过大约一个星期的敲击,在@Eric的帮助下,我已经明白了。事实证明我需要在我的片段run()中调用.onStart()方法,如下所示:

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

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

再次感谢Eric!