gpt4 book ai didi

java - 什么填充了这个数组列表?

转载 作者:行者123 更新时间:2023-11-30 11:44:26 25 4
gpt4 key购买 nike

我的数组列表以某种方式填充。

看这段代码:

final ListView list = getListView();
boolean threadNeeded = true;
//settings
list.setItemsCanFocus(false);
Bundle extras = getIntent().getExtras();
allFriends = getIntent().getParcelableExtra("shibby.whisper.allFriends");
selectedFriends = getIntent().getParcelableExtra("shibby.whisper.selectedFriends");
System.out.println("JUST ENTERED ACTIVITY : "+selectedFriends.getPositions());

if(extras !=null) {
maxPlayers = extras.getInt("max_players");
}

if(allFriends != null){
if(!allFriends.getIds().isEmpty()){
threadNeeded = false;
}
}

if(selectedFriends == null){
System.out.println("selectedFriends is null before callback from ListActivity");
selectedFriends = new PlayerList();
}

list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FacebookFriendsAdapter a = (FacebookFriendsAdapter) list.getAdapter();
//select item
boolean isSelected = a.toggleSelected(new Integer(position));

//if reached maximum players and have selected a new user
if(((maxPlayers+1 == a.getMaxPlayersCounter() && maxPlayers != 0) && isSelected)
|| (maxPlayers == 0 && a.getMaxPlayersCounter() == maxAvailablePlayers+1)){
Log.e("addfriends","Max Players Reached");
a.toggleSelected(new Integer(position));
final Dialog dialog = new Dialog(FacebookFriendsListActivity.this, R.style.CustomDialogTheme );
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.maxplayers);
dialog.setCancelable(true);
// set the custom dialog components - text, image and button
Button dialogButton = (Button) dialog.findViewById(R.id.max_players_dismiss_btn);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
});

m_friends = new ArrayList<Player>();
this.m_adapter = new FacebookFriendsAdapter(this, R.layout.facebbok_friends_item, m_friends);

//set the selected friends on the adapter to remember earlier picks
if(selectedFriends != null){
oldPositions = selectedFriends.getPositions();
m_adapter.setSelectedIds(selectedFriends.getPositions());
m_adapter.setMaxPlayersCounter(selectedFriends.getIds().size());
System.out.println("Max player selected Is : "+maxPlayers+" And Selected Players Counter Is :" +m_adapter.getMaxPlayersCounter());
}

Activity 开始时,selectedFriends.getPosition() 返回空值。

当我点击一个按钮时,我会执行以下操作:

if(oldPositions != null){
System.out.println("This is old positions : " + oldPositions);
}

其中已填充(按新位置),但我不明白为什么。我引用了我不应该引用的内容吗?我是否覆盖内存中的数据?

编辑: 这发生在我从 ListView 中选择一些 friend 之后。 toggleSelected() 在内部 ArrayList 中添加或删除项目。

最佳答案

我认为你的错误是假设这一行复制了 getPositions 返回的任何内容:

oldPositions = selectedFriends.getPositions();

实际上它只复制引用 - 它不会复制整个列表。

你可能想要这样的东西(我只是在猜测类型):

oldPositions = new ArrayList<Position>(selectedFriends.getPositions());

关于java - 什么填充了这个数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com