Я хотел бы выполнить обновление на моем Upgrades
, когда я нажимаю определенные Item
на моем ListView
, они обновляются, но затем я должен вернуться к другому Activity
. Когда я изменяю Activity
на предыдущую, я вижу Items
перед обновлением.
Мой заказ Adapter
public class UpgradeAdapter extends ArrayAdapter<Upgrade> {
private List<Upgrade> upgrades;
public UpgradeAdapter(Context context, List<Upgrade> upgrades) {
super(context,0,upgrades);
this.upgrades = upgrades;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Upgrade upgrade = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).
inflate(R.layout.product_list_layout, parent, false);
}
TextView name = convertView.findViewById(R.id.nameTextField);
TextView lvl = convertView.findViewById(R.id.lvlTextField);
TextView modifier = convertView.findViewById(R.id.modifierTextField);
TextView type = convertView.findViewById(R.id.typeTextField);
TextView cost = convertView.findViewById(R.id.costTextField);
name.setText(upgrade.getName());
lvl.setText(upgrade.getLvl() + "");
modifier.setText(upgrade.getModifier() + "");
type.setText(upgrade.getType().toString());
cost.setText(upgrade.getCost() + "");
return convertView;
}
}
Обработка события клика, выполнение notifyDataSetChanged()
LIST_VIEW.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Upgrade upgrade = (Upgrade)LIST_VIEW.getAdapter().getItem(position);
if(upgrade.getCost() > player.getMoney()) {
LIST_VIEW.getChildAt(position).setEnabled(false);
} else {
player.buyUpgrade(upgrade);
upgrade.setName(upgrade.getName());
upgrade.setCost(upgrade.getCost() * 10);
upgrade.setLvl(upgrade.getLvl() + 1);
upgrade.setModifier(upgrade.getModifier() + 1);
mockedUpgrades.set(position,upgrade);
LIST_VIEW.setAdapter(new UpgradeAdapter(StoreActivity.this, mockedUpgrades));
((BaseAdapter) LIST_VIEW.getAdapter()).notifyDataSetChanged();
}
}
});