2015-07-03 89 views
0

我有一個活動,必須顯示AlertDialog與ListView供用戶選擇其中一個選項。當我將AlertDialog的代碼放入onCreate方法時,它可以工作。但是在其他地方,它不會。控制檯上沒有錯誤。AlertDialog不顯示時不創建

這裏是它被稱爲(在活動):

@Override 
public void onStateChanged(IntegratorState state) { 
    switch (state.getState()) { 
    case AWAITING_MENU_OPTION: 
     IntegratorHelper.showOptionsMenu(state, SitefMenuActivity.this).show(); 
     break; 
    default: 
     Toast.makeText(getApplicationContext(), state.getState().name(), 
       Toast.LENGTH_LONG).show(); 
    } 
} 

這裏是我創建對話框(在其他類):

public static AlertDialog showOptionsMenu(IntegratorState state, Context context) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setTitle(state.getGatewayMessageExtra()); 
    String[] strings; 
    strings = state.getGatewayMessage().split(";"); 

    final List<String> options = Arrays.asList(strings); 

    builder.setAdapter(new MenuSaleAdapter(context, 
      android.R.layout.simple_list_item_1, options), 
      new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        if (which == options.size()-1) { 
         // Do a thing 
        } else { 
         // Do other thing 
        } 
       } 
      }); 
    return builder.create(); 
} 

private static class MenuSaleAdapter extends ArrayAdapter<String> { 

    public MenuSaleAdapter(Context context, int textViewResourceId, 
      List<String> objects) { 
     super(context, textViewResourceId, objects); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     TextView view = (TextView) super.getView(position, convertView, 
       parent); 
     view.setTextColor(Color.BLACK); 
     return view; 
    } 
} 

我試圖把所有的代碼在同一個活動中,但它仍然不起作用。該應用程序不凍結或任何東西,它只是不顯示對話框。有任何想法嗎?感謝

+0

1.使用上下文菜單列表視圖+或2.使用彈出菜單。我強烈建議編號2. –

+0

我不認爲這些菜單會做。主要是因爲他們的佈局我想。但我會試一試! –

回答

0

嘗試改變第一部分:

@Override 
    public void onStateChanged(IntegratorState state) { 
    switch (state.getState()) { 
    case AWAITING_MENU_OPTION: 
     IntegratorHelper.showOptionsMenu(state, SitefMenuActivity.this); 
     break; 
    default: 
     Toast.makeText(getApplicationContext(), state.getState().name(), 
       Toast.LENGTH_LONG).show(); 
    } 
} 

和第二部分:

public static void showOptionsMenu(IntegratorState state, Activity activity) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
    builder.setTitle(state.getGatewayMessageExtra()); 
    String[] strings; 
    strings = state.getGatewayMessage().split(";"); 

    final List<String> options = Arrays.asList(strings); 

    builder.setAdapter(new MenuSaleAdapter(activity, 
      android.R.layout.simple_list_item_1, options), 
      new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        if (which == options.size()-1) { 
         // Do a thing 
        } else { 
         // Do other thing 
        } 
       } 
      }); 
    builder.create().show(); 
} 
+0

剛剛嘗試過,它也沒有工作:( –

+0

另一種選擇是使用DialogFragment與原始活動的接口/回調。DialogFragment也處理旋轉比標準對話更好。 –

+0

@ samuel-birocchi - 已更新的答案 –