Search code examples
javaandroid-studioandroid-listviewglobal-variablesandroid-adapter

Use global variables in an Adaptor Android Studio


I want to use my class GlobalState in my HomePlayerAdapter. When I click on a Switch in my ListView they should save the ID from my player object in GlobalState. But when I click on a Switch my App crashed.

Here my GlobalState class:

public class GlobalState extends Application {

    private ArrayList<Integer> selectedPlayer;


    public ArrayList<Integer> getSelectedPlayer() {
        return selectedPlayer;
    }

    public void addSelectedPlayer(int p) {
        this.selectedPlayer.add(p);
    }

    public void removeSelectedPlayer(int p) {
        this.selectedPlayer.remove(p);
    }
}

Here my HomePlayerAdapter class:

public class HomePlayerAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Player> items;

   private GlobalState globalState;


    public HomePlayerAdapter(Context context, ArrayList<Player> items){

        this.context = context;
        this.items = items;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.home_player_list_item, parent, false);
        }

        globalState = (GlobalState) context.getApplicationContext();

        final Player currentItem = (Player) getItem(position);

        TextView playerName = (TextView) convertView.findViewById(R.id.PlayerItemTextView);
        SwitchMaterial deckSwitch = (SwitchMaterial) convertView.findViewById(R.id.PlayerItemSwitch);

        playerName.setText(currentItem.getName());

        deckSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {

            if (isChecked){

                globalState.addSelectedPlayer(currentItem.getId());

            }if (!isChecked){

                globalState.removeSelectedPlayer(currentItem.getId());

            }

        });

        return convertView;
    }

    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

}

When I click on a Switch then they call: deckSwitch.setOnCheckedChangeListener and check if the swith is checked or not. When its checked it should save the ID from the selected player in the GlobalState class and when its not checked then it should delete the ID.


Solution

  • I don't know if you have done this somewhere else in your code, but the code snippet that you provided, the problem lies in the fact that you haven't actually created the 'selectedPlayer' Arraylist. It can be checked if you are getting the nullPointerException.

    although, I would suggest, rather than creating a globalState class like this, Make a class That would hold context Independent information, and declare static member variables, and methods. So that you do not have to instantiate the class for using. Since you are going to use selectedPlayer as a global variable anyway, its better to make it static, to prevent duplicate instantiation.

    As for the solution to your problem for this particular case, just doing

     private ArrayList<Integer> selectedPlayer = new ArrayList<>();
    
    

    should be enough.