0

Currently I have Project A (as Android Library) and Project B.

In Project B I am using page fragment for the design. At page fragment 1, the fragment is actually called from Project A (the library).

How do i receive the onclick value on Project A to Project B.

// Declare the fragment page on Project B

CustFragment fragment1 = new CustFragment ();

// onclick listener on Project A

mContactList.setOnItemClickListener(new OnItemClickListener() {

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

            Cursor cursor = (Cursor) parent.getAdapter().getItem(position);

            long rawId = cursor.getLong(cursor
                    .getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));

// how do i pass the value to B?

        }

EDITED: I edited this post with more code from my side

// Declare the fragment on Project B

public class MainPagerFragmentAdapter extends FragmentPagerAdapter {

ListFragment list;
SearchFragment search;  
public MainPagerFragmentAdapter(FragmentManager fragmentManager) {
    super(fragmentManager);
}

@Override
public int getCount() {
    return 2;
}

public String getPageTitle(int position) {
    switch (position) {
        case 0: return AppSingleton.getCurrentActivity().getString(R.string.pager_appointments_tab);
        case 1: return  AppSingleton.getCurrentActivity().getString(R.string.explist_filter_text);
    }
}

@Override
public Fragment getItem(int position) {

    switch (position) {             
        case 0:
            if (customerList == null)
                list = new ListFragment();
            return customerList;
        case 1:
            if (contactSearch == null)
                search = new SearchFragment();
            return contactSearch;
    }

    return null;
}

}

// onclick listener on Project A (library project)

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

    super.onCreateView(inflater, container, savedInstanceState);
    log.info("ContactsFragment onCreateView");
    mContactFragmentView = inflater.inflate(R.layout.fragment_contacts,
            container, false);

    mContactList = (ListView) mContactFragmentView
            .findViewById(R.id.contacts_list);

    mContactList.setOnItemClickListener(new OnItemClickListener() {

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

            Cursor cursor = (Cursor) parent.getAdapter().getItem(position);

            long rawId = cursor.getLong(cursor
                    .getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));

            // what have to done here to pass back to B dynamically

        }

    });
    getLoaderManager().initLoader(0, null, this);
    return mContactFragmentView;
}
  • java.lang.NullPointerException at com.contact.library.ContactsFragment$2.onItemClick(ContactsFragment.java:143) at android.widget.AdapterView.performItemClick(AdapterView.java:308) at android.widget.AbsListView.performItemClick(AbsListView.java:1509) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3467) at android.widget.AbsListView$3.run(AbsListView.java:4830) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)
Then
  • 1
  • 4

2 Answers2

0

You want to set up a callback triggered on Fragment A. Similar to this:

create an interface in your library:

public interface MyInterface {
  public void onRawIdAcquired(long rawId);
}

Create an instance of it somewhere in your FragmentA (and make a setter that can set its value):

public class FragmentA extends Fragment {
...
MyInterface i;
...
public void setListener(MyInterface i) {
  this.i = i;
}

then call it when rowId gets the value:

long rawId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
i.onRawIdAcquired(rawId);

Now in your PageFragment in your non-library activty, implement MyInterface and override onRawIdAcquired:

public PageFragment extends Fragment implements MyInterface {
@Override
public void onRawIdAcquired(long rawId) {
  **// you can now use the value of rawId here.**
}

Finally, somewhere when instantiating your FragmentA, pass PageFragment in setListener:

FragmentA f = ...;
PageFragment myPageFragment = ...;
f.setListener(myPageFragment);
josephus
  • 8,284
  • 1
  • 37
  • 57
  • Project A is a library, i want to make it dynamic so any other project could when click on the Item could return back to Project B without big modification of code – Then Aug 15 '14 at 03:02
  • maybe i didn't understand your question. do you want to get hold of the OnClickListener object, or just the value of rawID? – josephus Aug 15 '14 at 03:04
  • my idea is to have the value of rawID from Project A pass to Project B – Then Aug 15 '14 at 03:22
  • Ah. then you need some kind of callback. I'll update my answer. – josephus Aug 15 '14 at 03:27
  • thanks for the suggestion, but when i implements the MyInterface in non-library project it gives me error – Then Aug 15 '14 at 06:48
  • the IDE will ask me to create MyInterface in the non-library project – Then Aug 15 '14 at 06:56
  • other than that, when i click on the list item it will generate NullPointerException error when calling the interface method – Then Aug 15 '14 at 06:59
  • that's weird. can you post the exact error, maybe a screenshot? – josephus Aug 15 '14 at 07:01
  • hi, i had updated my question with more code and the error. Could one of the reason is i somehow miss out the last part of your code, cause i not sure which part should implement it – Then Aug 15 '14 at 07:42
  • right now i can implements MyInterface on the PagerAdapater but when execute i.onRawIdAcquired(rawId); it give error – Then Aug 15 '14 at 08:00
0

In your library project, create global variable that variable call from your fragment. If you want to easy way, you should try this way. Just my idea. Hope for help. Android main project with library project - how to pass setting between projects

Community
  • 1
  • 1
B M
  • 1,863
  • 1
  • 25
  • 40
  • hi, i am trying to use Interface to solve the problem but it give me nullpointerexception when i call the interface method – Then Aug 20 '14 at 06:02
  • This is global variable using sample. http://stackoverflow.com/a/708317/3247356 OR http://stackoverflow.com/a/1945297/3247356 – B M Aug 20 '14 at 06:08