Wednesday, April 10, 2013

[android help] Android File Chooser in On Item Click


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 item = null;
private List path = null;
public static final String FORMAT_FILTER = "FORMAT_FILTER";
private TextView myPath;

private EditText mFileName;


private static String currentpath;
private LinkedHashMap lastPositions = new LinkedHashMap();
private ArrayList> 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();
path = new ArrayList();
mList=new ArrayList>();
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 dirsMap = new TreeMap();
TreeMap dirsPathMap = new TreeMap();
TreeMap filesMap = new TreeMap();
TreeMap filesPathMap = new TreeMap();
for(int i=0;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){
HashMapitem=new HashMap();
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)


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...