创建ListView时遇到问题,因为我无法启动新活动。 这是代码
public class MainActivity extends Activity {
static final String[] modules = new String[] { "Modulo 442", "Modulo 433" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> codeLearnArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, modules);
ListView codeLearnLessons = (ListView) findViewById(R.id.listView);
codeLearnLessons.setAdapter(codeLearnArrayAdapter);
}
protected void onListItemClick(ListView l, View v, int pos, long id) {
if (pos==0){
Intent intent = new Intent(this, Module442.class);
startActivity(intent);
}
else if (pos==1){
Intent intent = new Intent(this, Module433.class);
startActivity(intent);
}
}
}
这是布局文件的代码:
<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" >
<ListView android:id="@+id/listView"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
当我点击选项时,没有任何事情发生......
这是另一项新活动:
public class Module442 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_module442);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.module442, menu);
return true;
}
}
感谢您的帮助!!!!
解决
公共类MainActivity扩展了Activity {
/** Called when the activity is first created. */
ListView list;
private String array[] = { "Modulo 442", "Modulo 433" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, array));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> paramAdapterView,
View paramView, int position, long paramLong) {
switch (position) {
case 0:
Intent newActivity0 = new Intent(MainActivity.this,
Module442.class);
startActivity(newActivity0);
break;
case 1:
Intent newActivity1 = new Intent(MainActivity.this,
Module433.class);
startActivity(newActivity1);
break;
}
}
});
}
}
答案 0 :(得分:4)
您尚未在Listener
上设置ListView
。
ListView codeLearnLessons = (ListView) findViewById(R.id.listView);
codeLearnLessons.setAdapter(codeLearnArrayAdapter);
codeLearnLessons.setOnItemClickListener(this); // here
然后你会想要以这种方式将它添加到你的类声明中
public class MainActivity extends Activity implements OnItemClickListener{
您应该将@Override
添加到onItemClick()
@Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
答案 1 :(得分:0)
你想这样做:
codeLearnLessons.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position ==0){
Intent intent = new Intent(this, Module442.class);
startActivity(intent);
}
else if (position ==1){
Intent intent = new Intent(this, Module433.class);
startActivity(intent);
}
}
});