2011-02-27 98 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);