填充此数组列表的内容是什么?

时间:2012-05-28 17:54:53

标签: java android arraylist

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

看看这段代码:

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

当活动开始时,selectedFriends.getPosition()将返回空白。

当我点击按钮时,我会执行此操作:

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

这是填充(由新职位),但我不明白为什么。我引用了一些我不应该提到的东西吗?我是否覆盖内存中的数据?

编辑:在我从ListView中选择一些朋友后会发生这种情况。 toggleSelected()在内部ArrayList中添加或删除项目。

1 个答案:

答案 0 :(得分:1)

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

oldPositions = selectedFriends.getPositions();

实际上它只是制作了一份参考文献 - 它并没有制作整个清单的浅层副本。

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

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