2016-02-05 45 views
-1

我正在从事一个项目,该项目显示SD卡中目录中的文件列表。我所显示的所有文件都是.xls文件。我的手机中有一个XLS阅读器,我想打开列表视图中单击的文件。这里是我的代码:android:从列表视图中列出的文件列表中打开一个文件

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
     setContentView(R.layout.activity_subjects_list); 

     final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.abc_grow_fade_in_from_bottom); 
     Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf"); 
     subjectslistview = (ListView) findViewById(R.id.subjectslistview); 
     addSubjectbutton = (ImageButton) findViewById(R.id.subjectslistfabutton); 

     adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, fileList); 
     subjectslistview.setAdapter(adapter); 
     subjectslistview.setOnItemClickListener(this); 

    //attempt to connect to SD card 
    String path = Environment.getExternalStorageDirectory().toString() + "/SPAMS excel files"; 
    File dir = new File(path); 

    File files[] = dir.listFiles(); 
    for(int i =0; i < files.length; i++){ 
     fileList.add(files[i].getName()); 
    } 

    addSubjectbutton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      v.startAnimation(myAnim); 
      Toast.makeText(getApplicationContext(), "I am touched.", 
        Toast.LENGTH_SHORT).show(); 

      v.postDelayed(new Runnable() { 

       @Override 
       public void run() { 
        Intent goto_addsubj = new Intent(SubjectsListActivity.this, 
          AddSubjectActivity.class); 
        startActivity(goto_addsubj); 
        finish(); 
       } 
      }, 130); 

     } 
    }); 


} 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 

     TextView temp = (TextView) view; 
     Toast.makeText(this, temp.getText() + " " + position, 
      Toast.LENGTH_SHORT).show(); 

} 

我希望你的帮助家伙!请帮帮我。非常感谢:)

回答

0

你想要做的是用意图启动XLS阅读器(假设XLS阅读器有一个你可以启动的意图)。

当项目被点击时,抓住文件的URI,并启动意图。

Uri uri = new Uri(path); 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(uri, "application/vnd.ms-excel"); 

try { 
    startActivity(intent); 
} 
catch (ActivityNotFoundException e) { 
    Toast.makeText(context, "No Application Available",Toast.LENGTH_SHORT).show(); 
} 
+0

谢谢你的回复@Jeff Houghton。有一件事我需要问,在行中, 'Uri uri = new Uri(path);' 你指的是什么“路径”? – iamcarlok

+0

该路径应该是要用XLS阅读器打开的特定文件的路径。 –