0

I currently have a list view and I'm trying to direct the list view to different activities. So that if you click an item from let's say 1-4 you'll get the class that corresponds to that. The only way that I can think of doing it is grabbing the text of the item in the list view and starting the activity of that name. The code for it would go something like this:

final String chosen = ""; chosen = (String) ((TextView) view).getText(); Intent nextScreen = new Intent(getApplicationContext(), chosen.class); 

That does not work. I get an error on the last line, saying that chosen cannot be resolved into a type.

I know that ((TextView) view).getText() works because

Log.d("Debug", "Test"+((TextView) view).getText()); 

gives me the correct chosen item in logcat.

Any ideas/suggestions? Thanks in advance

EDIT:

I tried changing my code to this:

String chosen = (String) ((TextView) view).getText(); try { Intent nextScreen = new Intent(getApplicationContext(), Class.forName(chosen)); startActivity(nextScreen); Log.d("Debug", "Good"+((TextView) view).getText()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.d("Debug", "Bad"+((TextView) view).getText()); } Log.d("Debug", "Final"+((TextView) view).getText()); 

Log cat gave me an output of

BadItem1 FinalItem1 

I think I'm going about this the wrong way as someone pointed out. I also think I should be using OnItemClickListener. I will try it and post my results for easier help in the future.

1
  • Don't use application context, use the activity you just came from as the context Commented Dec 18, 2011 at 21:51

5 Answers 5

1

The error is here:

final String chosen = ""; chosen = (String) ((TextView) view).getText(); 

Since you declare chosen as final, you can assign it a value only once:

final String chosen = (String) ((TextView) view).getText(); 

Moreover I suppose you want to start the Activity which has the name that is stored in variable chosen. You cannot write chosen.class for this. The correct way to do this is:

Class.forName(chosen); 

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

There is no Intent constructor that takes a Context and a String. You can probably do something like Class.forName(chosen) in the call to the Intent constructor.

Comments

0

Calling .class on a variable gets the class for that variable, not the class for the content of the variable.

I think that you should use the OnItemClickListener on the ListView to identity the clicked item. The position parameter, or calling getItemAtPosition(position), should be enough to identify uniquely a item in the listview and call the appropriate activity.

See documentation here: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

Comments

0

Make a Factory.

This way you don't have to tie your class names to the text the user is reading. giving you more flexibility / better user experience / easier code to maintain. Helping with your separation of concerns as well.

class NavFactory { private static final int CLASS_FIRST = 1; public static Intent getNavIntent(Context context, String name){ switch(getId(name)){ case CLASS_FIRST: return new Intent(context, FirstClass.class); default: return null; } } private static int getId(String name){ if("listItemOneText".equals(name) return CLASS_FIRST; return -1; } } 

Ref: Java Factory Pattern

In your case:

String chosen = (String) ((TextView) view).getText(); Intent nextScreen = NavFactory.getNavIntent(this, chosen); 

Comments

0

You are new sending String as the class of supposed Activity. This is what you need:

final String chosen = ""; chosen = ((TextView) view).getText(); Class<?> chosenActivity = Class.forName(chosen); Intent nextScreen = new Intent(this, chosenActivity); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.