简体   繁体   中英

How to navigate in to another activity using list item click in Android

I want to know how can I navigate to a new activity on click of a list item using onItemClickListener method. I know we use Intents for the same but can someone provide me with sample code?

I am not providing any adapter to listview to populate it, make sure you do provide some adapter with some data and Activity name's are dummy you would have to define them as well

private static ListView listView;
    listView = (ListView) findViewById(R.id.listView);

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                Intent intent = null;
            switch(position){
            case 0:
                intent = new Intent(getApplicationContext(), AClassName.class);
                break;
            case 1:
                intent = new Intent(getApplicationContext(), AClassName.class);
                break;
            case 2:
                intent = new Intent(getApplicationContext(), AClassName.class);
                break;
            case 3:
                intent = new Intent(getApplicationContext(), AClassName.class);
                break;
            case 4:
                intent = new Intent(getApplicationContext(), AClassName.class);
                break;
            case 5:
                intent = new Intent(getApplicationContext(), AClassName.class);
                break;
            default:
                intent = new Intent(getApplicationContext(), AClassName.class);
                break;
            }

            if(intent != null){
                startActivity(intent);
            }

        }
    });

every case in switch statement denotes a different activity that you want to open, like this you can open activities depending on which item clicked in the list

When implementing the onClickListener, you can use v.getContext.startActivity

btn.setOnClickListener(new OnClickListener() {                   
    @Override 
    public void onClick(View v) {
        v.getContext().startActivity(PUT_YOUR_INTENT_HERE);
    } 
}); 

use mList.setOnItemClickListener(new ListItemClickListener());

private class ListItemClickListener implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long idOfView) {
            Intent intent = new Intent(WorkingActivity.this, TargetActivity.class);
            startActivity(intent);
        }
    }

You need to use set up the listener on the listview. Use setOnItemClickListener.

mlistView.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                                //Handle the click here
                }
              });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM