2011-02-27 57 views
0

我开始使用Honeycomb,并试图做一个简单的碎片布局,左侧文件列表以及右侧文件的详细信息(当选择文件时)。那么,直到我真的试图列出这些文件,它才进行得很顺利,现在我只是得到一个“/”斜线,就是这样。没有其他的。我设置了一个日志来跟踪im目录中的文件数量,它看到26,但不会列出它们。继承人我的代码Java和Android文件浏览器问题

package com.bv.dual_fragments; 


import java.io.File; 
import java.util.ArrayList; 
import java.util.List; 

import android.app.AlertDialog; 
import android.app.Fragment; 
import android.app.ListFragment; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

public class fragment_one extends ListFragment{ 
private File currentDirectory = new File("/"); 
private List<String> directoryEnteries = new ArrayList<String>(); 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    return inflater.inflate(R.layout.fragment_one,container, false); 
} 
@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    //Inflate the layout for this fragment 
    super.onCreate(savedInstanceState); 

    //Browse to root directory 
    browseTo(new File("/")); 
} 
private void upOneLevel() { 
    if (this.currentDirectory.getParent() !=null) { 
     this.browseTo(this.currentDirectory.getParentFile()); 
    } 
} 
private void browseTo(final File aDirectory) { 
    //if we want to browse directory 
    if (aDirectory.isDirectory()){ 
     this.currentDirectory = aDirectory; 
     Integer fileLength = aDirectory.listFiles().length; 
     Log.i("File",fileLength.toString()); 
     File[] files = new File[fileLength]; 

     for (File file : files) { 
      Log.i("File", file.getAbsolutePath()); 
     } 
     fill(aDirectory.listFiles()); 

     //set the titlemanger text 
     TextView titleManager = (TextView)getView().findViewById(R.id.titlemanager); 
     titleManager.setText(aDirectory.getAbsolutePath()); 
    } else { 
     //if we want o open file, show this dialog: 
     //listener when Yes button clicked 
     OnClickListener okButtonListener = new OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       //intent to navigate file 
       Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("file://" + aDirectory.getAbsolutePath())); 
       startActivity(i); 
      } 
     }; 
     OnClickListener cancelButtonListener = new OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 

      } 
     }; 
     //create dialog 
     new AlertDialog.Builder(getActivity()) 
     .setTitle("Dialog Title") 
     .setMessage("File Name " + aDirectory.getName() + "?") 
     .setPositiveButton("Ok", okButtonListener) 
     .setNegativeButton("No", cancelButtonListener) 
     .show(); 
    } 
} 
private void fill(File[] files) { 
    //clear the list 
    this.directoryEnteries.clear(); 

    if (this.currentDirectory.getParent() != null) 
     this.directoryEnteries.add(".."); 

    //add every file in the list 
    for (File file : files) { 
     this.directoryEnteries.add(file.getAbsolutePath()); 
    } 
    //create array adapter to show everything 
    ArrayAdapter<String> directoryList = new ArrayAdapter<String>(getActivity(), R.layout.row, directoryEnteries); 
    this.setListAdapter(directoryList); 
} 
} 

所以,它运行,我得到我的碎片布局,但它不会列出文件。任何帮助,将不胜感激。哦,当我尝试把一个for循环添加到我的日志文件的每个名称,它会给出一个零点例外

编辑:我想我已追赶到它的行的资源文件,其实际上使用,这是R.layout.row,但我没有看到什么是错的。继承人该文件

<?xml version="1.0" encoding="utf-8"?> 
<TextView  
xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="fill_parent"  
android:layout_height="40sp"  
android:padding="5dip"  
android:background="@color/white" 
android:gravity="center_vertical"/> 

回答

1

布局添加后续调用 //浏览后根目录 browseTo(新文件(“/”));

ListView lv = getListView(); 
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
    lv.setCacheColorHint(Color.TRANSPARENT);