2011-09-07 49 views
0

喜米的機器人制作filebrowser,這是它的代碼:機器人放置圖標列出(filebrowser)

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佈局,但我不知道如何dynamicaly檢查哪個列表項是文件,以便它顯示一個文件圖標旁邊,哪一個是一個文件夾來顯示文件夾圖標。 請幫忙!

回答

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; 

} 
+0

謝謝,但我沒有得到它:我是一個noob對不起..在我的代碼如何將這個? –

+0

在你的代碼中聲明:'private FileArrayAdapter adapter; '在這個FileArrayAdapter類中應該有方法:'getView(int position,View convertView,ViewGroup parent)'。如果你不想自定義FileArrayAdapter類本身,你可以創建一個自己的類來擴展FileArrayAdapter並覆蓋getView方法。 – Dyonisos