将“R.id.myID”从String转换为int值R.id.myID?

我的观点上的文字代表一个id。 因此,当点击时,我想获得对该资源的引用。 以下错误代码代表我正在尝试做的事情

public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.categories); Bundle bundle = getIntent().getExtras(); int myArrayID = bundle.getInt("category_id", 0); String [] myArray = getResources().getStringArray(myArrayID); setListAdapter(new CategoryAdapter(this, android.R.layout.simple_list_item_1, R.id.categoryText, myArray)); ListView lv = getListView(); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { String myIdString = (String) ((TextView) view.findViewById(R.id.categoryText)).getText(); int myIdInt = // convert to the correct id that is represented by this string myIdString Intent intent = new Intent(Categories.this, Categories.class); intent.putExtra("category_id", myIdInt); startActivity(intent); } }); } 

我理解为什么这种方法不起作用。 例如,视图将具有文本“example1”,然后将需要获取引用的R.id.example1值。 显然我正在接近这个错误的方式,并希望得到一些指导。

编辑 – 根据要求提供更多代码。

Edit2 – 描述不佳。 所以我的活动是一个自定义列表。 单击其中一个视图时,该项的标题将对应于我在strings.xml中的字符串数组。 因此,如果按下“example1”,将会有一个字符串数组,其ID为R.array.example1。 我希望我的代码提取视图的文本,并使用它来查找正确的字符串数组。 然后,此字符串数组将用于填充新活动,自定义列表项表示数组中的项目

我没有得到你的代码应该做的确切内容。 但是要从资源名称中获取资源ID,请使用以下命令

 getResources().getIdentifier(, "id", getPackageName()) 

在这里阅读更多。

存储资源Id的最佳解决方案可能是使用TextView的Tag属性(如果我理解正确,代码中的TextView是自定义列表项的一部分,对吗?)