我是Android开发的菜鸟,我想知道是否有一种从字符串和数组资源设置值的有效方法。当用户从很长的列表视图中选择一个值时,我必须访问非常大量的字符串和arrya资源。例如,如果listview包含Acne,Bruise等,则可以设置如下值:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.ailmentslistview:
String value = ((TextView) arg1).getText();
remedyDescription.setText(R.string.value_description);
remedyCause.setText(R.string.value_cause);
remedyHerbs.setText(R.string.value_herbs);
remedyFoods.setText(R.string.value_foods);
remedyListSupplements = res.getStringArray(R.array.value_supplements);
remedyListDosage = res.getStringArray(R.array.value_dosage);
remedyListComments = res.getStringArray(R.array.value_comment);
break
}
}
而不是:
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.ailmentslistview:
if(String value = ((TextView) arg1).getText()==Acne){
remedyDescription.setText(R.string.acne_description);
remedyCause.setText(R.string.acne_cause);
remedyHerbs.setText(R.string.acne_herbs);
remedyFoods.setText(R.string.acne_foods);
remedyListDosage = res.getStringArray(R.array.acne_dosage);
remedyListComments = res.getStringArray(R.array.acne_comment);
}else if(String value = ((TextView) arg1).getText()==Bruise) {
remedyDescription.setText(R.string.bruise_description);
remedyCause.setText(R.string.bruise_cause);
remedyHerbs.setText(R.string.bruise_herbs);
remedyFoods.setText(R.string.bruise_foods);
remedyListDosage = res.getStringArray(R.array.bruise_dosage);
remedyListComments = res.getStringArray(R.array.bruise_comment);
}
break;
}
}
非常感谢任何见解。
答案 0 :(得分:1)
String myString = getApplicationContext().getResources().getString(R.string.myString);
String [] myArray = getApplicationContext().getResources().getStringArray(R.array.myArray);
答案 1 :(得分:1)
您可以使用 getIdentifier(String name,String defType,String defPackage) 用于动态资源加载。
context.getResources()。getIdentifier(name,“string”,context.getApplicationInfo()。packageName);
在您的情况下,您可以按
创建字符串名称String value =((TextView)arg1).getText();
name = value.toLowerCase()+“_ description”;
等......
答案 2 :(得分:1)
String myString = getApplicationContext()。getResources()。getString(R.string.myString); String [] myArray = getApplicationContext()。getResources()。getStringArray(R.array.myArray);