-1

ファイルチューザーを作成し、フォルダーとファイルをに追加しましたListView。しかし、アイテムクリックでそれは私ArrayIndexOutOfBoundsExceptionの視点で私を正しい位置に与えます。誰かが問題が何であるか教えてもらえますか?

FileUpload.Java

public class FileUpload extends ListActivity {
    private static final String ITEM_KEY = "key";
    Boolean contains=false;
    private static final String ITEM_IMAGE = "image";
    private static String ROOT = "/";
    public static final String START_PATH = "START_PATH";
    public static final String RESULT_PATH = "RESULT_PATH";
    private List<String> item = null;
    private List<String> path = null;
    public static final String FORMAT_FILTER = "FORMAT_FILTER";
    private TextView myPath;

    private EditText mFileName;


    private static String currentpath;
    private LinkedHashMap<String, Integer> lastPositions = new LinkedHashMap<String, Integer>();
    private ArrayList<HashMap<String, Object>> mList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_upload);
        myPath = (TextView) findViewById(R.id.path);
        mFileName = (EditText) findViewById (R.id.fdEditTextFile);
        String startPath = getIntent().getStringExtra(START_PATH);
        startPath = startPath != null ? startPath : ROOT;
        getDir(startPath);
    }
    public void getDir(String DirPath){
        /*Integer position = lastPositions.get(parentPath);*/
        if(DirPath!=null){
        getDirImpl(DirPath);
        }
        }
    public void getDirImpl(final String dirpath){
        currentpath = dirpath;
        myPath.setText("Location is:"+currentpath);
        item = new ArrayList<String>();
         path = new ArrayList<String>();
         mList=new ArrayList<HashMap<String,Object>>();
        File f=new File(currentpath);
        File[] files=f.listFiles();


        if (!currentpath.equals(ROOT)) {

            item.add(ROOT);
            addItem(ROOT, R.drawable.folder);
            item.add("../");
            addItem("../", R.drawable.folder);
            path.add(f.getParent());
            }
        TreeMap<String, String> dirsMap = new TreeMap<String, String>();
        TreeMap<String, String> dirsPathMap = new TreeMap<String, String>();
        TreeMap<String, String> filesMap = new TreeMap<String, String>();
        TreeMap<String, String> filesPathMap = new TreeMap<String, String>();
        for(int i=0;i<files.length;i++){
            File file = files[i];

            if(file.isDirectory()){
                path.add(file.getPath());
                String dirName=file.getName()+"/";
                dirsMap.put(dirName, dirName);
                dirsPathMap.put(dirName, file.getPath());

            }
            else{

                final  String fileName=file.getName();
                filesMap.put(fileName, fileName);
                filesPathMap.put(fileName, file.getPath());
                }
        }
        item.addAll(dirsMap.tailMap("").values());
        item.addAll(filesMap.tailMap("").values());
        item.addAll(dirsPathMap.tailMap("").values());

        SimpleAdapter fileList = new SimpleAdapter(this, mList, R.layout.file_dialog_row, new String[] {
                ITEM_KEY, ITEM_IMAGE }, new int[] { R.id.fdrowtext, R.id.fdrowimage });
        for(String dir:dirsMap.tailMap("").values()){
            addItem(dir, R.drawable.folder);
        }
        for(String file:filesMap.tailMap("").values()){
            addItem(file,R.drawable.file);
        }
    fileList.notifyDataSetChanged();
    setListAdapter(fileList);
    }
    public void addItem(String FileName,int imageid){
        HashMap<String,Object>item=new HashMap<String, Object>();
        item.put(ITEM_KEY, FileName);
         item.put(ITEM_IMAGE, imageid);
         mList.add(item);
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        File file = new File(path.get(position));

        if (file.isDirectory()) {

            if (file.canRead()) {
                lastPositions.put(currentpath, position);
                getDir(path.get(position));

            }
        } else {
                String filename=file.getName();
                mFileName.setText(filename);
            }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_file_upload, menu);
        return true;
    }

}

StackRace

12-03 18:08:31.458: E/AndroidRuntime(1784): FATAL EXCEPTION: main
12-03 18:08:31.458: E/AndroidRuntime(1784): java.lang.IndexOutOfBoundsException: Invalid index 10, size is 0
12-03 18:08:31.458: E/AndroidRuntime(1784):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at java.util.ArrayList.get(ArrayList.java:311)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at com.example.projectupload.FileUpload.onListItemClick(FileUpload.java:112)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at android.widget.ListView.performItemClick(ListView.java:3382)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at android.os.Handler.handleCallback(Handler.java:587)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at android.os.Looper.loop(Looper.java:123)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at java.lang.reflect.Method.invoke(Method.java:521)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-03 18:08:31.458: E/AndroidRuntime(1784):     at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0

onListItemClickメソッドでデータ構造mListからファイル名を取得する必要があります。

于 2012-12-03T13:44:06.317 に答える