I am new to java and I use the code below to display a list of clicks of TextViews that call a specific class when clicked.
This 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 names of football teams.
I have an array of textview IDs that I look through and assign them an interactive action. It works great.
I have a string array of command names, the code goes through an array of strings and assigns each Class name to the Class object that is used in the Intent () method.
When I run this code, a list is created, but when I click on the command name, it always opens Wol.java, the last position in the string array.
I need help with logic, so when I click on Arsenal, ars.class opens
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(); } } ; }