Android: Quoting through an array of strings - help needed for logic for the intent method

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(); } } ; } 
+7
source share
2 answers

classname rewritten every time you repeat the foreach loop inside setTeamNames() . Thus, only the last class name remains at the end.

If teams and teamnames have the same number of entries (what they need), you can use the following code and completely get rid of setTeamNames() .

 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(); } }); } ; 
+5
source

Assuming the actions you are trying to run have similar (or the same) layout and functionality, but display different data specific to each command, try the following.

Change your onClick(View v) method as follows:

 tv.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent open = new Intent(Transfers.this, TeamActivity.class); open.putExtra("team", ((TextView)v).getText()); startActivity(open); } }); 

Then create a "shared" Activity ...

 public class TeamActivity extends Activity { String teamName = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the layout here // Get the Intent and team name with the following... teamName = getIntent().getStringExtra("team"); // Do something with teamName } } 

Just register TeamActivity in your manifest and you should be good to go.

+1
source

All Articles