使用改造到片段中获取数据

时间:2018-09-25 11:37:49

标签: android fragment onclicklistener

*holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // transaction to the targeted fragment from current fragment
            AppCompatActivity activity = (AppCompatActivity) view.getContext();
            Fragment myFragment = new open_order();
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.view_order_frag, myFragment).addToBackStack(null).commit();         
        }
    });*

这是我的适配器类的代码,每当单击某个项目时,我都想打开一个片段,必须根据单击的项目来获取片段中的数据。

我有一个回收站视图,它包含一个订单列表,并假设有3个带有发票编号的订单。 1,2,3分别是我想通过项目单击来打开订单,并获取该被单击项目的片段数据,并且必须根据该发票编号或该项目来获取它,因为我仅使用一个片段来获取数据。

有什么建议吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

如果您要向片段发送数据活动

Bundle bundle = new Bundle();
bundle.putString("KEY", "VALUE");
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);    // set Fragmentclass Arguments

将数据放入片段时:

String str = getArguments().getString("KEY");   

答案 1 :(得分:1)

// Create fragment and give it an argument for the selected article

        ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        // in case your list is of custom objects
        //args.putParcelableArrayList("prgmUri", prgmUri);
        //in case of string arraylist
        //args.putStringArrayList("prgmName", prgmName);
        args.putInt("selected", position);
        newFragment.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();

您可以使用setArguments()将分发包传递给片段。

您可以发送捆绑包中的列表。

在bundle中,您可以将水果(0)和veletable(1)情况下的position值更改为不同。 在片段中检索它时,您可以决定要检索哪个列表。 并使用列表。

在片段内:-

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    String strtext = getArguments().getString("key");    
    return inflater.inflate(R.layout.fragment, container, false);
}

答案 2 :(得分:0)

  

首先,您需要广播,以便您可以发送点击的内容   适配器到您的片段的信息

1-创建BroadcastHelper类别:

 public class BroadcastHelper {
     public static final String BROADCAST_EXTRA_METHOD_NAME = "INPUT_METHOD_CHANGED";
    public static final String ACTION_NAME = "hassan.hossam";
    private static final String UPDATE_LOCATION_METHOD = "updateLocation";


    public static void sendInform(Context context, String method) {
        Intent intent = new Intent();
        intent.setAction(ACTION_NAME);
        intent.putExtra(BROADCAST_EXTRA_METHOD_NAME, method);
        try {
            context.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendInform(Context context, String method, Intent intent) {
        intent.setAction(ACTION_NAME);
        intent.putExtra(BROADCAST_EXTRA_METHOD_NAME, method);
        try {
            context.sendBroadcast(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

2-从适配器发送意图

   holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent url = new Intent("url");
            url ("url_adapter",item.get(position).getURL());
            BroadcastHelper.sendInform(context,"url",url);
        }
    });

3-在您的片段中使用此方法:

 Receiver receiver;
boolean isReciverRegistered = false;


@Override
public void onResume() {
    super.onResume();

    if (receiver == null) {
        receiver = new Receiver();
        IntentFilter filter = new IntentFilter(BroadcastHelper.ACTION_NAME);
        getActivity().registerReceiver(receiver, filter);
        isReciverRegistered = true;
    }
}

@Override
public void onDestroy() {
    if (isReciverRegistered) {
        if (receiver != null)
            getActivity().unregisterReceiver(receiver);
    }
    super.onDestroy();
}


private class Receiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Log.v("r", "receive " + arg1.getStringExtra(BroadcastHelper.BROADCAST_EXTRA_METHOD_NAME));
        String methodName = arg1.getStringExtra(BroadcastHelper.BROADCAST_EXTRA_METHOD_NAME);
        if (methodName != null && methodName.length() > 0) {
            Log.v("receive", methodName);
            switch (methodName) {

                case "url":

                    Toast.makeText(getActivity(), url_adapter , Toast.LENGTH_SHORT).show();
                    break;

                default:
                    break;
            }
        }
    }
}
  

请注意,您可以使用此广播功能,而不仅仅是适配器,例如,您可以使用它来从应用程序中的任何位置通过新更新通知您的主要活动。