0

私は ViewPager を持っています。2 番目のページには、Ftp に接続するためのフォームがあります。接続ボタンを押すと、フラグメントが別のフラグメント (ftp リスト フォルダー) に置き換えられます。あるフラグメントから別のフラグメントに FTPClient オブジェクトを渡す方法がわかりません。なにか提案を?

MainActivity.java

public class MainActivity extends FragmentActivity {
  FileAdapter adapter = null;
  int mColor = 0;
  File[] files = null;
  File mCurrentFile = null;
  List<File> lfiles = null;
  public List<FTPFile> lftpFiles = null;
  ViewPager pager = null;
  // ListView lv = null;
  RelativeLayout ftp = null;
  List<Fragment> fragments = null;
  private boolean mCountdown = false;// Se pone a true cuando se le pica una

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpager);
    fragments = new Vector<Fragment>();
    fragments.add(Fragment.instantiate(this, LocalFragment.class.getName()));
    fragments.add(Fragment.instantiate(this, FtpFragment.class.getName()));
    fragments.add(Fragment.instantiate(this, CreditsFragment.class.getName()));

    ViewPagerAdapter mPagerAdapter = new ViewPagerAdapter(
            super.getSupportFragmentManager(), fragments);
    pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(mPagerAdapter);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && pager.getCurrentItem() == 0) {
        File parent = ((LocalFragment) fragments.get(0)).mCurrentFile.getParentFile();
        if (parent != null) {
            ((LocalFragment) fragments.get(0)).updateDirectory(parent);
            ((LocalFragment) fragments.get(0)).mCurrentFile = parent;
            mCountdown = false;
        } else {// estamos a la raiz, desplegamos un toast
            Toast.makeText(getApplicationContext(),"Ya esta a la raiz, pica de nuevo para salir",
                    Toast.LENGTH_SHORT).show();
            if (mCountdown == false) {
                mCountdown = true;
            } else
                finish();

        }
    } else if (keyCode == KeyEvent.KEYCODE_BACK && pager.getCurrentItem() != 0){
        return super.onKeyDown(keyCode, event);
    }
      else{
        return false; // return super.onKeyDown(keyCode, event);
    }
    return true;
}

// pager.setCurrentItem(0, true);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.action_settings:
        Intent i = new Intent(this, ExploradorPreference.class);
        startActivity(i);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

}

FtpFragment.java

public class FtpFragment extends Fragment {
  public FileAdapter adapter;
  Context mContext;
  List<File> lfiles = null;
  File[] files = null;
  File mCurrentFile = null;
  ListView lv = null;
  int mColor = 0;
  String server = null;
  String user = null;
  String pass = null;
  FTPClient ftpClient=null;
  FtpListFragment ftpList = null;
  List<FTPFile> ftpLstDir = null;
  List<FTPFile> ftpLstFiles =null;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getActivity();

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.ftp, container, false);
    Button b = (Button) v.findViewById(R.id.connect);
    EditText txtServer = (EditText) v.findViewById(R.id.TxtServidor);
    EditText txtUser = (EditText) v.findViewById(R.id.TxtUser);
    EditText txtPass = (EditText) v.findViewById(R.id.TxtPass);
    txtServer.setText("ftpperso.free.fr");
    txtUser.setText("fleur.de.lotus");
    txtPass.setText("9sz07jd0");
    server = ((EditText) v.findViewById(R.id.TxtServidor)).getText()
            .toString();
    user = ((EditText) v.findViewById(R.id.TxtUser)).getText().toString();
    pass = ((EditText) v.findViewById(R.id.TxtPass)).getText().toString();
    ftpList = new FtpListFragment();

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (server.equalsIgnoreCase("") || user.equalsIgnoreCase("")
                    || pass.equalsIgnoreCase("")) {
                Toast.makeText(mContext,
                        "A lo menos un campo no esta lleno", Toast.LENGTH_LONG).show();
            } else {
                boolean conectionOk = ftpConnect(server, user, pass);
                if (conectionOk) {
                    Toast.makeText(mContext, "Conexion Succeed", Toast.LENGTH_LONG).show();
                    try {
                         String toppath = new String();
                            FTPFile[] ftpDirs = ftpClient.listDirectories();
                            FTPFile[] ftpdirs2 = ftpClient.listFiles(toppath);
                            ftpLstDir = Arrays.asList(ftpDirs);
                            ftpLstFiles = Arrays.asList(ftpdirs2);

                        }
                            // Remplazar el fragento
                            final FragmentManager fm = getActivity().getSupportFragmentManager();
                            final FragmentTransaction ft = fm.beginTransaction();
                            // We can also animate the changing of fragment
                              ft.setCustomAnimations(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
                            ft.replace(R.id.ftp_fragment_form, ftpList);
                              ft.setTransition(FragmentTransaction.TRANSIT_EXIT_MASK);
                            ft.commit();


                    }catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    Toast.makeText(mContext, "Conexion Failed", Toast.LENGTH_LONG).show();
                }

            }
        }
    });
    return v;
}

public boolean ftpConnect(String server, String loginName, String password) {
    try {
        ftpClient = new FTPClient();
        ftpClient.connect(InetAddress.getByName(server));
        ftpClient.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        ftpClient.login(loginName, password);

        System.out.println("status :: " + ftpClient.getStatus());
        System.out.println("status :: " + ftpClient.getReplyString());
        System.out.println("status :: " + ftpClient.getStatus());
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}


public void onDestroyView() {
    try{
        ftpClient.logout();
        ftpClient.disconnect();
        Log.d("DISCONNECT","Desconectando");
        super.onDestroyView();
    }catch (IOException e){
        System.out.println(e.getMessage());
    }

}

FtpListFragment.java

public class FtpListFragment extends ListFragment {
public FtpFileAdapter adapter;
List<FTPFile> lfiles=null;
Context mContext;
FTPClient ftpClient = null;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getActivity();

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.ftplist, container, false);
        lfiles = new ArrayList<FTPFile>();
        try {
            // I'd like to get my ftpClient object from the FtpFragment !



        }catch (Exception e) {
            e.printStackTrace();
        }

        adapter = new FtpFileAdapter(mContext, lfiles);
        setListAdapter(adapter);    
    return v;
}


}
4

1 に答える 1