0

My problem is just as it says in the description, the context menu is not appearing on a "touch and hold" from the user. I've got a feeling it could be where I placed registerForContextMenu.
Here's my MainActivity:

import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerForContextMenu(getListView());
setContentView(R.layout.activity_main);
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Aggro");
s.setSoundResourceId(R.raw.aggro);
mSounds.add(s);
s = new Sound();
s.setDescription("Basix");
s.setSoundResourceId(R.raw.basix);
mSounds.add(s);
s = new Sound();
s.setDescription("Bender");
s.setSoundResourceId(R.raw.bender);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();

}
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
  }
}
JackWhiteIII
  • 1,388
  • 2
  • 11
  • 25

2 Answers2

1

Try switching these two lines around

registerForContextMenu(getListView());
setContentView(R.layout.activity_main);

so it should be

setContentView(R.layout.activity_main);
registerForContextMenu(getListView());

Your feeling was correct. Your ListView is in your layout so you need to inflate it first before registering the View

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Can I just ask, so I can learn from this, why does that make a such a difference which line comes first? – Jason Mills Aug 06 '13 at 18:42
  • As I said in my last sentence of the answer, your `Views` exist within your `layout` so its like they don't exist until the root `layout` has been inflated. Same reason that if you try to initialize a `View` before calling `setContentView()` it will return `null` and you can't use a method or listener on it – codeMagic Aug 06 '13 at 18:47
  • I hope that makes sense – codeMagic Aug 06 '13 at 18:48
  • Perfect sense yes, I understand now. Thanks. Just wondering, would you be able to have a peek at my other question, its driving me mad. http://stackoverflow.com/questions/18080679/make-soundclip-in-a-listview-selectable-as-notification-ringtone – Jason Mills Aug 07 '13 at 13:40
0

I assume that "touch and hold" from the user means "longpress"?

If so, try looking at this post to see if your issues are similar. Perhaps you need to use setOnLongClickListener...

Community
  • 1
  • 1
MiStr
  • 1,193
  • 10
  • 17