Android - 更新ListView无法正常工作

时间:2014-07-31 22:01:42

标签: android listview refresh adapter items

我正在尝试在Android上更新ListView。我在列表中插入新项目,然后调用notifyDataSetChanged(),没有任何反应!我正在使用runOnUIThread,它应该正常工作吗?

这是我的代码:

    @Override

//this is where I create the ListView with my custom adapter..

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Globals.getInstance().readWarehouse(this);
            Toast.makeText(this, "É:" + Globals.getInstance().getWarehouse().getJointNames().length , Toast.LENGTH_LONG).show();
            setContentView(R.layout.activity_main);
            adapter = new CustomList(MainActivity.this, Globals.getInstance().getWarehouse());
            list = (ListView) findViewById(R.id.list);
            list.setAdapter(adapter);

    }

然后我在另一个活动上编辑此adaptar的内容..添加更多项目.. 然后我回到这个活动,然后我调用这个方法刷新列表..但没有任何事情发生..这是我刷新的代码:

@Override
    protected void onResume(){
        super.onResume();

        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                ((BaseAdapter) list.getAdapter()).notifyDataSetChanged();
            }
        });

        Toast.makeText(this, "onResume triggered.. and list not refreshing.. even after the call made before to notify.." , Toast.LENGTH_LONG).show();

    }

有人能帮助我吗?非常感谢!

2 个答案:

答案 0 :(得分:0)

我通过自动动态添加新行和类似的东西来创建布局解决了这个问题..使用Layout Inflater。

这不是最好的解决方案,但我知道我可以完全控制布局:)

非常感谢;)

答案 1 :(得分:-1)

检查getView之后是否调用了适配器(CustomList)onResume函数。 如果调用getView函数,请检查Globals.getInstance().getWarehouse()中是否更新了数据。