gautamprajapati
gautamprajapati

Reputation: 2095

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?

Upvotes: 1

Views: 2468

Answers (4)

Pankaj Nimgade
Pankaj Nimgade

Reputation: 4549

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

Upvotes: 2

Adilp
Adilp

Reputation: 459

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
                }
              });

Upvotes: 0

Logic
Logic

Reputation: 2258

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);
        }
    }

Upvotes: 0

Muhammad Waleed
Muhammad Waleed

Reputation: 2601

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);
    } 
}); 

Upvotes: 0

Related Questions