简体   繁体   English

如何在Android中更新ListView?

[英]How to update ListView in Android?

I want to upgrade my ListView on clicks on it's elements with updated amount of buildings (and price in future). 我想通过单击具有更新的建筑物数量(以及将来的价格)的元素来升级ListView。 Now, the list only updated when app are re-launched. 现在,该列表仅在重新启动应用程序时更新。

onCreate: onCreate:

Building[] buildings = {
                new Building(1, getString(R.string.mouse), 15, iMouses),
                new Building(2, getString(R.string.grandma), 100, iGrandmas),
                new Building(3, getString(R.string.farm), 500, iFarms),
                new Building(4, getString(R.string.factory), 3000, iFactories),
                new Building(5, getString(R.string.mine), 10000, iMines)
        };

        ListView lstBuildings = (ListView)findViewById(R.id.lstBuildings);

        final ArrayAdapter<Building> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, buildings);

        lstBuildings.setAdapter(adapter);

        lstBuildings.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id){
                if (id+1 == 1) { //         Mouse
                    if ((int) fCats >= 15) {
                        fIncome = fIncome + 0.1f;
                        fCats = fCats - 15;
                        iMouses = iMouses + 1;
                        iBuildings = iBuildings + 1;

                        adapter.notifyDataSetChanged();
                    }

My building class: 我的建筑课:

class Building {
    private final int id;
    private final String name;
    private final double price;
    private final int number;

    public Building(int id, String name, double price, int number) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
        this.number = number;
    }

    @Override
    public String toString() {
        return this.id + ". " + this.name + " [" + (int)this.price + "] (" + this.number + ")";
    }
}

Thanks for any help and please give me some tricks for optimization for my code. 感谢您的帮助,请给我一些优化代码的技巧。

The solution is pretty simple: 解决方案非常简单:

adapter.notifyDataSetChanged()

Look at this: 看这个:

if (id+1 == 1) { //         Mouse
                if ((int) fCats >= 15)
                    fIncome = fIncome + 0.1f;
                    fCats = fCats - 15;
                    iMouses = iMouses + 1;
                    iBuildings = iBuildings + 1;

                    adapter.notifyDataSetChanged();
                }

you are simply not updating the data of the adapter you are just setting some variables 您只是不更新​​适配器的数据,而只是设置一些变量

First, I see you update the variable iMouses, but you do not update the location in the buildings array where you stored iMouses previously. 首先,我看到您更新了变量iMouses,但是没有更新以前在建筑物中存储iMouses的位置。 So the data in the buildings array has not changed. 因此建筑物数组中的数据未更改。 It must change in the array as that is what is displayed in the listview. 它必须在数组中更改,因为这就是列表视图中显示的内容。

Also, you will need to implement the following code to get notifyDataSetChanged to function. 另外,您将需要实现以下代码来使notifyDataSetChanged起作用。 The data in the adapter is a different set that your buildings array. 适配器中的数据与建筑物阵列的数据不同。

adapter.clear();
adapter.addAll(buildings);
notifyOnDataSetChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM