在Adaptor Android Studio中使用全局变量。

huangapple go评论73阅读模式
英文:

Use global variables in an Adaptor Android Studio

问题

我想在我的HomePlayerAdapter中使用我的GlobalState类。当我点击ListView中的Switch时,它们应该将我的Player对象的ID保存在GlobalState中。但是,当我点击Switch时,我的应用程序崩溃了。

这是我的GlobalState类:

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);
    }
}

这是我的HomePlayerAdapter类:

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;
    }
}

当我点击Switch时,它们会调用deckSwitch.setOnCheckedChangeListener并检查开关是否被选中。如果被选中,它应该将所选播放器的ID保存在GlobalState类中,如果未被选中,则应删除ID。

英文:

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&lt;Integer&gt; selectedPlayer;
public ArrayList&lt;Integer&gt; 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&lt;Player&gt; items;
private GlobalState globalState;
public HomePlayerAdapter(Context context, ArrayList&lt;Player&gt; 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) -&gt; {
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.

答案1

得分: 0

代码片段中提到的问题在于您尚未创建 'selectedPlayer' ArrayList。如果您遇到了 nullPointerException,可以检查一下。

另外,我建议您不要创建像这样的全局状态类,而是创建一个类来保存与上下文无关的信息,并声明静态成员变量和方法。这样,您就不必为了使用它而实例化类。既然您打算将 selectedPlayer 用作全局变量,最好将其声明为静态变量,以防止重复实例化。

至于解决这种特殊情况的问题,只需执行以下操作:

private ArrayList<Integer> selectedPlayer = new ArrayList<>();
英文:

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&lt;Integer&gt; selectedPlayer = new ArrayList&lt;&gt;();

should be enough.

huangapple
  • 本文由 发表于 2020年8月6日 16:45:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63279943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定