开发者

How do I display a Dialog on Android app when I long click on an item in a GridView?

开发者 https://www.devze.com 2023-03-17 14:43 出处:网络
I am trying to display a Dialog when I long click on an item in my GridView.I have tried this but it is throwing errors. I don\'t really understand how to use OnItemLongClickListener and why it return

I am trying to display a Dialog when I long click on an item in my GridView. I have tried this but it is throwing errors. I don't really understand how to use OnItemLongClickListener and why it returns a boolean. Can someone please help me understand this more and figure out how to display this Dialog.

 gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
        public boolean onItemLongClick(AdapterView<?> parent, View v,
          int position, long id) {
             FavoriteViewDialog dialog = 
                     new FavoriteViewDialog(FavoriteView.this, null, null);         
            dialog.show();
            return true;
        }
 });

Thank you.

The error it is throwing is ...

Thread [<1> main] (Suspended (exception WindowManager$BadTokenException))   
FavoriteViewDialog(Dialog).show() line: 245 
FavoriteView$2.onItemLongClick(AdapterView, View, int, long) line: 39   
GridView(AbsListView).performLongPress(View, int, long) line: 1753  
AbsListView.access$600(AbsListView, View, int, long) line: 72   
AbsListView$CheckForLongPress.run() line: 1711  
ViewRoot(Handler).handleCallback(Message) line: 587 
ViewRoot(Handler).dispatchMessag开发者_运维百科e(Message) line: 92 
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 4627    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 521  
ZygoteInit$MethodAndArgsCaller.run() line: 868  
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method]  


Try using showDialog(int i) method of Activity to show a dialog: http://developer.android.com/guide/topics/ui/dialogs.html

The best way to define the onCreateDialog(int) and onPrepareDialog(int, Dialog) callback methods is with a switch statement that checks the id parameter that's passed into the method...

static final int DIALOG_PAUSED_ID = 0;
static final int DIALOG_GAMEOVER_ID = 1;

Then, define the onCreateDialog(int) callback with a switch case for each ID:

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
    case DIALOG_PAUSED_ID:
        // do the work to define the pause Dialog
        break;
    case DIALOG_GAMEOVER_ID:
        // do the work to define the game over Dialog
        break;
    default:
        dialog = null;
    }
    return dialog;
}

Note: In this example, there's no code inside the case statements because the procedure for defining your Dialog is outside the scope of this section. See the section below about Creating an AlertDialog, offers code suitable for this example.

Then call showDialog(DIALOG_PAUSED_ID); //or another int representing a Dialog.


Generally the WindowManager$BadTokenException is context related.

Generally it is thrown when you use one context to show a dialog in another context.

And generally this can be avoided by using getApplicationContext for your dialog instead of SomeActivity.this

0

精彩评论

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

关注公众号