所以我正在使用列表视图,试图在选择某个项目时打开特定的活动。这是我正在使用的代码。我知道它错了,但我不确定我需要改变它才能使它工作。我想我需要一个意图,并尝试了一些想法,但没有提出太多。感谢。
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);}
}
答案 0 :(得分:2)
我已经编辑了一下你的代码。您需要在布局XML文件中创建一个ListView
元素,然后在Java文件中使用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);}
}
}