有没有更有效的方法来制作我的ListView?只用 Android 编程了大约 2 周

发布于 2024-12-02 12:38:56 字数 949 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2024-12-09 12:38:56

该代码在技术上没有问题,但您的 onListItemClick() 方法实现有些不寻常。使用 ListView 时的典型模式是获取您单击的项目,并对其执行某些操作,或将其传递给另一个活动。一个简单的例子:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // get the item we clicked on
    String item = (String) this.getListAdapter().getItem(position);

    Intent second = new Intent(this, second.class);
    // add the clicked item to the intent, to pass it to the second activity
    second.putExtra("com.my.package.listItem", item);
    startActivity(second);
}

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:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // get the item we clicked on
    String item = (String) this.getListAdapter().getItem(position);

    Intent second = new Intent(this, second.class);
    // add the clicked item to the intent, to pass it to the second activity
    second.putExtra("com.my.package.listItem", item);
    startActivity(second);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文