开发者

Android: how do i switch from one activity to another by clicking items from the ListView

开发者 https://www.devze.com 2023-04-11 15:22 出处:网络
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 thankful

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.OnItemClickLi开发者_运维技巧stener(){} 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));
                }
            }
        });
    }
}


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


use intent

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

there's lots of tutorial about menu


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消