0

I've been trying to get an id from a listview-onclicklistener to three tabbed fragments. The user firstly clicks in myTicketsFragmentand then goes to the detail page which contains 3 swipeable tabs. These views are 'hosted' by one individual activity named TicketActivity. So currently I've succesfully passed data from the fragment to TicketActivity but I cannot go further than that. Been searching for 2 hours now and still no results..

Here's my code:

myTicketsFragment: passing the data in setOnItemClickListener to tab activity

public void onItemClick(AdapterView<?> parentView,
                        View childView, int position, long id) {

                    Bundle bundle = new Bundle();
                    bundle.putInt("ticketId", myTickets.get(position).getId());
                    Intent ticketDetail = new Intent(getActivity(), TicketActivity.class);
                    ticketDetail.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    ticketDetail.putExtras(bundle);
                    startActivity(ticketDetail);
}

TicketActivity: receiving data and passing it through to the 3 tabs

private ViewPager viewPager;
private TicketTabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Info", "Intern", "Extern" };

public TicketInfoFragment ticketInfoFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);

    // Receive data
    Bundle bundle = getIntent().getExtras();
    int ticketId = bundle.getInt("ticketId");

            // Pass data to fragments
            // ...

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TicketTabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

Example of a tab fragment

public class TicketInfoFragment extends Fragment {

TicketFull ticket = new TicketFull();
private DatabaseHelper db;
int ticketId;
String androidId;
String authCode;
String platform_url;
int uId;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    db = new DatabaseHelper(getActivity());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_ticket_info, container, false);

    return rootView;
}
}

I would be pleased if anyone could help me out

Thanks in advance

Niels
  • 659
  • 2
  • 7
  • 15

3 Answers3

5

2 quick ways:

  1. use an static method in your activity to retrieve current ticket id

  2. Design and implement an interface and register the fragments as listeners from the activity

First option, In your activity:

    private static int ticketId;

    public static int getCurrentTicketId(){
         return ticketId;
    }

and in your fragment you can do:

TickerActivity.getCurrentTicketId();

Second option, Use an interface:

public interface TicketListener{
    public void onTicketChanged(int newTicket);
}

and in your activity add:

public List<TicketListener> listeners = new ArrayList<TicketListener>();

public void addListener(TicketListener listener){
    listeners.add(listener);
}

and register every fragment as a new listener

YourFragment frag = new YourFragment();
addListener(frag);

and finally when you want to notify the key to the listeners iterate over the list:

for(TicketListener listener : listeners){
    listener.onTicketChanged(ticket);
}
Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
  • Seems like first option is easiest but I still don't understand how to use it. If I set the getCurrectTicketId in TicketActivity.java how do I call it in the fragment? – Niels Mar 18 '14 at 12:58
2

You can do in two different ways. The more simple is in your Activity container has to provider a getter for the data that you want to access from the Fragments, so in your fragments has to access to this getter via getActivity to get a reference to the father and the invoke the get method, I mean:

In TicketActivity:

private int ticketId;

@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Receive data
Bundle bundle = getIntent().getExtras();
ticketId = bundle.getInt("ticketId");
...
}

public int getTicketId() {
    return ticketId;
}

And in your fragments:

((TicketActivity)getActivity()).getTicketId();

Or a more elegant way, is passing the Bundle via arguments when you initialize your fragments. You will have to do this inside your TicketTabsPagerAdapter class. I mean something like that:

TicketInfoFragment f = new TicketInfoFragment();
f.setArguments(bundle);

To do this last method is better use the Singleton pattern. You can follow the next link: http://developer.android.com/reference/android/app/Fragment.html

public static class DetailsFragment extends Fragment {
/**
 * Create a new instance of DetailsFragment, initialized to
 * show the text at 'index'.
 */
public static DetailsFragment newInstance(int index) {
    DetailsFragment f = new DetailsFragment();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);

    return f;
}

public int getShownIndex() {
    return getArguments().getInt("index", 0);
}
icastell
  • 3,495
  • 1
  • 19
  • 27
1

it is very simple. When you will pass a bundle from ActivityA to other ActivityB(with bundle). it will be received by the ActivityB class instead of fragment.

It's simple to implement: in onResume method of ActivityB ==> receive the bundle and passes attach it to your required fragment. check my code to pass from onresume to Framgent class

String tag = Constants.TAG_Search;
Fragment fragment;
fragment = fragmentManager.findFragmentByTag(tag);
fragmentTransaction.remove(fragment);
Search searchFragment = new Search();
searchFragment.setArguments(Globals.bd);
fragmentTransaction.add(R.id.tab2, searchFragment, tag);

Hope it will help

Attiq ur Rehman
  • 475
  • 1
  • 6
  • 21