Android:如何通过单击 ListView 中的项目从一个活动切换到另一个活动

发布于 2024-12-08 15:31:25 字数 2597 浏览 4 评论 0原文

我制作了包含播放、设置、退出的菜单列表。但是单击按钮不会将我带到所需的活动,侦听器无法正常工作..任何人都可以帮助我解决问题..将感激不已.. 是的,它是一个列表视图控件。

我的代码中有两个错误,其中一个是 @@ new AdapterView.OnItemClickListener(){} 类型必须实现 LINE1 中继承的抽象方法 AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)@@

而另外一个是 @@ View 无法解析为类型@@ 在第 2 行 “实际上,当我单击主菜单屏幕中的项目时,我想从一个屏幕切换到另一个屏幕”

这是主菜单的代码

public class MenuActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        ListView menuList = (ListView) findViewById(R.id.list);
        String[] items = {
            getResources().getString(R.string.pla),
            getResources().getString(R.string.sco),
            getResources().getString(R.string.set),
            getResources().getString(R.string.hel),
            getResources().getString(R.string.qui)
        };
        ArrayAdapter < String > adapt = new ArrayAdapter < String > (this, R.layout.menu_items, items);
        menuList.setAdapter(adapt);
        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { //LINE 1 error 
            public void onItemClick(AdapterView <? > parent, View itemClicked, //LINE 2 error
                int position, long id) {
                TextView textView = (TextView) itemClicked;
                String strText = textView.getText().toString();
                if (strText.equalsIgnoreCase(getResources().getString(
                    R.string.pla))) {
                    // Game
                    startActivity(new Intent(MenuActivity.this,
                        GameActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(
                    R.string.hel))) {
                    // Help
                    startActivity(new Intent(MenuActivity.this,
                        HelpActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(
                    R.string.set))) {
                    //Settings 
                    startActivity(new Intent(MenuActivity.this,
                        SettingsActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(
                    R.string.sco))) {
                    //  Scores 
                    startActivity(new Intent(MenuActivity.this,
                        ScoresActivity.class));
                }
            }
        });
    }
}

i have made menu list containing play, settings, exit. but clicking the button doesn take me to the desired activity the listner is not working..can any one help me solve the problem.. will b thankfull..
yes its a list view control..

there are two errors in my code one is that
@@ The type new AdapterView.OnItemClickListener(){} must implement the inherited abstract method AdapterView.OnItemClickListener.onItemClick(AdapterView, View, int, long)@@ in LINE1

And the other one is @@ View cannot be resolved to a type@@ in LINE 2
"Actually i want to shift from one screen to another when i click on the item in main menu screen"

Here is the code of main menu

public class MenuActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        ListView menuList = (ListView) findViewById(R.id.list);
        String[] items = {
            getResources().getString(R.string.pla),
            getResources().getString(R.string.sco),
            getResources().getString(R.string.set),
            getResources().getString(R.string.hel),
            getResources().getString(R.string.qui)
        };
        ArrayAdapter < String > adapt = new ArrayAdapter < String > (this, R.layout.menu_items, items);
        menuList.setAdapter(adapt);
        menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { //LINE 1 error 
            public void onItemClick(AdapterView <? > parent, View itemClicked, //LINE 2 error
                int position, long id) {
                TextView textView = (TextView) itemClicked;
                String strText = textView.getText().toString();
                if (strText.equalsIgnoreCase(getResources().getString(
                    R.string.pla))) {
                    // Game
                    startActivity(new Intent(MenuActivity.this,
                        GameActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(
                    R.string.hel))) {
                    // Help
                    startActivity(new Intent(MenuActivity.this,
                        HelpActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(
                    R.string.set))) {
                    //Settings 
                    startActivity(new Intent(MenuActivity.this,
                        SettingsActivity.class));
                } else if (strText.equalsIgnoreCase(getResources().getString(
                    R.string.sco))) {
                    //  Scores 
                    startActivity(new Intent(MenuActivity.this,
                        ScoresActivity.class));
                }
            }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

微暖i 2024-12-15 15:31:25
ListView lv = ...;
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3) 
{
  //Do Your Stuff
}
});

或者您可以制作自定义适配器并编写按钮的单击事件

ListView lv = ...;
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0,View arg1, int arg2, long arg3) 
{
  //Do Your Stuff
}
});

or you can make your custom adapter and write the button's click event

听不够的曲调 2024-12-15 15:31:25

使用意图

Intent play= new Intent(getApplicationContext(),Play.class);
startActivity(play);

有很多关于菜单的教程

use intent

Intent play= new Intent(getApplicationContext(),Play.class);
startActivity(play);

there's lots of tutorial about menu

故人如初 2024-12-15 15:31:25

此任务可以使用名为 Intents 的 Android 主要构建块之一和属于 Activity 类的方法之一 public void startActivity (Intent Intent) 来完成。

意图是要执行的操作的抽象描述。它可以与startActivity一起使用来启动一个Activity,与broadcastIntent一起使用来将其发送到任何感兴趣的BroadcastReceiver组件,并与startService(Intent)或bindService(Intent, ServiceConnection, int)一起使用来与后台服务进行通信。

Intent 提供了一种在不同应用程序中的代码之间执行后期运行时绑定的工具。它最重要的用途是启动活动,可以将其视为活动之间的粘合剂。它基本上是一种被动数据结构,保存要执行的操作的抽象描述。

参考官方文档 -- http://developer.android.com/reference/android /content/Intent.html

public void startActivity (Intent Intent) -- 用于启动一个新的活动。

因此,假设您有两个 Activity 类,并且在单击按钮的 OnClickListener() 时,您想从一个 Activity 移动到另一个 Activity,然后 --

  1. PresentActivity -- 这是您当前的您要从该活动转到第二个活动。

  2. NextActivity -- 这是您要移动的下一个 Activity。

所以意图是这样的

Intent(PresentActivity.this, NextActivity.class)

最后这将是完整的代码

  public class PresentActivity extends Activity {
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            setContentView(R.layout.content_layout_id);

            final Button button = (Button) findViewById(R.id.button_id);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   

                    Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);

                    // currentContext.startActivity(activityChangeIntent);

                    PresentActivity.this.startActivity(activityChangeIntent);
                }
            });
        }
    }

这个例子与按钮单击有关,您可以在任何您想要的地方使用按钮单击的 OnClickListener() 内编写的代码在您的活动之间切换,就像在 setOnItemClickListener 中一样。

This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html

public void startActivity (Intent intent) -- Used to launch a new activity.

So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --

  1. PresentActivity -- This is your current activity from which you want to go the second activity.

  2. NextActivity -- This is your next Activity on which you want to move.

So the Intent would be like this

Intent(PresentActivity.this, NextActivity.class)

Finally this will be the complete code

  public class PresentActivity extends Activity {
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            setContentView(R.layout.content_layout_id);

            final Button button = (Button) findViewById(R.id.button_id);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   

                    Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);

                    // currentContext.startActivity(activityChangeIntent);

                    PresentActivity.this.startActivity(activityChangeIntent);
                }
            });
        }
    }

This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities like in your setOnItemClickListener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文