【问题标题】:Android - Update ListView not WorkingAndroid - 更新 ListView 不起作用
【发布时间】:2014-07-31 22:01:42
【问题描述】:

我正在尝试在 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);

    }

然后我在另一个活动上编辑此适配器的内容.. 添加更多项目.. 然后我回到这个活动,然后我调用这个方法来刷新列表..但是什么都没有发生..这是我刷新的代码:

@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();

    }

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

【问题讨论】:

  • 你用 onResume() 方法中的 runOnUiThread 杀了我
  • 你能把修改列表的第二个活动的代码贴出来吗?
  • 列表大小正在更新,因为我用 Toast 消息检查它...
  • 能否请您发布适配器CustomList的代码。我相信适配器应该会发生一些事情。

标签: android listview refresh adapter items


【解决方案1】:

我通过自己创建布局来解决问题,动态添加新行和类似的东西......使用布局充气器。

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

还是谢谢 ;)

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 在 onResume 之后没有调用 getView 函数,现在应该怎么办? getView 函数只是在活动开始时被调用,但是当我启动另一个活动并返回第一个活动时.. getView 没有被调用,我确定因为我使用了 Toast 消息......提前谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 2023-04-09
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多