【问题标题】:How take image uri from my array declared in one fragment and pass it to other fragment?如何从我在一个片段中声明的数组中获取图像 uri 并将其传递给另一个片段?
【发布时间】:2017-10-13 00:13:04
【问题描述】:

我想将图像 uri 从我的数组 images[] 的 0 位置传递给其他片段。 这是我的片段,我想在 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;
        }

两个片段都在同一个活动中。

我该怎么做?

【问题讨论】:

标签: java android arrays android-fragments android-intent


【解决方案1】:

我认为在正在运行的活动和片段之间发送数据的最佳和最简单的方法是 LocalBroadcast。

Intent intent = new Intent("some_name");
intent.putExtra("some_data_key", "some_data_value");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);

这是发送本地广播的方法。

要收听本地广播,您必须在 onCreate()(如果侦听器是 Activity)或 onCreateView()(如果侦听器是 Fragment)或您想要接收该广播时注册该广播:

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

【讨论】:

  • 这里我使用的是fragments..它适用于从fragments到fragments的发送吗?
  • 是的。在接收器片段中声明 BroadcastReceiver (someReceiver) 然后在 onCreateView() 中注册它就是这样。之后,从您想要的任何地方发送您的本地广播。从任何地方发送本地广播都可以,但我认为您只能在 Activity 和 Fragment 中接收它们。也许你可以接受服务,但我不确定。此示例适用于活动,但也适用于片段:stackoverflow.com/questions/8802157/…
  • 我在片段中写什么代替 getApplicationContext()?
  • 在片段中使用 getContext()。
【解决方案2】:

您可以使用 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");

【讨论】:

  • 我试过了,但无法发送图像 uri。如何从我的数组中传递图像 uri?
  • 你试图传递包含可绘制对象的 images[] 数组……我说的对吗?
  • 请查看此链接,希望对您有所帮助stackoverflow.com/a/8407644
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多