2

我需要创建文件资源管理器可扩展视图。在互联网上有许多文件资源管理器的例子,但我们无法访问文件夹中的孩子的孩子。我尝试通过扩展BaseExpandableListAdapter来开发cunstome适配器。但是setOnClickListener()不起作用。有没有人有这样的建议或任何其他解决方案,使文件exploere访问儿童的孩子?Android文件资源管理器访问子文件的孩子

这是我的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); 

    } 
} 

仓储类:

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类:

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类:

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; 
    } 
} 

这是我得到的例外。

 
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) 
+0

转换为 ArrayList<String> 这个方法是什么是MyExpandableAdapter.java文件中的第87行:? 。 – SilentKiller

+0

@Override \t公众诠释getChildrenCount(INT groupPosition){ \t \t回报((ArrayList的)childtems.get(groupPosition))尺寸(); \t} – IBunny

+1

显示您的适配器类。 – SilentKiller

回答

2

你的对象childItems是一种类型的ArrayList<Object>且将其

@Override 
public int getChildrenCount(int groupPosition) { 
    return ((ArrayList<String>) childtems.get(groupPosition)).size(); 
} 

replce这种方法具有以下一个

@Override 
public int getChildrenCount(int groupPosition) { 
    return ((ArrayList<Object>) childtems.get(groupPosition)).size(); 
} 
+0

哦!谢谢..但仍然Expandableview无法正常工作。所有家长和孩子的项目都在主页面列出。一旦我触摸应用程序的任何项目,然后应用程序关闭,说有错误。 – IBunny

相关问题