Home >Java >javaTutorial >How to Implement Communication Between a Fragment and a Custom Cursor Adapter?

How to Implement Communication Between a Fragment and a Custom Cursor Adapter?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-16 07:29:02417browse

How to Implement Communication Between a Fragment and a Custom Cursor Adapter?

Interfacing Fragment and Adapter

Introduction:
Creating a communication channel between a fragment and a custom cursor adapter can facilitate seamless data exchange and event handling between these components.

Problem:
A fragment contains a ListView associated with a cursor adapter. The adapter features a button in each list row with an onClick listener. The goal is to notify the fragment when this button is pressed.

Solution:

  1. Define an Interface in the Adapter:

    • Create an AdapterInterface within the adapter class with a method buttonPressed().
  2. Implement the Interface in the Fragment:

    • In the fragment class, implement the AdapterInterface and override the buttonPressed() method.
  3. Pass the Fragment as an Argument to the Adapter:

    • Add a constructor to the adapter class that accepts an AdapterInterface object.
    • When instantiating the adapter, pass the fragment (which implements AdapterInterface) as an argument.
  4. Invoke the Interface from the Adapter's OnClickListener:

    • Inside the adapter's bindView() method, set the onClick listener for the button.
    • Within the listener, call the buttonPressed() method of the passed-in fragment.

Example Code:

public class MyListAdapter extends CursorAdapter {

    public interface AdapterInterface {
        public void buttonPressed();
    }

    private AdapterInterface buttonListener;

    public MyListAdapter (Context context, Cursor c, int flags, AdapterInterface buttonListener) {
        super(context,c,flags);
        this.buttonListener = buttonListener;
    }

    @Override
    public void bindView(final View view, final Context context, final Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();

        ...

        holder.button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonListener.buttonPressed();
            }
        });
    }
}

public MyListFragment extends Fragment implements AdapterInterface {

    @Override
    public void buttonPressed() {
        // some action
    }
}
  1. Usage:

    • Create an instance of the adapter, passing the fragment as an argument:

      MyListAdapter adapter = new MyListAdapter (getActivity(), myCursor, myFlags, this);
  2. Caution for Orientation Changes:

    • Note that fragment recreation on orientation changes can lead to a lingering adapter reference to a non-existent fragment. To prevent this, consider recreating the adapter as well.

The above is the detailed content of How to Implement Communication Between a Fragment and a Custom Cursor Adapter?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn