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.