2013-08-27 79 views
0

我有一個自定義列表視圖,如果用戶單擊某個項目,我一直在嘗試進入下一個屏幕。它似乎沒有工作。我試過,如果別的,切換。它可以工作,如果我不使用,但這樣,每個項目將會去同一屏幕。任何幫助都會變得渺茫。如何從此ListView中獲取所選項目?

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.options_main); 

    OptionMainHeader option_list[] = new OptionMainHeader[] 
    { 
     new OptionMainHeader(R.drawable.clock_icon_48, "Helo"), 
     new OptionMainHeader(R.drawable.weather_cloudy, "Apple"), 
     new OptionMainHeader(R.drawable.weather_cloudy, "Mango"), 
     new OptionMainHeader(R.drawable.weather_cloudy, "Banana"), 

     }; 

    OptionMainHeader1 adapter = new OptionMainHeader1(this, R.layout.option_main_header1, option_list); 

    listView1 = (ListView)findViewById(R.id.listView1); 

    View header = (View)getLayoutInflater().inflate(R.layout.option_main_header, null); 
    listView1.addHeaderView(header); 
    listView1.setAdapter(adapter); 

    listView1.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      int position = listView1.getSelectedItemPosition(); 
      String selectedFromList =(String) (listView1.getItemAtPosition(position)); 

      if (selectedFromList == "Helo"){ 
       Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class); 
       startActivity(CurrentWaitTime); 
      } else if (selectedFromList == "Apple"){ 
       Intent Appointments = new Intent(getApplicationContext(), no.class); 
       startActivity(Appointments); 
      } else if (selectedFromList == "Mango"){ 
       Intent Logout = new Intent(getApplicationContext(), go.class); 
       startActivity(Logout); 
      } else if (selectedFromList == "Banana"){ 
       Intent Exit = new Intent(getApplicationContext(), low.class); 
       startActivity(Exit); 
      } 
     } 
    }); 

} 
+0

您無法將字符串與「==」進行比較。你需要做的是更好地使用equals(String) – gian1200

+0

@ gian1200方法,他應該將字符串放在string.xml文件中,並使用enums作爲值。這樣他也可以使用開關箱,並減少代碼編寫。 –

+0

'getItemAtPosition()'不應該返回一個'OptionMainHeader'?如果它返回一個'OptionMainHeader',是不是你的轉換String selectedFromList =(String)(listView1.getItemAtPosition(position));'wrong?你可以發佈LogCat消息來檢查崩潰的原因嗎? –

回答

0

您無法將字符串與「==」進行比較。你需要做的是使用方法String.equals(String)。喜歡的東西:

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    String selectedFromList = option_list[position].getTHATSTRING(); 
    if (selectedFromList.equals("Helo")){ 
     Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class); 
     startActivity(CurrentWaitTime); 
    } else if (selectedFromLis.equals("Apple")){ 
     Intent Appointments = new Intent(getApplicationContext(), no.class); 
     startActivity(Appointments); 
    } else if (selectedFromList.equals("Mango")){ 
     Intent Logout = new Intent(getApplicationContext(), go.class); 
     startActivity(Logout); 
    } else if (selectedFromList.equals("Banana")){ 
     Intent Exit = new Intent(getApplicationContext(), low.class); 
     startActivity(Exit); 
    } 
} 
+0

此外,我不認爲有必要getSelectedItemPosition,因爲你的第三個參數onItemClick是你的位置(你有它名爲arg2) –

+0

應用程序停止,如果我這樣做,我得到一個錯誤 –

+0

@AndrewG是正確的,這是他的問題的真正原因。由於沒有任何選擇,'position'是'INVALID_POSITION'(定義爲'-1'),'getItemAtPosition'不會給任何明智的東西。簡單地使用正確的參數應該可以解決它。 –

0

試試這個

ListView1.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
TextView textView = (TextView) arg1; 
String strText = textView.getText().toString(); 
     if ("Helo".equals(strText)){ 
      Intent CurrentWaitTime = new Intent(getApplicationContext(), go.class); 
      startActivity(CurrentWaitTime); 
    } else if ("Apple".equals(strText){ 
      Intent Appointments = new Intent(getApplicationContext(), no.class); 
      startActivity(Appointments); 
     } etc, etc 
+0

它也不能工作,應用程序停止 –

0

試試這個,它會幫助你。

listView1.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) { 

     //global variable 
     idItem = (int) id; 

     switch (idItem) { 

     case 0: 
     // This is your first Item 

     // Add your Intent and startActivity 
    break; 

     case 1: 
    // Add your Intent and startActivity 
    break; 

     case 2: 
    // Add your Intent and startActivity 

    break; 

     case 3: 
    // Add your Intent and startActivity 

    break; 
     } 

     } 
    }); 

你得到了你點擊的項目ID。然後,您可以爲此項目選擇正確的活動。 希望它有幫助。

相關問題