这是我的看法的文字重新present的ID。所以,当点击时,我想获得该资源的引用。以下不正确的code再presents我想要做的。
The text on my views represent an id. So when clicked I would like to get a reference to that resource. The following incorrect code represents what I'm trying to do
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);
}
});
}
我明白为什么这个方法是行不通的。例如该视图有文字例1,然后会有一个R.id.example1价值,我需要得到一个参考。显然,我这接近走错了路,想一些指导。
I understand why this method doesn't work. For example the view would have the text "example1" and then there would be an R.id.example1 value that I need to get a reference to. Obviously I am approaching this the wrong way, and would like some guidance.
编辑 - 的要求更多code。
Edit - more code as requested.
EDIT2 - 可怜的描述。所以,我的活动是一个自定义列表。当点击的意见之一,该项目的冠军将对应一个字符串数组我有我的strings.xml。因此,如果例1是pressed,会有一个字符串数组用id R.array.example1。我希望我的code提取观点的案文,并用它来寻找正确的字符串数组。然后这个字符串阵列将被用于填充新的活动,与自定义列表项阵列中的项
Edit2 - Poor description. So my activity is a custom list. When one of the views is clicked, the title of that item will correspond to a string-array I have in my strings.xml. So if "example1" is pressed, there will be a string-array with the id R.array.example1. I want my code to extract the text of the view, and use it to find the correct string-array. This string-array would then be used to populate a new activity, with the custom list items the items in the array
我没有得到什么你code是应该做的。但是为了得到一个资源ID出来的资源名称,请使用以下
I didn't get exactly what your code is supposed to do. But to get a resource id out of the resource name, use the following
getResources().getIdentifier(<resource name>, "id", getPackageName())
阅读更多here.
上一篇:如何使用Roboto字体在TextView中?如何使用、字体、Roboto、TextView
下一篇:ActionBarActivity - NoClassDefFoundError的异常异常、ActionBarActivity、NoClassDefFoundError