Associate an Activity with an item in XML ListViews in Android
I have a ListView that is populated using an XML file. However, I want each item, when clicked, to start a new Activity related to that item.
I understand how to use OnItemClick to start a Toast that shows the selected item's text. However, since the ListView is populated from an XML there is not a specific Id for each item in the开发者_开发百科 list.
So, how would I associate an Activity with each item in the ListView when the items do not have Ids?
Maintain in your XML file a node for the Activity that has to be called, in a numeric or string format. Then have a list of activities in a collection, that are implementing a certain interface/abstract class, so that you can loop on it. Also activities have a static field/method that return their reference number or string so you can compare too.
Some sketch:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<category
name="Item One"
id="grp1" />
<category
name="Item Two"
id="grp2" />
</resources>
In Java:
List<AbstractTable> col = new ArrayList<AbstractTable>();
col.add(new clsGroup1(this.ctx));
col.add(new clsGroup2(this.ctx));
for (AbstractTablecls : col) {
if (cls.getTag().equals(varFromListSelection)) {
//launch intent of this class
}
}
where getTag()
returns the identifier for the class eg: grp1
or grp2
精彩评论