How to set multiple tags to a button?

android, java

Solution

If you need to add multiple tag into one view then you have to define the id for every tag in `strings.xml` file like:

<item type="id" name="section" />
<item type="id" name="hide_show" />

After adding the key you can use these keys in java file like below:

rowView.setTag(R.id.section,mSectionList.get(position));
rowView.setTag(R.id.hide_show,"close");

This will set the tag. At the time of getting tag you need to typecast the object which originally you set like:

String mSection=(String)rowView.getTag(R.id.section);
String isOpen=(String)rowView.getTag(R.id.hide_show);

Problem

I have 16 buttons and I tag them to pair some terms set to buttons and imported from sqlite database. So, I tag them like this: ``` // labelForButton and tagForButton class MyStruct { public MyStruct (String lab, String t){ label = lab; tag = t; } private String label; private String tag; } mDbHelper.open(); Cursor c = mDbHelper.getSpojnice(generateWhereClause()); ArrayList<MyStruct> labelsA = new ArrayList<MyStruct>(); ArrayList<MyStruct> labelsB = new ArrayList<MyStruct>(); labelsA.add(new MyStruct(c.getString(2), "1")); // this tag should be the same to button that matches labelsB.add(new MyStruct(c.getString(3), "1")); labelsA.add(new MyStruct(c.getString(4), "2")); labelsB.add(new MyStruct(c.getString(5), "2")); labelsA.add(new MyStruct(c.getString(6), "3")); labelsB.add(new MyStruct(c.getString(7), "3")); labelsA.add(new MyStruct(c.getString(8), "4")); labelsB.add(new MyStruct(c.getString(9), "4")); labelsA.add(new MyStruct(c.getString(10), "5")); labelsB.add(new MyStruct(c.getString(11), "5")); labelsA.add(new MyStruct(c.getString(12), "6")); labelsB.add(new MyStruct(c.getString(13), "6")); labelsA.add(new MyStruct(c.getString(14), "7")); labelsB.add(new MyStruct(c.getString(15), "7")); labelsA.add(new MyStruct(c.getString(16), "8")); labelsB.add(new MyStruct(c.getString(17), "8")); Collections.shuffle(labelsA); Collections.shuffle(labelsB); a1.setText(labelsA.get(0).label); a1.setTag(labelsA.get(0).tag); a1.setOnClickListener(clickListener); b1.setText(labelsB.get(0).label); b1.setTag(labelsB.get(0).tag); b1.setOnClickListener(clickListener); a2.setText(labelsA.get(1).label); a2.setTag(labelsA.get(1).tag); a2.setOnClickListener(clickListener); b2.setText(labelsB.get(1).label); b2.setTag(labelsB.get(1).tag); b2.setOnClickListener(clickListener); . . . . } } ``` So, I need A1 and B1 to have same tags, also A2 and B2 and so on. BUT, I also need for some other reason to all A buttons have tag for example "A" and all the B's "B". So, how to set multiple, in my case two, tags to one button? EDIT: I added this to my string.xml: ``` <item type="id" name="KOLONA1"/> <item type="id" name="KOLONA2"/> ``` Then added tags: ``` a1.setTag(R.id.KOLONA1, labelsA.get(0).tag); b1.setTag(R.id.KOLONA2, labelsA.get(0).tag); ```

Original source