0

我开始使用 Honeycomb,并尝试使用左侧的文件列表和右侧的文件详细信息(选择文件时)制作一个简单的碎片布局。好吧,在我真正尝试列出文件之前一切顺利,现在我只得到一个“/”斜线,就是这样。没有其他的。我设置了一个日志来跟踪我所在目录中的文件数,它看到 26,但它不会列出它们。这是我的代码

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 循环以将文件的每个名称添加到我的日志时,它给出了一个空点异常

编辑:我想我已经把它追到了它实际使用的 Row 的资源文件,即 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"/>
4

1 回答 1

1

在调用 //Browse to root directory browseTo(new File("/")); 之后添加以下内容

    ListView lv = getListView();
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lv.setCacheColorHint(Color.TRANSPARENT);
于 2011-07-06T02:14:06.317 回答