开发者

Android file dialog woes

开发者 https://www.devze.com 2023-03-17 14:35 出处:网络
Alright, so I\'m trying to make a file choice dialog, and I got it so I can choose the file, but I want it so when I choose a directory it moves and lists all the files in the new directory.

Alright, so I'm trying to make a file choice dialog, and I got it so I can choose the file, but I want it so when I choose a directory it moves and lists all the files in the new directory. Right now it just goes to a black screen when I hit a directory. How do I make it list the directory's files when I hit a directory? This is the code I got so far:

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;
         } 


Take a look at the code.google.com/p/android-file-dialog/ source code. Alternatively, you can just use it, not writing your own

0

精彩评论

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