我有 16 个按钮,我标记它们以将一些术语设置为按钮并从 sqlite 数据库导入。所以,我这样标记它们:
// 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);
.
.
.
.
}
}
所以,我需要 A1 和 B1 有相同的标签,还有 A2 和 B2 等等。
但是,由于某些其他原因,我还需要所有 A 按钮都有标签,例如“A”和所有 B 的“B”。那么,如何将多个(在我的情况下为两个)标签设置为一个按钮?
编辑:
我将此添加到我的 string.xml 中:
<item type="id" name="KOLONA1"/>
<item type="id" name="KOLONA2"/>
然后添加标签:
a1.setTag(R.id.KOLONA1, labelsA.get(0).tag);
b1.setTag(R.id.KOLONA2, labelsA.get(0).tag);