views:

156

answers:

1

So I'm working with a list view trying to get specific activities to open when a certain item is selected. This is the code that I'm working with right now. I know its wrong but am not sure what I need to change it make it work. I think I need an intent and tried a few ideas but did not come up with much. Thanks.

package table.periodic;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class List extends ListActivity{



String[] elements={"actinium", "aluminum", "americium", 
        "antimony", "argon", "arsenic", "astatine", "barium", 
        "berkelium", "beryllium", "bismuth", "bohrium", "boron", 
        "bromine", "cadmium", "calcium", "californium", "carbon", 
        "cerium", "cesium", "chlorine", "chromium", "cobalt", 
        "copper", "copernicium", "curium", "darmstadtium", "dubnium", 
        "dysprosium", "einsteinium", "erbium", "europium", "fermium", 
        "fluorine", "francium", "gadolinium", "gallium", "germanium", 
        "gold", "hafnium", "hassium", "helium", "holmium", 
        "hydrogen", "indium", "iodine", "iridium", "iron", 
        "krypton", "lanthanum", "lawrencium", "lead", "lithium", 
        "lutetium", "magnesium", "manganese", "meitnerium", "mendelevium", 
        "mercury", "molybdenum", "neodymium", "neon", "neptunium", 
        "nickel", "niobium", "nitrogen", "nobelium", "osmium", 
        "oxygen", "palladium", "phosphorus", "platinum", "plutonium", 
        "polonium", "potassium", "praseodymium", "promethium", "protactinium", 
        "radium", "radon", "rhenium", "rhodium", "roentgenium", "rubidium", 
        "ruthenium", "rutherfordium", "samarium", "scandium", "seaborgium", 
        "selenium", "silicon", "silver", "sodium", "strontium", "sulfur", 
        "tantalum", "technetium", "tellurium", "terbium", "thallium", "thorium", 
        "thulium", "tin", "titanium", "tungsten", "ununhexium", "ununoctium", 
        "ununpentium", "ununquadium", "ununseptium", "ununtrium", "uranium", 
        "vanadium", "xenon", "ytterbium", "yttrium", "zinc", "zirconium"};

 public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.list); 
    setListAdapter(new ArrayAdapter<String>(
            this, android.R.layout.simple_list_item_1, 
            elements));

 }

 public void onListItemClick(ListView parent, View v, int position,
         long id){


     if ("actinium".equals(elements[position]))
        {startActivity(Actinium.class);}

     else if ("aluminum".equals(elements[position]))
        {startActivity(Aluminum.class);}



 }
+1  A: 

Hi

I 've edited your code a bit. You need to create one listview element in your layout xml file. and then in your java file do find list view using findViewById.

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView;

public class List extends Activity{

String[] elements={"actinium", "aluminum", "americium", "antimony", "argon", "arsenic", "astatine", "barium", "berkelium", "beryllium", "bismuth", "bohrium", "boron", "bromine", "cadmium", "calcium", "californium", "carbon", "cerium", "cesium", "chlorine", "chromium", "cobalt", "copper", "copernicium", "curium", "darmstadtium", "dubnium", "dysprosium", "einsteinium", "erbium", "europium", "fermium", "fluorine", "francium", "gadolinium", "gallium", "germanium", "gold", "hafnium", "hassium", "helium", "holmium", "hydrogen", "indium", "iodine", "iridium", "iron", "krypton", "lanthanum", "lawrencium", "lead", "lithium", "lutetium", "magnesium", "manganese", "meitnerium", "mendelevium", "mercury", "molybdenum", "neodymium", "neon", "neptunium", "nickel", "niobium", "nitrogen", "nobelium", "osmium", "oxygen", "palladium", "phosphorus", "platinum", "plutonium", "polonium", "potassium", "praseodymium", "promethium", "protactinium", "radium", "radon", "rhenium", "rhodium", "roentgenium", "rubidium", "ruthenium", "rutherfordium", "samarium", "scandium", "seaborgium", "selenium", "silicon", "silver", "sodium", "strontium", "sulfur", "tantalum", "technetium", "tellurium", "terbium", "thallium", "thorium", "thulium", "tin", "titanium", "tungsten", "ununhexium", "ununoctium", "ununpentium", "ununquadium", "ununseptium", "ununtrium", "uranium", "vanadium", "xenon", "ytterbium", "yttrium", "zinc", "zirconium"};

public void onCreate(Bundle icicle) { super.onCreate(icicle);

setContentView(R.layout.main); 
ListView lv = (ListView)findViewById(R.id.ListView01);
lv.setAdapter(new ArrayAdapter<String>(
        this, android.R.layout.simple_list_item_1, 
        elements));

}

public void onListItemClick(ListView parent, View v, int position, long id){

 //if ("actinium".equals(elements[position]))
   // {startActivity(Actinium.class);}

 //else if ("aluminum".equals(elements[position]))
 //   {startActivity(Aluminum.class);}

} }

success_anil
ok. so that got my list to appear but how do i get a selection to start the new activity?
Joshua Sutherland
sure In that case uncomment the if ... and else if blocks.. Inside If or else if block { Intent i = new Intent(List.this,Actinium.class)startActivity(i);}Actinium class should be defined as shud extend Activity .. Moreoveran activity is to be declared in Android Manifest .xml so that Ancroid System can reconized that and start it as per our need
success_anil
ok. i see. thanks man.
Joshua Sutherland
well, I've got it all set up and it seems like it should be working but for some reason I get no result when I press a selection. I don't even get anything in the logcat. It's like nothing has happened.
Joshua Sutherland
Hi Joshua.. please send me the code files on which you are trying to implement this. I will resolve the error and let you know. My address is [email protected]
success_anil