开发者

android put icons to list (filebrowser)

开发者 https://www.devze.com 2023-04-02 23:12 出处:网络
hi m making a filebrowser in android and this is the code for it : public class FileBrowser extends ListActivity {

hi m making a filebrowser in android and this is the code for it :

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

        }
    }

now what i want is to display an icon for the files and the folders.(no other icons, just these 2) i have put an image view in the xml layout but i dont know how to dynamicaly check which list item is file so that it displays a file icon next to it and which one is a folder to display the folder icon. please help!


You have to customize the 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;

}
0

精彩评论

暂无评论...
验证码 换一张
取 消