2010-08-06 60 views
1

因此,我正在使用列表視圖來嘗試獲取特定活動以在選定某個項目時打開。這是我現在正在使用的代碼。我知道它錯了,但不知道我需要改變它使它工作。我想我需要一個意圖,並嘗試了一些想法,但沒有提出太多。謝謝。列表視圖選擇開始新的活動

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



} 

回答

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

確定。以便讓我的名單出現,但我如何獲得選擇以開始新的活動? – 2010-08-06 16:33:06

+0

sure在這種情況下,取消註釋if ...和else if塊。 Inside if或else if block { Intent i = new Intent(List.this,Actinium.class) startActivity(i); } Act類應定義爲shud extend Activity。此外, 活動將在Android Manifest .xml中聲明,以便Ancroid系統可以重新調整 並根據我們的需要啓動它 – 2010-08-06 17:36:22

+0

ok。我懂了。謝啦。 – 2010-08-07 14:46:23