Skip to content

Commit

Permalink
The list nows updates when a character is deleted.
Browse files Browse the repository at this point in the history
  • Loading branch information
JayTSmith committed Oct 19, 2017
1 parent 4202de5 commit f8290a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public View onCreateView(LayoutInflater li, ViewGroup vg, Bundle b) {
View view = li.inflate(R.layout.fragment_character_selection, vg, false);

ListView charListView = (ListView) view.findViewById(R.id.character_list_view);
charListView.setAdapter(new CharacterAdapter(getContext(), Portfolio.get().getPortfolio()));
charListView.setAdapter(new CharacterAdapter(getContext()));

charListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,31 @@
*/

public class CharacterAdapter extends BaseAdapter {
private ArrayList<PlayerCharacter> mDataset;
private Portfolio mPortfolio = Portfolio.get();
private Context mContext;

public CharacterAdapter(Context context, ArrayList<PlayerCharacter> dataset) {
public CharacterAdapter(Context context) {
mContext = context;
mDataset = dataset;
}

public void removeCharacter(int i) {
mPortfolio.deletePlayerCharacter(mPortfolio.getPlayerCharacter(i));
notifyDataSetChanged();
}

@Override
public int getCount() {
return mDataset.size();
return mPortfolio.getPortfolio().size();
}

@Override
public PlayerCharacter getItem(int i) {
return mDataset.get(i);
return mPortfolio.getPlayerCharacter(i);
}

@Override
public long getItemId(int i) {
return mDataset.get(i).getCharClass().getResId();
return getItem(i).getCharClass().getResId();
}

@Override
Expand All @@ -50,9 +54,9 @@ public View getView(int i, View view, ViewGroup viewGroup) {
TextView classView = (TextView) view.findViewById(R.id.character_class_view);
TextView levelView = (TextView) view.findViewById(R.id.character_level_view);

nameView.setText(mDataset.get(i).getName());
classView.setText(mDataset.get(i).getCharClass().getResId());
levelView.setText(String.valueOf(mDataset.get(i).getCharClass().getLevel()));
nameView.setText(getItem(i).getName());
classView.setText(getItem(i).getCharClass().getResId());
levelView.setText(String.valueOf(getItem(i).getCharClass().getLevel()));
return view;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ public View onCreateView(LayoutInflater li, ViewGroup vg, Bundle b) {
super.onCreateView(li, vg, b);
View view = li.inflate(R.layout.fragment_character_selection, vg, false);

ListView charListView = (ListView) view.findViewById(R.id.character_list_view);
charListView.setAdapter(new CharacterAdapter(getContext(), Portfolio.get().getPortfolio()));
final ListView charListView = (ListView) view.findViewById(R.id.character_list_view);
charListView.setAdapter(new CharacterAdapter(getContext()));

charListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Portfolio port = Portfolio.get();
if (port.getPortfolio().size() > 1) {
port.deletePlayerCharacter(port.getPlayerCharacter(i));
if (charListView.getAdapter().getCount() > 1) {
((CharacterAdapter) charListView.getAdapter()).removeCharacter(i);
} else {
Toast.makeText(getContext(), R.string.error_delete_character, Toast.LENGTH_SHORT).show();
}
Expand Down

0 comments on commit f8290a2

Please sign in to comment.