我正在开发一个项目,显示SD卡目录中的文件列表。我显示的所有文件都是.xls文件。我的手机里有一个XLS阅读器,我想打开列表视图中点击的文件。这是我的代码:
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 :(得分: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();
}