title: How to display files in a list view on Android
body: <p>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:</p>
  
  <blockquote>
    <p>java.lang.ArrayIndexOutOfBoundsException length=1; index=1</p>
  </blockquote>
  
  <p>This is my activity after clicking button to populate list view:</p>
  
  <pre><code>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&lt;String&gt; filesinfolder = GetFiles("/sdcard/Download");
          List = (ListView)findViewById(R.id.listView_files);
          List.setAdapter(new ArrayAdapter&lt;String&gt;(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&lt;String&gt; GetFiles(String directorypath){
          ArrayList&lt;String&gt; Myfiles = new ArrayList&lt;String&gt;();
          File f = new File(directorypath);
          f.mkdirs();
          File[] files = f.listFiles();
          if(files.length==0){
              return null;
          }
          else{
              for(int i=0;i&lt;f.length();i++)
                  Myfiles.add(files[i].getName());
          }
          return Myfiles;
      }
  }
  </code></pre>
  
  <p>This is my logcat:</p>
  
  <pre><code>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
  </code></pre>