我想将图像uri从0位置的数组images []传递给其他片段。 这是我的片段我想在postion == 0 传递onclick方法。
public class OrganicAcidFragment extends Fragment {
String[] product_name={"Formic Acid","Acetic Acid","Propionic Acid",};
String[] product_cn={"CH2O2","CH3COOH","C3H602"};
int[] images={R.drawable.formicacid,R.drawable.aceticacidjpg,R.drawable.propionicacid};
ListView list;
String quantity;
String price;
FragmentManager fm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.i_fragment_organic_acid, container, false);
fm=getFragmentManager();
MyAdapter adapter=new MyAdapter(getActivity(),product_name,images,product_cn,quantity,price);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
PurchaseFragment ldf = new PurchaseFragment ();
Bundle args = new Bundle();
args.putString("product_name", product_name[0]);
args.putString("product_cn", product_cn[0]);
ldf.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.content_frame, ldf).commit();
}
if(position==1){
fm.beginTransaction().replace(R.id.content_frame,new OrganicBaseFragment()).addToBackStack("chemicalorganicfragment").commit();
}
if(position==2){
fm.beginTransaction().replace(R.id.content_frame,new OrganicBaseFragment()).addToBackStack("chemicalorganicfragment").commit();
}
}
});
return rootview;
}
}
在此片段中,我想获取该数据:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.i_fragment_purchase, container, false);
String product_names = getArguments().getString("product_name");
String product_cns = getArguments().getString("product_cn");
imagee=(ImageView)rootview.findViewById(R.id.productimage);
cancel= (Button) rootview.findViewById(R.id.cancel);
address= (EditText) rootview.findViewById(R.id.address);
name.setText(product_names);
cn.setText("("+product_cns+")");
return rootview;
}
两个片段都处于相同的活动中。
我该怎么做?
答案 0 :(得分:0)
我认为在运行的活动和片段之间发送数据的最佳和最简单的方法是LocalBroadcast。
Intent intent = new Intent("some_name");
intent.putExtra("some_data_key", "some_data_value");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
这是如何发送本地广播。
对于listen本地广播,您必须在onCreate()(如果侦听器是Activity)或onCreateView()(如果侦听器是片段)或每当您想要接收该广播时注册该广播:
LocalBroadcastManager.getInstance(this/*here is context*/).registerReceiver(someReceiver,
new IntentFilter("some_name"));
这是接收数据的广播接收器:
BroadcastReceiver someReceiver= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String data= intent.getStringExtra("some_data_key");
//Here do whatever you want with data
//It isn't must be a string. If you want to send a object you must make object class implement Parcelable properly.
}
};
最后,不要忘记取消注册此广播接收器。在onDestroy()或你想要的任何地方做。
LocalBroadcastManager.getInstance(this).unregisterReceiver(someReceiver);
答案 1 :(得分:0)
您可以使用bundle将数据从一个片段发送到另一个片段。
//Put the value
YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
//Inflate the fragment
getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();
在另一个片段的onCreateView()中:
//Retrieve the value
String value = getArguments().getString("YourKey");