Android文件浏览器访问孩子的孩子

时间:2013-12-19 06:33:05

标签: android eclipse expandablelistview expandablelistadapter

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

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

1 个答案:

答案 0 :(得分:1)

您的对象childItemsArrayList<Object>的一种类型,您正在将其转换为 ArrayList<String> 在这个方法

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

使用以下方法重复此方法

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