如何在Android的列表视图中显示文件 [英] How to display files in a list view on Android

查看:105
本文介绍了如何在Android的列表视图中显示文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Android应用程序,该应用程序由一个列表视图组成,该列表视图由内部存储器中存在的文件填充.当我单击一个按钮时,它显示一个错误:

I am creating an Android application that consists of a list view populated from files which were present in an internal storage. When I click a button it shows an error:

java.lang.ArrayIndexOutOfBoundsException length = 1;索引= 1

java.lang.ArrayIndexOutOfBoundsException length=1; index=1

这是单击按钮填充列表视图后的活动:

This is my activity after clicking button to populate list view:

package com.developer.milanandroid;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Review extends Activity {
    ListView List;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.review);
        ArrayList<String> filesinfolder = GetFiles("/sdcard/Download");
        List = (ListView)findViewById(R.id.listView_files);
        List.setAdapter(new ArrayAdapter<String>(Review.this, android.R.layout.simple_list_item_1, filesinfolder));
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public ArrayList<String> GetFiles(String directorypath){
        ArrayList<String> Myfiles = new ArrayList<String>();
        File f = new File(directorypath);
        f.mkdirs();
        File[] files = f.listFiles();
        if(files.length==0){
            return null;
        }
        else{
            for(int i=0;i<f.length();i++)
                Myfiles.add(files[i].getName());
        }
        return Myfiles;
    }
}

这是我的logcat:

This is my logcat:

01-02 02:43:25.860: E/AndroidRuntime(13136): FATAL EXCEPTION: main
01-02 02:43:25.860: E/AndroidRuntime(13136): Process: com.developer.milanandroid, PID: 13136
01-02 02:43:25.860: E/AndroidRuntime(13136): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.developer.milanandroid/com.developer.milanandroid.Review}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.os.Looper.loop(Looper.java:136)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.app.ActivityThread.main(ActivityThread.java:5017)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at java.lang.reflect.Method.invokeNative(Native Method)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at java.lang.reflect.Method.invoke(Method.java:515)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at dalvik.system.NativeStart.main(Native Method)
01-02 02:43:25.860: E/AndroidRuntime(13136): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
01-02 02:43:25.860: E/AndroidRuntime(13136):    at com.developer.milanandroid.Review.GetFiles(Review.java:58)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at com.developer.milanandroid.Review.onCreate(Review.java:23)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.app.Activity.performCreate(Activity.java:5231)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 02:43:25.860: E/AndroidRuntime(13136):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-02 02:43:25.860: E/AndroidRuntime(13136):    ... 11 more

推荐答案

尝试在for循环中访问错误的引用:

Try to access wrong reference in for loop :

for(int i=0;i<f.length();i++)

替换为:

for(int i=0;i<files.length;i++)

这篇关于如何在Android的列表视图中显示文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆