apw2012
apw2012

Reputation: 233

I Have a Basic ListView, but How to Link it to Activities?

I'm lost, so can I please have some help on how to link the list items to seperate activities? I have it to explain the different species available for what I am trying to do. It displays stuff such as dwarves, humans, all the fun stuff. Thanks in advance.

 package com.apw.games.rpg.medieval;

 import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Species extends Activity {

  private ListView mainListView ;
  private ArrayAdapter<String> listAdapter ;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.species);

    // Find the ListView resource. 
    mainListView = (ListView) findViewById( R.id.mainListView );

    // Create and populate a List of planet names.
    String[] planets = new String[] { "Human", "Dwarf", "Earth-Born", "Elf",
                                  "Sky Spirit", "Water Spritit", "Death Spirit", "War Spirit"};  
    ArrayList<String> planetList = new ArrayList<String>();
    planetList.addAll( Arrays.asList(planets) );

    // Create ArrayAdapter using the planet list.
    listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

    // Add more planets. If you passed a String[] instead of a List<String> 
    // into the ArrayAdapter constructor, you must not add more items. 
    // Otherwise an exception will occur.
    listAdapter.add( "Troll- Coming Soon" );
    listAdapter.add( "Giant- Coming Soon" );
    listAdapter.add( "God- Coming Soon" );
    listAdapter.add( "Monster- Coming Soon" );

    // Set the ArrayAdapter as the ListView's adapter.
    mainListView.setAdapter( listAdapter );      
  }
}

Upvotes: 0

Views: 451

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006944

Call setOnItemClickListener() on your ListView, then call startActivity() from the onItemClick() method of that listener.

More often, though, you will find developers using ListActivity, instead of Activity, and overriding onListItemClick() in their ListActivity. See: https://github.com/commonsguy/cw-omnibus/tree/master/Selection/List

Upvotes: 1

Related Questions