2016-02-05 18 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閱讀器打開的特定文件的路徑。 –

相關問題