私は Android アプリケーションに取り組んでいますが、小さな問題があります。assestsフォルダーにhtmlファイルのリストがあり、すべて「United Kingdom.html」などの国名に応じて名前が付けられており、ユーザーがリストビューから国名を選択しているため、リストから国名を取得して. 「country_name」という文字列を「value」文字列として別のアクティビティに渡します。同じ名前のファイルがassetsフォルダーに存在するかどうかを基本的に確認し、存在する場合はwebviewを使用してそのファイルを開く解決策を考え出そうとしています。以下のコードは、これまでに得たものです。
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("country_name");
result_info = (TextView) findViewById(R.id.result_info);
result_info.setText(value);
try {
Arrays.asList(getResources().getAssets().list("")).contains(value);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
webview = (WebView) findViewById(R.id.webview);
webview.loadUrl("file:///android_asset/United Kingdom.html");
}
前もって感謝します。
編集:可能な解決策を見つけたかもしれません。基本的に、ファイル名のリストを文字列の配列として取得し、それを私の文字列 (「値」 + 「.html」) と比較します。
final AssetManager assetManager = getAssets();
try {
// for assets folder add empty string
String[] filelist = assetManager.list("");
// for assets/subFolderInAssets add only subfolder name
String[] filelistInSubfolder = assetManager.list("subFolderInAssets");
if (filelist == null) {
// dir does not exist or is not a directory
} else {
for (int i=0; i<filelist.length; i++) {
// Get filename of file or directory
String filename = filelist[i];
}
}
// if(filelistInSubfolder == null) ............
} catch (IOException e) {
e.printStackTrace();
}
編集2:解決策
Venkatesh のおかげで、うまくいきました。彼のソリューションはほとんど変更する必要がありませんでしたが、次のコードを使用することで最終的に機能するようになりました。
AssetManager am = getAssets();
String extension = ".html";
try {
String fileList[] = am.list("");
//checking if any file gets returned from the array
//result_info.setText(fileList[1]);
for (String fileName : fileList) {
if (fileName.equals(value+extension)) {
//checking what the filename looks like
//result_info.setText(fileName);
webview = (WebView) findViewById(R.id.webview);
webview.loadUrl("file:///android_asset/"+fileName);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}