2013-05-31 60 views
0

我似乎無法得到此代碼在此刻工作。我想單擊列表視圖打開一個新的活動我知道這個代碼是什麼後,但我一直在玩這幾天現在,我只是不能做到這一點。對話框修復中的OnClick列表視圖

public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    switch(position) 
{ 
case 0: Intent newActivity = new Intent(this, superleague.class);  
     startActivity(newActivity); 
     break; 
case 1: Intent newActivity = new Intent(this, youtube.class);  
     startActivity(newActivity); 
     break; 
    case 2: Intent newActivity = new Intent(this, olympiakos.class);  
     startActivity(newActivity); 
     break; 
    case 3: Intent newActivity = new Intent(this, karaiskaki.class);  
     startActivity(newActivity); 
     break; 
    case 4: Intent newActivity = new Intent(this, reservetickets.class);  
     startActivity(newActivity); 
     break; 
    } 
    } 

main.java

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:onClick="popUp" 
    android:text="pop dialog list" /> 

    </RelativeLayout> 

Custom.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<ListView 
    android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:divider="#b5b5b5" 
    android:dividerHeight="1dp" /> 
</LinearLayout> 

正如我所說的,如果可能的話我想開一個新的活動當列表視圖被點擊時。 我已經看過並嘗試過,但沒有運氣希望有人可以幫助

回答

1

當你初始化IntentsthisonItemClick。您需要將其更改爲NameOfYourActivity.this

newActivity = new Intent(ActivityName.this, reservetickets.class); 

此外,你應該使用不同的變量爲Intents這樣,但你也可以簡化創建Intents的方式。這也會照顧你的其他問題。見this answer。這看起來很難,但我認爲它簡化了一些東西,使其更加便攜。

+0

我不能將代碼放入我的項目中,因爲我不知道它放在哪裏以查看它是否可以工作 – coolcat

+1

您可以將它放在'onItemClick()'中。它現在在'onClick()'中,所以它基本上是一樣的。你只需用你的包名替換'package','act'就是你想要啓動的'Activity'的名字。 – codeMagic

+0

你必須做一些改變,比如''position'上的'switch'而不是'View's'id',但它的概念相同 – codeMagic

1

意圖newActivity在一個塊中多次聲明。提取出來只聲明一次。

編輯:用popup()方法代替listView.setOnItemClickListener(...)部分。如第二個答案所述,您應該使用MainActivity.this,而不是this,因爲this將引用OnItemClickListener.this

listView.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
     Intent newActivity; 
     switch(position) 
     { 
     case 0: newActivity = new Intent(MainActivity.this, superleague.class);  
      startActivity(newActivity); 
      break; 
     case 1: newActivity = new Intent(MainActivity.this, youtube.class);  
      startActivity(newActivity); 
      break; 
     case 2: newActivity = new Intent(MainActivity.this, olympiakos.class);  
      startActivity(newActivity); 
      break; 
     case 3: newActivity = new Intent(MainActivity.this, karaiskaki.class);  
      startActivity(newActivity); 
      break; 
     case 4: newActivity = new Intent(MainActivity.this, reservetickets.class);  
      startActivity(newActivity); 
      break; 
     } 
     dialog.dismiss(); 
    } 
}; 
+0

是否將這個代碼放在它的工作 – coolcat

+1

http://img23.imageshack.us/img23/9252/18324662.jpg – coolcat

+0

不是「插入」,只是「替換」。 – Naetmul

相關問題