在ListView中显示文件

时间:2013-09-11 14:38:21

标签: android android-listview file-manager

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

@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);

2 个答案:

答案 0 :(得分:1)

今年5月可以帮助你:

保存文件的方法:

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

答案 1 :(得分:0)

尝试这对我有用:

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

   }

  }