开发者

List View selection to start a new Activity

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", 
        "f开发者_如何学JAVAluorine", "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);}



 }


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, find it 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 = { … };  // (omitted for better legibility)

    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);}
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜