有没有更有效的方法来制作我的ListView?只用 Android 编程了大约 2 周
我对 Android 编程和一般编程非常陌生。以前没有学过任何东西,只是想学习一些 android 开发。无论如何,我会粘贴我的代码,但我想知道是否有更好、更有效的方法来创建我正在做的事情,因为我花了大约 3 天的时间才弄清楚如何创建它,因为我找不到任何有关使用 ListView 项目设置意图的教程。提前感谢您的帮助:)
public class main extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] listitems = new String[] {"1","2","3","4","5"};
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout,
R.id.label, listitems));
}
//这主要是我不确定的部分:)
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
switch (position){
case 0:
Intent second = new Intent(this, second.class);
startActivity(second);
}
return;
}
}
I'm extremely new to programming android and programming in general. Haven't learnt anything previous and was just interested in learning some android dev. Anyhow I'll paste my code, but I was wondering if there's a better and more efficient way to create what I'm doing, because it took my about 3 days to even work out how to create this as I couldn't find any tutorials on setting intents up with ListView items. Thanks for your help in advance :)
public class main extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] listitems = new String[] {"1","2","3","4","5"};
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout,
R.id.label, listitems));
}
//This is mainly the section I'm unsure about :)
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
switch (position){
case 0:
Intent second = new Intent(this, second.class);
startActivity(second);
}
return;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码在技术上没有问题,但您的 onListItemClick() 方法实现有些不寻常。使用 ListView 时的典型模式是获取您单击的项目,并对其执行某些操作,或将其传递给另一个活动。一个简单的例子:
The code is technically OK, but your onListItemClick() method implementation is somewhat unusual. A typical pattern when using ListView is to get the item you clicked on, and do something with it, or pass it to another activity. A simple example: