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檢查哪個列表項是文件,以便它顯示一個文件圖標旁邊,哪一個是一個文件夾來顯示文件夾圖標。 請幫忙!
謝謝,但我沒有得到它:我是一個noob對不起..在我的代碼如何將這個? –
在你的代碼中聲明:'private FileArrayAdapter adapter; '在這個FileArrayAdapter類中應該有方法:'getView(int position,View convertView,ViewGroup parent)'。如果你不想自定義FileArrayAdapter類本身,你可以創建一個自己的類來擴展FileArrayAdapter並覆蓋getView方法。 – Dyonisos