我尝试实现和意图在用户点击ListView
上的项目时为其带来新活动。它没有识别onItemClick
方法而且我不断收到消息
"无法解析onItemClick(android.widget.AdapterView>,android View.view,int,long)'
我的活动代码
public class LaunchActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
String[] subjects = {"Physics", "Biology", "Economics"};
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subjects);
ListView subjectListView = (ListView) findViewById(R.id.subjectListView);
subjectListView.setAdapter(adapter);
subjectListView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
super.onItemClick(parent, view, position, id);
if (position == 0) {
Intent intent = new Intent(LaunchActivity.this, Driver.class);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(LaunchActivity.this, Driver.class);
startActivity(intent);
} else if (position == 2) {
Intent intent = new Intent(LaunchActivity.this, Driver.class);
startActivity(intent);
}
}
}
);
}
}
我的XML文件是
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.owner.omegaphi.LaunchActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/subjectListView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
非常感谢任何帮助。