Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:21:09+00:00 2026-06-15T09:21:09+00:00

I have written a file chooser and added the folder and files in ListView

  • 0

I have written a file chooser and added the folder and files in ListView. But On Item Click it gives me ArrayIndexOutOfBoundsException in position on my point of view. Can someone tell me what the problem is?

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:21:10+00:00Added an answer on June 15, 2026 at 9:21 am

    You need to retrieve the file name from your data structure mList in the onListItemClick method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written my webmethod in aspx.cs file , but when i call n.Nautilus()
I have written java code for generating json of my searched data from file.But
I have written xml file which contains html tags as element like <component> <input
I have written the batch file below, in order to automate the process of
I have written a raw binary image file into a buffer which is an
I have a written up HTML file using the Google Maps API v3. All
I have written a Flex Library project - a .swc file - which is
I have written a shellscript which tries to pull a tar file from an
I have written a following code to get just the file name without extension
I have written simple code to get content from xml file to php. $xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.