在android中,如何将一个活动中的数组数据发送到片段?
我正在构建一个android应用程序,用户可以在其中选择自己喜欢的东西 当用户单击物品的图像时,物品的名称会添加到数组中 现在我想知道如何将该数组的值解析为任何片段,并在微调器列表中显示它 例如:用户通过单击相应的图像来选择移动和平板电脑,然后将这些值添加到数组名称“stuffarray”中。现在,我想通过“提交”按钮将此数组传递给我的片段,当我单击片段的微调器时,它应该在列表中包含移动和平板电脑值 以下是我选择素材的代码:在android中,如何将一个活动中的数组数据发送到片段?,android,arrays,android-fragments,Android,Arrays,Android Fragments,我正在构建一个android应用程序,用户可以在其中选择自己喜欢的东西 当用户单击物品的图像时,物品的名称会添加到数组中 现在我想知道如何将该数组的值解析为任何片段,并在微调器列表中显示它 例如:用户通过单击相应的图像来选择移动和平板电脑,然后将这些值添加到数组名称“stuffarray”中。现在,我想通过“提交”按钮将此数组传递给我的片段,当我单击片段的微调器时,它应该在列表中包含移动和平板电脑值 以下是我选择素材的代码: submite = (ImageButton) findViewByI
submite = (ImageButton) findViewById(R.id.nextscreen);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent innext = new Intent(getApplicationContext(), MainActivitytabnew.class);
startActivity(innext);
});
img1 = (ImageButton) findViewById(R.id.imageButton1);
img1.setBackgroundResource(R.drawable.mobile);
img1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isClicked1=!isClicked1;
if (isClicked1) {
img1.setImageResource(R.drawable.mobile);
start();
stuff1 = "mobile";
myList.add(stuff1);
}else {
img1.setImageResource(R.drawable.mobile);
myList.remove(sport1);
//sport1 = "";
txt1.setText("");
}
}
});
img2 = (ImageButton) findViewById(R.id.imageButton2);
img2.setBackgroundResource(R.drawable.tablet);
img2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isClicked2=!isClicked2;
if (isClicked2) {
img2.setImageResource(R.drawable.tablet);
start();
stuff2 = "tablet";
myList.add(stuff2);
}else {
img2.setImageResource(R.drawable.tablet);
// sport2 = "";
myList.remove(sport2);
}
}
});
使用
及
你必须在这里使用
从活动或实例化片段或进行事务的位置:
private void putList(List<String> list) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("myList", new ArrayList<>(list));
Fragment fragment = new MyFragment();
fragment.setArguments(bundle);
}
private void putList(列表){
Bundle=新Bundle();
bundle.putStringArrayList(“myList”,新的ArrayList(list));
Fragment Fragment=新的MyFragment();
fragment.setArguments(bundle);
}
在每个片段中:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<String> list = getArguments().getStringArrayList("myList");
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
List List=getArguments().getStringArrayList(“myList”);
}
您可以这样做
碎片。按片段访问活动方法
ActivityName activityName=(ActivityName)getActivity();
if(activityName!=null){
activityName.MethodOfActivityName();
}
在活动中,按活动访问片段方法
fragmentObject.MethodName();
在我的例子中,我只是使用intent将字符串数组从活动共享到片段。这对我很管用 活跃
Intent send=new Intent(getApplicationContext(),FragmentActivity.class);
send.putExtra("array",TouristPlaces);
在onCreateView下的片段中
Intent in=getActivity().getIntent();
String [] ReceivedTouristPlaces=in.getStringArrayExtra("array");
注意:在我的活动中,我使用了列表视图来满足我的需求,单击它会将数据发送到fragment,fragment有一个选项卡。您也可以按活动调用fragment的方法,或者调用activity-fragment。@KrunalIndrodiya我怎么做?@HiteshMatnani请检查我给出的答案。如果这里提到了任何问题。@KrunalIndrodiya我提到了一个错误,请从这里开始。。谢谢你!!我正在使用您的答案,但在PutCharSequenceArrayListRa中它显示错误-类型意图中的方法PutCharSequenceArrayListRa(字符串,ArrayList)不适用于参数(字符串,ArrayList)抱歉,请使用PutStringArrayListRa(标记,数组);相反,当运行应用程序时,它不幸停止了。我的应用程序不幸停止了。在实现List=getArguments()时出现错误。getStringArrayList(“myList”)@HiteshMatnani,把你的错误公布出来吧。代码很好。当运行应用程序时,它不幸停止了我正在这样编码-next.setOnClickListener(new OnClickListener(){@Override public void onClick(视图arg0){{{Intent innext=new Intent(getApplicationContext(),MainActivitytabnew.class);Bundle=new Bundle();Bundle.putStringArrayList(“my”,myList);FragmentPlayo fragment=new FragmentPlayo();fragment.setArguments(bundle);startActivity(innext);};@HiteshMatnani,因为您做得不对。问题是如何将数据从“活动”传递到片段。但您启动了另一个“活动”。请附加更多源。对不起,您的代码我无法理解您的ans代码,我正在解析数组的值-list@HiteshMatnani,您好,您必须在method.Mean MethodOfActivityName中解析arrayList(arrayList)是方法名称。我的数组列表名称是myArray。我的活动名称是MainActivity,片段名称是fragmentA,我想在其中获取数据。您现在可以告诉我根据您的method@HiteshMatnani很抱歉给您带来不便。MainActivity activityName=(MainActivity)getActivity();if(activityName!=null){activityName.MethodOfActivityName(myArray);}以及如何获取值??
Intent send=new Intent(getApplicationContext(),FragmentActivity.class);
send.putExtra("array",TouristPlaces);
Intent in=getActivity().getIntent();
String [] ReceivedTouristPlaces=in.getStringArrayExtra("array");