gpt4 book ai didi

java - 动态创建资源ID android

转载 作者:行者123 更新时间:2023-11-30 01:26:31 24 4
gpt4 key购买 nike

我试图从资源文件中动态获取 XML 数组,但我一直收到异常编号错误。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_class, container, false);
TextView classTitle = (TextView) rootView.findViewById(R.id.class_title);
classTitle.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

//FETCH THE XML ARRAY
String[] classAvgArray = getResources().getStringArray(R.array.classAvg);
//INDEX OF THE ARRAY
int key = getArguments().getInt(ARG_SECTION_NUMBER);

//DISPLAY THE AVG AMOUNT
TextView classTextAverage = (TextView) rootView.findViewById(R.id.classAvgAmount);
classTextAverage.setText("Avg: "+classAvgArray[key]);

//FETCH THE XML ARRAY FOR ITEMS
String ID = "c"+key;

int resID = getResources().getIdentifier(ID, "R.array.c"+key, "za.co.infomycito.mycitoapp");

String[] classactivities = getResources().getStringArray(resID);

最佳答案

我认为你应该这样做

int resID = getResources().getIdentifier("c"+key, "array", getPackageName());

如果你想获取R.array.c<key>的资源id .

关于java - 动态创建资源ID android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36358489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com