我似乎無法得到此代碼在此刻工作。我想單擊列表視圖打開一個新的活動我知道這個代碼是什麼後,但我一直在玩這幾天現在,我只是不能做到這一點。對話框修復中的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>
正如我所說的,如果可能的話我想開一個新的活動當列表視圖被點擊時。 我已經看過並嘗試過,但沒有運氣希望有人可以幫助
我不能將代碼放入我的項目中,因爲我不知道它放在哪裏以查看它是否可以工作 – coolcat
您可以將它放在'onItemClick()'中。它現在在'onClick()'中,所以它基本上是一樣的。你只需用你的包名替換'package','act'就是你想要啓動的'Activity'的名字。 – codeMagic
你必須做一些改變,比如''position'上的'switch'而不是'View's'id',但它的概念相同 – codeMagic