0
votes

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

2 Answers

0
votes

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

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