如何从R获取id的int id值?当我使用getIdentifier它的正确回报0.
int i = getArguments().getInt(SELECTION_NUMBER);
String drawerSelection = getResources().getStringArray(R.array.drawerSelection_array)[i];
int panelId = this.getResources().getIdentifier(drawerSelection.toLowerCase(),"id",getActivity().getPackageName());
Run Code Online (Sandbox Code Playgroud)
编辑
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/Bus_Schedules"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
日志
06-10 21:24:30.372: I/System.out(3572): Selection = Bus_Schedules
06-10 21:24:30.372: I/System.out(3572): panelId = 0
Run Code Online (Sandbox Code Playgroud)
R.java
public static final class id {
public static final int Bus_Schedules=0x7f090004;
public static final int basemenu=0x7f090005;
public static final int content_frame=0x7f090001;
public static final int drawer_layout=0x7f090000;
public static final int left_drawer=0x7f090002;
Run Code Online (Sandbox Code Playgroud)
Ted*_*opp 23
问题似乎是您正在转换drawerSelection为小写.从R.java文件中可以清楚地看出,标识符的大小写是保留的.试着打电话:
int panelId = this.getResources().getIdentifier(drawerSelection,"id",getActivity().getPackageName());
Run Code Online (Sandbox Code Playgroud)
刚刚回过头来看我正在写的项目:
int id = getResources().getIdentifier("resourcename", "drawable", getPackageName());
Run Code Online (Sandbox Code Playgroud)
getResources回报int.
更新:选中R.array.drawerSelection_array仅包含现有元素的ID.
| 归档时间: |
|
| 查看次数: |
31872 次 |
| 最近记录: |