0

Is there a way to register an onClick event for a listView item without sending an intent through startActivity, but instead just send and event using EventBus.

@Override
    public void onClickListener(AdapterView<?> av,View v, int position, long id){
        cursor.moveToPosition(position);
        String st = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
        Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
        Log.d("item slected",st);
        MusicPlayerEvent playerevent = new MusicPlayerEvent(uri);
        EventBus.getDefault().post(playerevent);
    }

Fragment:

public class MusicPlayer extends Fragment {
    MediaPlayer mp;
    public MusicPlayer() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        mp = new MediaPlayer();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_music_player, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
    }

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

    @Override
    public void onDetach() {
        EventBus.getDefault().unregister(this);
        super.onDetach();
    }
    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(Uri uri);
    }
    public void onEvent(MusicPlayerEvent playerevent){
        try {
            mp.setDataSource(this.getActivity(), playerevent.uri);
            mp.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mp.start();
    }

}

event class:

public class MusicPlayerEvent {
    Uri uri;
    MusicPlayerEvent(Uri uri){
        this.uri = uri;
    }
}

Many issues here i'm sure but i'd like to at least get an idea of whether what i'm trying to do is possible and perhaps a pointer to some relevant examples or reading. It might be the case that i should get more familiar with regular intents before i try to work with EventBus, but if i can learn the more flexible option first that would be nice.

spintron
  • 107
  • 10
  • I see no errors in code. Except one thing. You probably want to register/unregister EventBus in opposite methods like onCreate/onDestroy or onAttach/onDetach depending on what you are trying to accomplish. And that's it. When you post event it siply finds onEvent(EventType) in your subscribers and call it. As long as you work in UI thread no issues will appear. – Fedor Kazakov Nov 06 '14 at 11:07
  • @Fedor Kazakov thanks for the reply i think it should work except my onClickListener "does not override method from it's superclass" – spintron Nov 07 '14 at 08:52

1 Answers1

0

I should have posted the rest of my activity. I think this would have been easier to answer if i had. I will not make that mistake in the future. I am using a list activity.. as such the proper method to us is onListItemClick.

@Override
    public void onListItemClick(ListView parent,View v, int position, long id){
        cursor.moveToPosition(position);
        String st = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
        Uri uri = Uri.parse(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
        Log.d("item slected",st);
        MusicPlayerEvent playerevent = new MusicPlayerEvent(uri);
        EventBus.getDefault().post(playerevent);
    }
spintron
  • 107
  • 10