Android文件对话框很糟糕

时间:2011-07-04 04:33:38

标签: java android dialog

好吧,所以我正在尝试创建一个文件选择对话框,我得到了它,所以我可以选择该文件,但我想要它,所以当我选择一个目录时,它会移动并列出新目录中的所有文件。 现在,当我点击某个目录时,它只会进入黑屏。当我点击目录时,如何让它列出目录的文件? 这是我到目前为止的代码:

private void loadFileList(){
          try{
             dir.mkdirs();
          }
          catch(SecurityException e){
             Log.e("Error: ", "unable to write on the sd card " + e.toString());
          }
          if(dir.exists()){
             mFileList = dir.list();
          }
          else{
            mFileList= new String[0];
          }
        }

    protected Dialog onCreateDialog(int id){
          Dialog dialog = null;
          AlertDialog.Builder builder = new Builder(this);

          switch(id){
          case DIALOG_LOAD_ROM:
           builder.setTitle("Choose your rom");
           if(mFileList == null){
             dialog = builder.create();
             return dialog;
           }
             builder.setItems(dir.list(), new DialogInterface.OnClickListener(){
               public void onClick(DialogInterface dialog, int which){
                  mChosenFile = dir.list()[which];
                  rom = new File(dir, mChosenFile);
                  if(rom.isDirectory()){
                      Log.e("log:",dir.getAbsolutePath());
                      dir = new File(dir, mChosenFile);
                      Log.e("log:",dir.getAbsolutePath());
                      loadFileList();
                      showDialog(DIALOG_LOAD_ROM);
                  }

                  else{
                      loadFileList();
                      showDialog(DIALOG_LOAD_PATCH);
                  }
               }
              });
          break;


          case DIALOG_LOAD_PATCH:
           builder.setTitle("Choose your patch");
           if(mFileList == null){
             dialog = builder.create();
             return dialog;
           }
             builder.setItems(dir.list(), new DialogInterface.OnClickListener(){
               public void onClick(DialogInterface dialog, int which){
                  mChosenFile = dir.list()[which];
                  File sdcard = Environment.getExternalStorageDirectory();
                  patch = new File(sdcard, mChosenFile);



                    AlertDialog.Builder alert = new AlertDialog.Builder(masterApp);

                    alert.setTitle("New Rom Name");
                    alert.setMessage("Please input a name");

                    // Set an EditText view to get user input 
                    final EditText input = new EditText(masterApp);
                    alert.setView(input);

                    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                       String value = input.getText().toString();
                       File sdcard = Environment.getExternalStorageDirectory();
                       File newRom = new File(sdcard,value);

                       patchROM(rom, patch, newRom);


                       AlertDialog.Builder alert2 = new AlertDialog.Builder(masterApp);

                         alert2.setTitle("Finished");
                         alert2.setMessage("Rom has been patched");

                          alert2.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                 System.exit(0);

                           }
                          });

                        alert2.show();
                      }
                    });

                    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int whichButton) {
                           File sdcard = Environment.getExternalStorageDirectory();
                           File newRom = new File(sdcard,"newrom.gb");

                           patchROM(rom, patch, newRom);

                           AlertDialog.Builder alert2 = new AlertDialog.Builder(masterApp);

                             alert2.setTitle("Finished");
                             alert2.setMessage("Rom has been patched");

                              alert2.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                     System.exit(0);

                               }
                              });

                            alert2.show();
                      }
                    });

                    alert.show();

               }
              });
          break;


          }
          dialog = builder.show();
          return dialog;
         } 

1 个答案:

答案 0 :(得分:0)

查看code.google.com/p/android-file-dialog/源代码。或者,您可以使用它,而不是编写自己的