我想在 Android 列表视图中打开一个选定的项目。如果是图像,我想显示它,如果是文本文件,那么我想打开它,但是我的代码不起作用,这里是 oncreate 方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv;
ArrayList<String> FilesInFolder = GetFiles("/sdcard/DCIM/Camera/", true);
lv = (ListView) findViewById(R.id.filelist);
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, FilesInFolder));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Clicking on items
String name_of_file = lv.getItemAtPosition(position).toString();
Intent myIntent = new Intent(v.getContext(), MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("/sdcard/DCIM/Camera/", name_of_file);
myIntent.putExtras(bundle);
try {
startActivity(myIntent);
finish();
} catch (android.content.ActivityNotFoundException e) {
}
}
});
}