使用ListView控件开始新的活动 [英] Using ListView to start new activities
本文介绍了使用ListView控件开始新的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,当我创建一个ListView,因为我不能启动新的活动。 这是code
I have a problem when I create a ListView because I'm not able to start new activities. This is the code
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);
}
}
}
这就是布局文件的code:
and this is the code of the layout file:
<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>
当我点击选项没有发生......
When I click on the options nothing happen...
这是另一种新的活动:
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;
}
}
感谢您的帮助!
Thanks for your help!!!!
解决
公共类MainActivity延伸活动{
public class MainActivity extends 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;
}
}
});
}
}
推荐答案
您想要做这样的事情:
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);
}
}
});
这篇关于使用ListView控件开始新的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文