开发者

I need help making a longpress Context menu on android

Ok, so now after reading I do actually have a menu on long press like I wanted...the only problem is that it doesn't actually get the sound file and save it

I am wondering what did I do wrong now? Here is the code I used:

Button SoundButton1 = (Button) findViewById(R.id.money);  
        registerForContextMenu(SoundButton1);
    }

     @Override  
        public void onCreateContextMe开发者_Go百科nu(ContextMenu menu, View v, 
                ContextMenuInfo menuInfo) { 
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.setHeaderTitle("Save as..."); 
            menu.add(0, MENU_RINGTONE, 0, "Ringtone"); 
            menu.add(0, MENU_NOTIFICATION, 0, "Notification"); 
    } 

     @Override  
        public boolean onContextItemSelected(MenuItem item) {  
            if(item.getTitle()=="Ringtone"){function1(item.getItemId());}  
            else if(item.getTitle()=="Notification"){function2(item.getItemId());}  
            else {return false;}  
        return true;  
        }  

        public void function1(int id){  
            Toast.makeText(this, "Ringtone saved", Toast.LENGTH_SHORT).show();  
        }  
        public void function2(int id){  
            Toast.makeText(this, "Notification saved", Toast.LENGTH_SHORT).show();  
        }


You are not comparing Strings properly. This test:

if(item.getTitle()=="Ringtone")

checks if the object returned by getTitle() is the same object as "Ringtone". This will always be false. Similarly with the else if test. You actually want to compare the value of getTitle with your String. You should replace them with the String#equals() method. Here is the code:

if(item.getTitle().equals("Ringtone")){function1(item.getItemId());}  
else if(item.getTitle().equals("Notification")){function2(item.getItemId());}  
else {return false;}


If you are using a ListFragment then you also need to do

getListView().setOnCreateContextMenuListener(this);

instead of

registerForContextMenu(listView);

it seems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜