Android: Looping through a string array - help needed on logic for intent method
android, android-intent, arrays, java, logic
Solution
`classname` is being overwritten every time you iterate in the foreach loop inside `setTeamNames()`. Thus, only the last classname remains at the end.
If `teams` and `teamnames` have the same number of entries (which they should), you can use the following code and get rid of `setTeamNames()` completely.
final String prefix = "ttj.android.ft.teams.";
for (int i = 0; i < teams.length; i++) {
tv = (TextView) findViewById(teams[i]);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
Class class = Class.forName(prefix + teamnames[i]);
Intent open = new Intent(Transfers.this, class);
startActivity(open);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
});
}
;
Problem
I'm a java novice and I've using the code below to display a list of clickable TextViews that call a specific class when they are clicked. It is a list of football team names. Eg. Arsenal Manchester United Chelsea Clicking on TextView Arsenal (TextView id = ars) will call ars.class Clicking on TextView Chelsea (TextView id = che)will call che.class I have over 20 football team names. I have an array of textview ids, which I loop through and assign them a clickable action. This works fine. I have an string array of teamnames, the code loops through the string array and assigns each teamname to a Class object, which is used in the Intent() method. When I run this code, the list is produced but when I click on a team name it always opens Wol.java, the last position in the string array. I need some help with the logic, so that when I click on Arsenal it opens ars.class Here is the code. ``` public final int[] teams = { R.id.ars, R.id.ast, R.id.bir, R.id.bla, R.id.blp, R.id.bol, R.id.che, R.id.eve, R.id.ful, R.id.hul, R.id.lee, R.id.liv, R.id.mid, R.id.mnc, R.id.mnu, R.id.nor, R.id.nwu, R.id.por, R.id.qpr, R.id.sto, R.id.sun, R.id.swa, R.id.tot, R.id.wes, R.id.wig, R.id.wol }; //String array of teamnames, named to correspond their class name. public final String[] teamnames = { "ars", "ast", "bir", "bla", "blp", "bol", "che", "eve", "ful", "hul", "lee", "liv", "mid", "mnc", "mnu", "nor", "nwu", "por", "qpr", "sto", "sun", "swa", "tot", "wes", "wig", "wol" }; TextView tv; Class classname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.m_transfers); setTeamNames(); //Method sets up team names to a Class name. for (int i = 0; i < teams.length; i++) { tv = (TextView) findViewById(teams[i]); tv.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent open = new Intent(Transfers.this, classname); startActivity(open); } }); } ; } public void setTeamNames() { for (String s : teamnames) { String name = "ttj.android.ft.teams." + s; try { classname = Class.forName(name); } catch (ClassNotFoundException e) { e.printStackTrace(); } } ; } ```