从选定的微调项字符串准备资源 ID
prepare resource id from selected spinner item string
我有一个带有各种项目的微调器,我用它来从所选项目中获取文本。
我想使用此文本为 openRawResource 准备一个资源 ID,我的代码看起来像
Spinner spinner = (Spinner) v.findViewById(R.id.accident);
String text = spinner.getSelectedItem().toString();
String newt = "R.raw." + text;
int textxx = Integer.parseInt(newt);
InputStream is = getResources().openRawResource(textxx);
但是不行,有什么想法
试试这个
将此添加到 strings.xml:
<string-array name="raw_resources">
<item>@raw/yourresource1</item>
<item>@raw/yourresource2</item>
<item>@raw/yourresource3</item>
<item>@raw/yourresource4</item>
<item>@raw/yourresource5</item>
</string-array>
现在在你的片段中像这样访问它:
final TypedArray resourceIDS = res.obtainTypedArray(R.array.select_sounds);
int[] resIds = new int[sounds.length()];
for (int i = 0; i < sounds.length(); i++) {
resIds[i] = sounds.getResourceId(i, -1);
}
resourceIDS.recycle();
现在您在 resIds 数组中拥有原始文件 ID 的所有 ID。只需根据在微调器中选择的项目对其进行索引即可。
第一个想法是使用 HashMap 来查找所需值的 ResID。但是,这有一个问题,即 'String' 需要是唯一的,否则您可能会遇到一些非常糟糕的行为(当您填充哈希图时,条目会相互覆盖)
但是 Resources has a method 会为您执行此查找
/*
Return a resource identifier for the given resource name.
A fully qualified resource name is of the form "package:type/entry". The
first two components (package and type) are optional if defType and
defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to
retrieve resources by identifier than by name.
*/
public int getIdentifier (String name, String defType, String defPackage)
所以试试这个:
Spinner spinner = (Spinner) v.findViewById(R.id.accident);
String resName = spinner.getSelectedItem().toString();
int resID= this.getResources().getIdentifier(
resName, "raw", this.getPackageName());
InputStream is = getResources().openRawResource(resID);
问题就在这里
int textxx = Integer.parseInt(newt);
正确的做法是:
int resID = getResources().getIdentifier(text, "raw", getApplicationContext().getPackageName());
现在您可以按如下方式使用 InputStream
:
InputStream is = getResources().openRawResource(resID);
编辑
does not allow getPackageName() says its indefined
如果你在 Activity
你可以做到 :
this.getPackageName(); //this -> context
如果你在 Fragment
你可以做到 :
getActivity().getPackageName();
but only opens the first item in the spinner does not open other files when i select other items
您需要实施 onItemSelected()
请参阅下面的代码。
final Spinner sp = (Spinner)findViewById(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
String text = sp.getSelectedItem().toString();
int resID = getResources().getIdentifier(text, "raw", getPackageName());
InputStream is = getResources().openRawResource(resID);
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
我有一个带有各种项目的微调器,我用它来从所选项目中获取文本。
我想使用此文本为 openRawResource 准备一个资源 ID,我的代码看起来像
Spinner spinner = (Spinner) v.findViewById(R.id.accident);
String text = spinner.getSelectedItem().toString();
String newt = "R.raw." + text;
int textxx = Integer.parseInt(newt);
InputStream is = getResources().openRawResource(textxx);
但是不行,有什么想法
试试这个 将此添加到 strings.xml:
<string-array name="raw_resources">
<item>@raw/yourresource1</item>
<item>@raw/yourresource2</item>
<item>@raw/yourresource3</item>
<item>@raw/yourresource4</item>
<item>@raw/yourresource5</item>
</string-array>
现在在你的片段中像这样访问它:
final TypedArray resourceIDS = res.obtainTypedArray(R.array.select_sounds);
int[] resIds = new int[sounds.length()];
for (int i = 0; i < sounds.length(); i++) {
resIds[i] = sounds.getResourceId(i, -1);
}
resourceIDS.recycle();
现在您在 resIds 数组中拥有原始文件 ID 的所有 ID。只需根据在微调器中选择的项目对其进行索引即可。
第一个想法是使用 HashMap 来查找所需值的 ResID。但是,这有一个问题,即 'String' 需要是唯一的,否则您可能会遇到一些非常糟糕的行为(当您填充哈希图时,条目会相互覆盖)
但是 Resources has a method 会为您执行此查找
/*
Return a resource identifier for the given resource name.
A fully qualified resource name is of the form "package:type/entry". The
first two components (package and type) are optional if defType and
defPackage, respectively, are specified here.
Note: use of this function is discouraged. It is much more efficient to
retrieve resources by identifier than by name.
*/
public int getIdentifier (String name, String defType, String defPackage)
所以试试这个:
Spinner spinner = (Spinner) v.findViewById(R.id.accident);
String resName = spinner.getSelectedItem().toString();
int resID= this.getResources().getIdentifier(
resName, "raw", this.getPackageName());
InputStream is = getResources().openRawResource(resID);
问题就在这里
int textxx = Integer.parseInt(newt);
正确的做法是:
int resID = getResources().getIdentifier(text, "raw", getApplicationContext().getPackageName());
现在您可以按如下方式使用 InputStream
:
InputStream is = getResources().openRawResource(resID);
编辑
does not allow getPackageName() says its indefined
如果你在 Activity
你可以做到 :
this.getPackageName(); //this -> context
如果你在 Fragment
你可以做到 :
getActivity().getPackageName();
but only opens the first item in the spinner does not open other files when i select other items
您需要实施 onItemSelected()
请参阅下面的代码。
final Spinner sp = (Spinner)findViewById(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
String text = sp.getSelectedItem().toString();
int resID = getResources().getIdentifier(text, "raw", getPackageName());
InputStream is = getResources().openRawResource(resID);
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});