1

我有一个问题:当我按下显示在ListView我无法打开的文件夹时。我有一个文件夹类和适配器类。这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    path = Environment.getExternalStorageDirectory().getPath();
    file = new File(path);
    folders = new ArrayList<Folders>();
    lv= (ListView) findViewById(R.id.listView1);

    adapter= new MyAdapter(getApplicationContext());
    lv.setAdapter(adapter);
    SetList();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {        
    String  newPath=    adapter.folders.get(pos).getNames()+folders.get(pos).getFiles();

    file = new File(path+"/"+newPath);
        String[] newdirs = file.list();
        folders.clear();
        for (int i = 0; i < newdirs.length; i++) {
            Folders fol = new Folders(newdirs[i], newdirs[i].length()+"itemes");
            adapter.folders.add(fol);
        }
        }
    }); 
}
public void SetList(){
    String[] dirs = file.list();
    for (int i = 0; i < dirs.length; i++) {
        Folders fol= new Folders(dirs[i], dirs[i].length()+" files");
        adapter.folders.add(fol);
4

2 回答 2

1

这可能会帮助您:

保存文件的方法:

 public void saveFile(String data, Context context, String filepath, String filename){

            ContextWrapper contextWrapper = new ContextWrapper(context);
            File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
            File myInternalFile = new File(directory , filename);

            try {
                FileOutputStream fos = new FileOutputStream(myInternalFile);

                fos.write(data.getBytes());
                fos.close();

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

        }

读取文件的方法:

public String readFile(Context context, String filepath, String filename) throws JSONException{

            ContextWrapper contextWrapper = new ContextWrapper(context);
            File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
            File myInternalFile = new File(directory , filename);

            String myData = "";

            try {
                FileInputStream fis = new FileInputStream(myInternalFile);
                DataInputStream in = new DataInputStream(fis);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));

                String strLine;
                while ((strLine = br.readLine()) != null) {
                    myData = myData + strLine;
                }

                in.close();

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

            return myData;
        }
于 2013-09-11T14:44:10.290 回答
0

try this worked for me:

public void onCreate(Bundle savedInstanceState) 
 {
super.onCreate(savedInstanceState);

myList = new ArrayList<String>();   

String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/external_sd" ) ;       
File list[] = file.listFiles();

for( int i=0; i< list.length; i++)
{
    myList.add( list[i].getName() );
}

setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, myList ));

}

protected void onListItemClick(ListView l, View v, int position, long id) 
{
super.onListItemClick(l, v, position, id);

File temp_file = new File( file, myList.get( position ) );  

if( !temp_file.isFile())        
{
    file = new File( file, myList.get( position ));
    File list[] = file.listFiles();

    myList.clear();

    for( int i=0; i< list.length; i++)
    {
        myList.add( list[i].getName() );
    }
    Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show(); 
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, myList ));

   }

  }
于 2013-09-11T14:42:08.823 回答