我现在似乎无法让这段代码工作。我想通过单击列表视图打开一个新活动做。
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
import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void popUp(View v){
// Dummy list:
ArrayList<String> dummies = new ArrayList<String>();
dummies.add("BMW");
dummies.add("FORD");
dummies.add("ROVER");
dummies.add("BMW");
dummies.add("FORD");
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.customalertdialogdctivity);
dialog.setTitle("List Title");
ListView listView = (ListView) dialog.findViewById(R.id.list);
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.listrow ,
R.id.singleItem, dummies);
listView.setAdapter(ad);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//do something on click
dialog.dismiss();
}
});
dialog.show();
}
}
主要的.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>
自定义.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>
正如我所说,如果可能的话,我想在单击列表视图时打开一个新活动。我已经看过并尝试过,但没有运气希望有人可以提供帮助