0

I want to upgrade my ListView on clicks on it's elements with updated amount of buildings (and price in future). Now, the list only updated when app are re-launched.

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.

Saibamen
  • 610
  • 3
  • 17
  • 43
  • 1
    possible duplicate of [update listview dynamically with adapter](http://stackoverflow.com/questions/5320358/update-listview-dynamically-with-adapter) – Nickolaus Feb 02 '15 at 20:53
  • 1
    I don't see you updating your adapter. If you're not then it wont update even if you use `notifyDataSetChanged` – Rohit5k2 Feb 02 '15 at 21:02
  • @Rohit5k2: and how to update adapter? – Saibamen Feb 02 '15 at 21:08
  • From your code I don't understand what are you trying to update on click of the item. Without that I can't tell you what you need to do. – Rohit5k2 Feb 02 '15 at 21:08
  • 1
    as a side note, these 2 lines and the subsequent matching indentation `if ((int) fCats >= 15) fIncome = fIncome + 0.1f;` are misleading. – njzk2 Feb 02 '15 at 21:11
  • njzk2: this is copy-paste bug - in my code is if ((int) fCats >= 15 /*&& iMouses == 0*/) { @Rohit5k2: I want to update int number of buildings and add this to the list. I want to do this automatically (for now, the int number in list is updated then I re-run my app). I used: lstBuildings.invalidate(); lstBuildings.setAdapter(adapter); ((BaseAdapter) lstBuildings.getAdapter()).notifyDataSetChanged(); after iBuildings = iBuildings + 1; and the list is not updated – Saibamen Feb 02 '15 at 21:37

2 Answers2

1

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

Nickolaus
  • 4,785
  • 4
  • 38
  • 60
  • I have already adapter.notifyDataSetChanged(); at end of my first copy-paste code and it didn't work – Saibamen Feb 02 '15 at 20:58
1

First, I see you update the variable iMouses, but you do not update the location in the buildings array where you stored iMouses previously. 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. The data in the adapter is a different set that your buildings array.

adapter.clear();
adapter.addAll(buildings);
notifyOnDataSetChanged();
Eric Alberson
  • 1,116
  • 1
  • 11
  • 23
Reenactor Rob
  • 1,508
  • 1
  • 11
  • 20
  • I set: `adapter.clear(); adapter.addAll(buildings); adapter.notifyDataSetChanged();` But now when I tap this option - app crashed :( – Saibamen Feb 03 '15 at 03:47
  • and what did logcat say? – Reenactor Rob Feb 03 '15 at 03:56
  • You can also recreate the adapter after you update the buildings array. Then assign it again to the list view, then call notifyDataSetChanged. ---- adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, buildings);---- lstBuildings.setAdapter(adapter);---- adapter.notifyDataSetChanged(); – Reenactor Rob Feb 03 '15 at 03:58