Java 如何从一个片段中声明的数组中获取图像uri并将其传递给另一个片段?
我想将图像uri从位于0位置的数组图像[]传递到其他片段。 这是我想在position==0处传入onclick方法的片段Java 如何从一个片段中声明的数组中获取图像uri并将其传递给另一个片段?,java,android,arrays,android-fragments,android-intent,Java,Android,Arrays,Android Fragments,Android Intent,我想将图像uri从位于0位置的数组图像[]传递到其他片段。 这是我想在position==0处传入onclick方法的片段 public class OrganicAcidFragment extends Fragment { String[] product_name={"Formic Acid","Acetic Acid","Propionic Acid",}; String[] product_cn={"CH2O2","CH3COOH","C3H602
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;
}
}
两个片段处于相同的活动中
我该怎么做呢?我认为在正在运行的活动和片段之间发送数据的最好、最简单的方法是本地广播
Intent intent = new Intent("some_name");
intent.putExtra("some_data_key", "some_data_value");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
这是如何发送本地广播的
对于侦听本地广播,您必须在onCreate()(如果侦听器为活动)或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);
您可以使用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");
这里我使用的是片段。它是否适用于从片段发送到片段?是的,它是。在receiver片段中声明BroadcastReceiver(someReceiver),然后在onCreateView()中注册它,仅此而已。在那之后,从任何你想要的地方发送你的本地广播。从任何地方发送本地广播都可以,但我认为您只能在活动和片段中接收它们。也许你可以接受服务,但我不确定。此示例适用于活动,但也适用于片段:我应该在片段中编写什么来代替getApplicationContext()?在片段中使用getContext()。我尝试发送图像uri,但不起作用。如何从数组中传递图像uri?U r尝试传递图像[]数组中包含可抽取的…我说的对吗?请检查此链接,希望它能帮助你,你接受一个好的答案会获得声誉积分
//Retrieve the value
String value = getArguments().getString("YourKey");