Java 为什么我的ArrayList会在片段事件中添加元素本身?
在我的问题中我是如何说的,我有一个带有复选框列表和所选项目的ArrayList(它们是复选框的标签)的片段。当触发复选框事件时,片段调用主活动中接口的方法,但在其中,项选择列表中已包含项,尽管在整个代码中没有任何方法可以向其添加项 代码如下: 主要活动:Java 为什么我的ArrayList会在片段事件中添加元素本身?,java,android,android-fragments,arraylist,Java,Android,Android Fragments,Arraylist,在我的问题中我是如何说的,我有一个带有复选框列表和所选项目的ArrayList(它们是复选框的标签)的片段。当触发复选框事件时,片段调用主活动中接口的方法,但在其中,项选择列表中已包含项,尽管在整个代码中没有任何方法可以向其添加项 代码如下: 主要活动: package com.fp.testfragment1; import java.util.ArrayList; import android.app.Activity; import android.app.FragmentManage
package com.fp.testfragment1;
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import com.fp.testfragment1.MultiChoiceFragment.OnMultiSelectedListener;
public class MainActivity extends Activity implements OnMultiSelectedListener
{
FrameLayout frameLayout1;
FrameLayout frameLayout2;
Button transitionButton;
FragmentManager fragmentManager;
MultiChoiceFragment multiChoiceFragment;
Boolean replace;
ArrayList<String> list;
ArrayList<String> mylis;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<String>();
mylis = new ArrayList<String>();
list.add("item1");
list.add("item2");
list.add("item3");
list.add("item4");
list.add("item5");
transitionButton = (Button)findViewById(R.id.transitionButton);
transitionButton.setOnClickListener(transitionListener);
frameLayout1 = (FrameLayout)findViewById(R.id.frameLayout1);
frameLayout1 = (FrameLayout)findViewById(R.id.frameLayout2);
fragmentManager = getFragmentManager();
}
@Override
protected void onStart()
{
super.onStart();
replace = false;
}
OnClickListener transitionListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
multiChoiceFragment = new MultiChoiceFragment(MainActivity.this, list, mylis, 8);
fragmentTransaction.setCustomAnimations(R.animator.card_flip_right_in, R.animator.card_flip_right_out, R.animator.card_flip_left_in, R.animator.card_flip_left_out);
if(replace)
{
fragmentTransaction.replace(R.id.frameLayout1, multiChoiceFragment);
}
else
{
fragmentTransaction.add(R.id.frameLayout1, multiChoiceFragment);
replace = true;
}
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
};
@Override
public void onGroupMCheckListener(String itemName, Boolean checked, int nf)
{
if(nf == 8)
{
//here i have the list already full
// this is for debug
mylis = mylis;
}
}
}
package com.fp.testfragment1;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.app.FragmentManager;
导入android.app.FragmentTransaction;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.FrameLayout;
导入com.fp.testfragment1.MultiChoiceFragment.OnMultiSelectedListener;
公共类MainActivity扩展了MultiSelectedListener上的活动实现
{
框架布局框架布局1;
框架布局框架布局2;
按钮转换按钮;
碎片管理器碎片管理器;
多发性骨折多发性骨折;
布尔替换;
数组列表;
阵列式mylis;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
列表=新的ArrayList();
mylis=新的ArrayList();
列表。添加(“第1项”);
列表。添加(“第2项”);
列表。添加(“第3项”);
列表。添加(“第4项”);
列表。添加(“第5项”);
transitionButton=(Button)findViewById(R.id.transitionButton);
setOnClickListener(transitionListener);
frameLayout1=(FrameLayout)findViewById(R.id.frameLayout1);
frameLayout1=(FrameLayout)findViewById(R.id.frameLayout2);
fragmentManager=getFragmentManager();
}
@凌驾
受保护的void onStart()
{
super.onStart();
替换=假;
}
OnClickListener transitionListener=新的OnClickListener()
{
@凌驾
公共void onClick(视图v)
{
FragmentTransaction FragmentTransaction=fragmentManager.beginTransaction();
multiChoiceFragment=新的multiChoiceFragment(MainActivity.this,list,mylis,8);
fragmentTransaction.setCustomAnimations(R.animator.card\u flip\u right\u in,R.animator.card\u flip\u right\u out,R.animator.card\u flip\u left\u in,R.animator.card\u flip\u left\u out);
如果(更换)
{
fragmentTransaction.replace(R.id.frameLayout1,multiChoiceFragment);
}
其他的
{
fragmentTransaction.add(R.id.frameLayout1,multiChoiceFragment);
替换=真;
}
fragmentTransaction.addToBackStack(空);
fragmentTransaction.commit();
}
};
@凌驾
public void onGroupMCheckListener(字符串itemName,布尔值选中,int-nf)
{
如果(nf==8)
{
//这张单子已经满了
//这是调试用的
迈利斯=迈利斯;
}
}
}
多选碎片:
package com.fp.testfragment1;
import java.util.ArrayList;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MultiChoiceFragment extends Fragment
{
Context context;
OnMultiSelectedListener mCallback;
LinearLayout groupsListLinearLayout;
TableRow congiungiTableRow;
TextView groupsTextView;
ArrayList<String> groups;
ArrayList<String> groupsChoosen;
int nFragment;
public MultiChoiceFragment(Context context, ArrayList<String> groups, ArrayList<String> groupsChoosen, int nFragment)
{
this.context = context;
this.groups = new ArrayList<String>();
this.groupsChoosen = new ArrayList<String>();
this.groups = groups;
this.groupsChoosen = groupsChoosen;
this.nFragment = nFragment;
}
public MultiChoiceFragment()
{
groups = new ArrayList<String>();
groupsChoosen = new ArrayList<String>();
}
public interface OnMultiSelectedListener
{
public void onGroupMCheckListener(String itemName, Boolean checked, int nf);
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
mCallback = (OnMultiSelectedListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + " must implement OnMultiSelectedListener");
}
context = activity;
}
@Override
public void onSaveInstanceState(Bundle outState)
{
outState.putStringArrayList("groups", groups);
outState.putStringArrayList("groupsChoosen", groupsChoosen);
outState.putInt("nFragment", nFragment);
super.onSaveInstanceState(outState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.choice_fragment, container, false);
groupsListLinearLayout = (LinearLayout)v.findViewById(R.id.groupsLinearLayout);
groupsTextView = (TextView)v.findViewById(R.id.groupsTextView);
congiungiTableRow = (TableRow)v.findViewById(R.id.congiungiTableRow);
if(savedInstanceState != null)
{
groups = savedInstanceState.getStringArrayList("groups");
groupsChoosen = savedInstanceState.getStringArrayList("groupsChoosen");
nFragment = savedInstanceState.getInt("nFragment");
}
refreshListGUI();
return v;
}
public void refreshListGUI()
{
for(int a = 0; a < groups.size(); a++)
{
CheckBox checkBox = new CheckBox(context);
checkBox.setId(a);
if(groupsChoosen == null || groupsChoosen.isEmpty() == true)
{
checkBox.setChecked(false);
}
else if(groupsChoosen.isEmpty() == false && groupsChoosen.contains(groups.get(a)))
{
checkBox.setChecked(true);
}
checkBox.setText((String)groups.get(a));
checkBox.setOnCheckedChangeListener(checkBoxMItemListener);
groupsListLinearLayout.addView(checkBox);
}
}
OnCheckedChangeListener checkBoxMItemListener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
String groupC = buttonView.getText().toString();
if(isChecked == true && groupsChoosen.contains(groupC) == false)
{
groupsChoosen.add(groupC);
mCallback.onGroupMCheckListener(groupC, isChecked, nFragment);
}
else if(isChecked == false && groupsChoosen.contains(groupC) == true)
{
groupsChoosen.remove(groupC);
mCallback.onGroupMCheckListener(groupC, isChecked, nFragment);
}
}
};
}
package com.fp.testfragment1;
导入java.util.ArrayList;
导入android.app.Activity;
导入android.app.Fragment;
导入android.content.Context;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.CheckBox;
导入android.widget.CompoundButton;
导入android.widget.CompoundButton.OnCheckedChangeListener;
导入android.widget.LinearLayout;
导入android.widget.TableRow;
导入android.widget.TextView;
公共类MultiChoiceFragment扩展片段
{
语境;
OnMultiSelectedListener McCallback;
线性布局组线性布局;
TableRow CongiungingTableRow;
文本视图组文本视图;
ArrayList群;
arraylistgroupschoosen;
内部碎片;
公共MultiChoiceFragment(上下文上下文、ArrayList组、ArrayList组Schoosen、int-nFragment)
{
this.context=上下文;
this.groups=new ArrayList();
this.groupsChoosen=新的ArrayList();
这个组=组;
this.groupsChoosen=groupsChoosen;
this.nFragment=nFragment;
}
公共多选择碎片()
{
groups=newarraylist();
groupsChoosen=newarraylist();
}
MultiSelectedListener上的公共接口
{
public void onGroupMCheckListener(String itemName,Boolean checked,int nf);
}
@凌驾
公共事务主任(活动)
{
超级转速计(活动);
尝试
{
mCallback=(OnMultiSelectedListener)活动;
}
catch(ClassCastException e)
{
抛出新的ClassCastException(activity.toString()+“必须在MultiSelectedListener上实现”);
}
上下文=活动;
}
@凌驾
SaveInstanceState上的公共无效(束超出状态)
{
outState.putStringArrayList(“组”,组);
outState.putStringArrayList(“groupsChoosen”,groupsChoosen);
outState.putInt(“nffragment”,nffragment);
super.onSaveInstanceState(超出状态);
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
{
视图v=充气机。充气(R.layout.choice_碎片,容器,错误);
groupsListLinearLayout=(LinearLayout)v.findViewById(R.id.groupsLinearLayout);
groupsTextView=(TextView)v.findviewbyd(R.id.groupsTextView);
congiungiTableRow=(TableRow)v.findViewById(R.id.congiungiTableRow);
如果(savedInstanceState!=null)
{
groups=savedInstanceState.getStringArrayList(“组”);
groupsChoosen=savedInstanceState.getStringArrayList(“groupsChoosen”);
nFragment=sa