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.
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);
}