3

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);
        }
      });
}

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.

Edit - more code as requested.

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

Shane
  • 2,315
  • 3
  • 21
  • 33

2 Answers2

11

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())

Read more here.

Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
  • This is perfect for the question I asked, thank you. Will look into the 'Tag' property also, it seems it would be more suited to my app as a whole – Shane Aug 19 '11 at 05:30
0

The best solution to store the resource Id would probably be using the Tag property of the TextView (If I understand correctly, the TextView from your code is part of a custom list item, correct ?)

Muzikant
  • 8,070
  • 5
  • 54
  • 88
  • It is part of a custom list item, and the Tag property sounds promising. I will look it up, as I have no experience with this property – Shane Aug 19 '11 at 05:06
  • just use setTag and getTag. You can store any object in that propertry. My advice to you is to set it to null on your activity onDestroy so you won't have memory leaks. – Muzikant Aug 19 '11 at 05:52