Prashant singh
Prashant singh

Reputation: 113

fetching data using retrofit into the Fragment

*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();         
        }
    });*

This is code from my adapter class and i want to open a fragment whenever an item is clicked, the data in fragment must be fetched according to what item is clicked.

I have a Recycler View and it contains a list of orders and suppose there are 3 orders with invoice no. 1,2,3 respectively i want open an order by an item click and fetch data in fragment for that clicked item an it must be fetched according to that invoice no or that item as i am using only one fragment for feched data.

Any Suggestions ?

Thank you !

Upvotes: 0

Views: 366

Answers (3)

hossam scott
hossam scott

Reputation: 478

First of all you will need broadcast so you can send your clicked info from adapter to your fragment

1- CREATE BroadcastHelper CLASS :

 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- Send intent from your adapter

   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- in your fragment this use :

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

note you can use This broadcast for anything not just adapter for example you can use it to notify your main activty with new updates from anywhere in your app.

Upvotes: 1

Kaveri
Kaveri

Reputation: 1088

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

Your can use the setArguments() to pass the bundle to the fragment .

Either u can send the list in the bundle.

Or

Inside the bundle you can change the value of position to be different for fruits(0) and veletable(1) case. On retrieving it in fragment , you could decide which list to retrieve. And use the list.

Inside the fragment :-

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

Upvotes: 1

Abhinav Gupta
Abhinav Gupta

Reputation: 2265

If you are sending data activity to fragment :

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

while you are getting data into fragment :

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

Upvotes: 1

Related Questions