2013-07-29 28 views
0

我已經填充了使用SD卡內容的列表視圖,但它在單獨的列表活動中打開。我想在我自己的活動中填寫自己的listview名爲「list_local_content」。這裏是代碼..如何使用SD卡內容填充自己的列表視圖而不是單獨的列表活動

公共類Local_Contents擴展ListActivity {

private File currentDir; 
private FileArrayAdapter adapter; 

public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    currentDir = new File("/"); 
    fill(currentDir); 
} 

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

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

} 
@Override 
protected void onListItemClick(ListView l, View v, int position, long id){ 
super.onListItemClick(l, v, position, id); 
Option o = adapter.getItem(position); 
if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")){ 
    currentDir = new File(o.getPath()); 
    fill(currentDir); 
} 
else 
    onFileClick(o); 
} 

private void onFileClick(Option o) 
{ 
    Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show(); 
} 

}

和處理filearrayadapter是

公共類FileArrayAdapter擴展ArrayAdapter {

​​3210 其他類

}

我如何填寫我的活動我自己的ListView用更少的修改這個代碼...

回答

0

爲第一這樣做u需要改變什麼烏爾從listactivity延伸至活動,然後使用findViewById找到烏爾列表視圖,然後在listview obj上使用方法setAdapter和ur done。

+0

謝謝,這工作就像一個關鍵字對我來說,所以我做到了...... :-) –