2012-12-03 160 views
1

我正在編寫Android文件選擇器程序,並且在ListView中添加詳細信息時,該流程無法正常工作。在文件Uploadlist.java中,我將目錄路徑發送到ArrayAdapter的另一個類filedetails。但是代碼流並沒有進入方法getView()。有人可以說出原因,並告訴爲什麼代碼不會進入getView()。我已經使用了下面的代碼。安卓文件選擇器無法正常工作

UploadList.Java

public class UploadList extends ListActivity { 
private List<String> item=null; 
private List<String> path=null; 
private String root; 
private TextView mypath; 
String pathname,filename; 
ImageView image; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mypath=(TextView)findViewById(R.id.path); 
     image=(ImageView)findViewById(R.id.imageView1); 
     root=Environment.getExternalStorageDirectory().getPath(); 
     filedetails details=new filedetails(this, root); 
     setListAdapter(details); 
    } 
    /** 
    * @param dirPath 
    */ 
    public void getDir(String dirPath){ 
     mypath.setText("Location is:"+dirPath); 
     pathname=dirPath; 
     filedetails details=new filedetails(this, dirPath); 
     setListAdapter(details); 
    } 
    protected void onListItemClick(ListView l,View v,int position,long id){ 
     File file=new File(path.get(position)); 
     if(file.isDirectory()){ 
      if(file.canRead()) 
       getDir(path.get(position)); 
     } 

     else 
     { 
      filename=file.getName(); 
      pathname=pathname+"/"+filename; 
      Intent back=new Intent(this,Upload.class); 
      Bundle filen=new Bundle(); 
      filen.putString("filepath",pathname); 
       back.putExtras(filen); 
      startActivity(back); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_upload_list, menu); 
     return true; 
    } 

} 

filedetails.java

public class filedetails extends ArrayAdapter<String> { 
Context context; 
File[]values; 
String dirPath; 
String root=Environment.getExternalStorageDirectory().getPath(); 
String filename; 
private List<String> item; 
private List<String> path; 
    public filedetails(Context context,String dirPath) 
    { 
     super(context,R.layout.row1); 
     this.context=context; 
     this.dirPath=dirPath; 
    } 

    static class ViewHolder { 
     public TextView text; 
     public ImageView image; 
     } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View rowView = inflater.inflate(R.layout.row1, parent, false); 
     ViewHolder viewholder=new ViewHolder(); 
     viewholder.text=(TextView)rowView.findViewById(R.id.textView1); 
     viewholder.image=(ImageView)rowView.findViewById(R.id.imageView1); 
     rowView.setTag(viewholder); 
     item=new ArrayList<String>(); 
     path=new ArrayList<String>(); 
     File file=new File(dirPath); 
     File[]filelist=file.listFiles(); 
     ViewHolder holder=(ViewHolder)rowView.getTag(); 
     if(!dirPath.equals(root)){ 
      item.add(root); 
      path.add(root); 
      item.add("../"); 
      path.add(file.getParent()); 
     } 
      for(int i=0;i<filelist.length;i++){ 
       File file1=values[i]; 
       if(file1.canRead()) 
       { 
         if(file1.isDirectory()){ 
         holder.image.setImageResource(R.drawable.folder); 
         holder.text.setText(file1.getName()); 

        } 
       } 
        else{ 
         holder.image.setImageResource(R.drawable.file); 
         holder.text.setText(file1.getName()); 

        } 
       } 

      return rowView; 

} 
} 
+0

嘗試加入'@ Override'? –

+0

嘗試在getView中重寫,嘗試不工作 –

+0

問題down投票爲什麼 –

回答

1

變化

File[]values; 

//File[]values; 

,然後改變

File file1=values[i]; 

File file1=filelist[i];