I need to create file explorer expandable view. There are number of examples over the internet for file explorer but we cannot access child of child in folders. I try to develop cunstome adapter by extending BaseExpandableListAdapter. But setOnClickListener() is not work. Do anyone has suggestion for this or any other solution to make file exploere to access child of child?
This is my MainActivity:
public class MainActivity extends ExpandableListActivity {
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListView expandableList = getExpandableListView();
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
DirManager.getDir(Environment.getExternalStorageDirectory()+"/HomeDir");
parentItems=Repository.getParentItems();
childItems=Repository.getChildItems();
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
}
Repository Class:
public class Repository {
private static ArrayList<String> parentItems = new ArrayList<String>();
private static ArrayList<Object> childItems = new ArrayList<Object>();
public static ArrayList<String> getParentItems() {
return parentItems;
}
public static void setParentItems(ArrayList<String> parentItems) {
Repository.parentItems = parentItems;
}
public static ArrayList<Object> getChildItems() {
return childItems;
}
public static void setChildItems(ArrayList<Object> childItems) {
Repository.childItems = childItems;
}
}
DirManager Class:
public class DirManager {
private static ArrayList<String> parentItems = new ArrayList<String>();
private static ArrayList<Object> childItems = new ArrayList<Object>();
public static void getDir(String dirPath) {
System.out.println(dirPath);
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(Environment.getExternalStorageDirectory()+"/HomeDir"))
{
System.out.println("Test 4");
parentItems.add(Environment.getExternalStorageDirectory()+"/HomeDir");
parentItems.add("../");
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
System.out.println("Test 5");
if(file.isDirectory())
parentItems.add(file.getName() + "/");
else
parentItems.add(file.getName());
System.out.println("Test 6");
getChild(file);
}
Repository.setParentItems(parentItems);
Repository.setChildItems(childItems);
}
public static void getChild(File f) {
System.out.println("Test 7");
File[] files = f.listFiles();
for(int i=0; i < files.length; i++)
{
System.out.println("Test 8");
File file = files[i];
if(file.isDirectory())
parentItems.add(file.getName() + "/");
else
parentItems.add(file.getName());
System.out.println("Test 9");
childItems.add(file);
}
}
MyExpandableAdapter class:
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
@SuppressWarnings("unchecked")
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group, null);
}
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
}
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
//return 2;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return parentItems.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
This is the exception I get.
12-23 09:18:50.813: D/AndroidRuntime(18501): Shutting down VM 12-23 09:18:50.813: W/dalvikvm(18501): threadid=1: thread exiting with uncaught exception (group=0x410f32a0) 12-23 09:18:50.813: E/AndroidRuntime(18501): FATAL EXCEPTION: main 12-23 09:18:50.813: E/AndroidRuntime(18501): java.lang.ClassCastException: java.io.File cannot be cast to java.util.ArrayList 12-23 09:18:50.813: E/AndroidRuntime(18501): at com.example.androidexpandablefileexplorer.MyExpandableAdapter.getChildrenCount(MyExpandableAdapter.java:88) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:568) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:693) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:569) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3067) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.widget.AbsListView$1.run(AbsListView.java:3963) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.os.Handler.handleCallback(Handler.java:615) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.os.Handler.dispatchMessage(Handler.java:92) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.os.Looper.loop(Looper.java:137) 12-23 09:18:50.813: E/AndroidRuntime(18501): at android.app.ActivityThread.main(ActivityThread.java:4898) 12-23 09:18:50.813: E/AndroidRuntime(18501): at java.lang.reflect.Method.invokeNative(Native Method) 12-23 09:18:50.813: E/AndroidRuntime(18501): at java.lang.reflect.Method.invoke(Method.java:511) 12-23 09:18:50.813: E/AndroidRuntime(18501): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 12-23 09:18:50.813: E/AndroidRuntime(18501): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 12-23 09:18:50.813: E/AndroidRuntime(18501): at dalvik.system.NativeStart.main(Native Method)
Your object
childItems
is a type ofArrayList<Object>
and you are converting it toArrayList<String>
in this methodreplce this method with the following one