私はハニカムで作業を開始し、左側にファイルのリスト、右側にファイルの詳細 (ファイルが選択されている場合) を含む単純な断片化されたレイアウトを作成しようとしています。実際にファイルを一覧表示しようとするまではうまくいっていたのですが、今は "/" スラッシュが表示されるだけです。他には何もありません。ディレクトリ内のファイル数を追跡するログを設定しました.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 ループを挿入しようとすると、nullpoint 例外が発生します。
編集:実際に使用しているR.layout.rowの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"/>