android把图标列表(filebrowser)

时间:2011-09-07 12:15:55

标签: android

你在android中创建一个文件浏览器,这是它的代码:

public class FileBrowser extends ListActivity {
    private IAppManager imService;

    private File currentDir;
    private FileArrayAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Editable usrr = Login.usernameText.getText();
        currentDir = new File("/sdcard/");
        fill(currentDir);

    }
    private void fill(File f)
    {
        File[]dirs = f.listFiles();
         this.setTitle(f.getName()+ "'s Chats" );
         List<Option>dir = new ArrayList<Option>();
         List<Option>fls = new ArrayList<Option>();
         try{
             for(File ff: dirs)
             {
                if(ff.isDirectory())
                    dir.add(new Option(ff.getName(),"Folder",ff.getAbsolutePath()));
                else
                {
                    fls.add(new Option(ff.getName(),"File Size: "+ff.length(),ff.getAbsolutePath()));
                }
             }
         }catch(Exception e)
         {

         }
         Collections.sort(dir);
         Collections.sort(fls);
         dir.addAll(fls);
        if(!f.getName().equalsIgnoreCase("sdcard"))
             dir.add(0,new Option("..","Parent Directory",f.getParent()));
         adapter = new FileArrayAdapter(FileBrowser.this,R.layout.filebrowser,dir);

         this.setListAdapter(adapter);

    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        Option o = adapter.getItem(position);
        if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")){
            //v.setBackgroundResource(R.drawable.foler);
                  currentDir = new File(o.getPath());
                fill(currentDir);
        }
        else
        {

            onFileClick(o);

        }
    }

现在我想要的是显示文件和文件夹的图标。(没有其他图标,只有这些2)我已经在xml布局中放置了一个图像视图,但我不知道如何动态检查哪个列表项是文件,以便它旁边显示一个文件图标,哪一个是显示文件夹图标的文件夹。 请帮忙!

1 个答案:

答案 0 :(得分:0)

您必须自定义FileArrayAdapter。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     View v = convertView;

     if (v == null) {

         LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = vi.inflate(id, null);

     }

         final Option o = items.get(position);

         if (o != null) {

             TextView fileName = (TextView) v.findViewById(R.id.FileName);
             ImageView fileIcon = (ImageView) v.findViewById(R.id.FileIcon);

             if(fileName!=null) {

                 fileName.setText(o.getName());

                 if(fileIcon!=null) {
                    if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")){
                    fileIcon.setImageRessource(<FolderIcon>)
                 } else {
                    fileIcon.setImageResouce(<FileIcon>)
                 }

             }
         }

    return v;

}